繁体   English   中英

如何在 mvc 5 中使用实体框架实现依赖注入

[英]How to implement dependency injection using entity framework in mvc 5

我正在尝试使用实体框架实现依赖注入,但它给出了“Unity.Container.dll 中发生'System.StackOverflowException'类型的未处理异常”的异常,并且应用程序处于中断模式

public class CategoryRepository : ICategoryRepository
{
    private LaundryManagementSystemEntities context;
    private ICategoryRepository _iCategory;

    //public CategoryRepository(LaundryManagementSystemEntities db) //For repositoty Patterns or Unit of work
    //{
    //    this.context = db;
    //}

        //For dependency Injection
        public CategoryRepository(ICategoryRepository iCategory,LaundryManagementSystemEntities _context)
    {
        this._iCategory = iCategory;
        this.context = _context;
    }


    public void CreateCategory(CategoryViewModel categoryViewModel)
    {
        var category = new Category();
        category.CategoryName = categoryViewModel.CategoryName;
        category.IsActive = categoryViewModel.IsActive;
        context.Categories.Add(category);
        context.SaveChanges();
    }

这里正在制作类别的存储库 class

 public interface ICategoryRepository:IDisposable
{
    List<Category> GetCategories();
    Category GetCategoryById(int? categoryId);
    void CreateCategory(CategoryViewModel category);
    void DeleteProductOfCategory(int productId);
    void DeleteCategory(int categoryId);
    void PostEditCategory(CategoryViewModel category);
    CategoryViewModel GetEditCategory(int? categoryId);
}

这是一个界面

 public class CategoryController : AdminBaseController
{
    LaundryManagementSystemEntities db = new LaundryManagementSystemEntities();

    private ICategoryRepository interfaceobj;
    //private UnitOfWork unitOfWork;

    public CategoryController(ICategoryRepository iCategory)
    {
        this.interfaceobj = iCategory;

        //For Repositorypatterns
        //this.interfaceobj = new CategoryRepository(new LaundryManagementSystemEntities());
        //For Unit Of Work
       // this.unitOfWork = new UnitOfWork(new LaundryManagementSystemEntities());
    }

    // GET: Category        
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult Create()
    {
        CategoryViewModel categoryViewModel = new CategoryViewModel();
        return PartialView("Create",categoryViewModel);
    }

    [HttpPost]
    public ActionResult Create(CategoryViewModel category)
    {
        if (ModelState.IsValid)
        {
            interfaceobj.CreateCategory(category);
           // unitOfWork.CategoryRepository.CreateCategory(catogery);
           // interfaceobj.CreateCategory(catogery);
        }
        return PartialView("Create",category);
    }

这是 controller

我没有得到例外

我想正确了解它以及它将如何运行

ICategoryRepository注入从同一接口派生的CategoryRepository会创建循环/循环依赖关系,从而导致堆栈溢出。

删除该依赖项。 最初显示的代码似乎没有使用也不需要这种依赖关系。

public class CategoryRepository : ICategoryRepository {
    private readonly LaundryManagementSystemEntities context;

    public CategoryRepository(LaundryManagementSystemEntities context) {
        this.context = context;
    }

//...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM