1

I am trying to implement dependency injection using entity framework but it is giving an exception of "An unhandled exception of type 'System.StackOverflowException' occurred in Unity.Container.dll" and application is gone on break mode

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();
    }

Here is making Repository class of category

 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);
}

This is an interface

 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);
    }

This is the controller

I am not getting the exception

I want to know about it properly and how it would run

1 Answer 1

1

The injection of ICategoryRepository into CategoryRepository which is derived from the same interface is creating a cyclic/circular dependency which is causing the stack to overflow.

Remove that dependency. The code originally shown does not appear to use nor need that dependency.

public class CategoryRepository : ICategoryRepository {
    private readonly LaundryManagementSystemEntities context;

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

//...
Sign up to request clarification or add additional context in comments.

6 Comments

Secondly i want to ask that the constructor for dependency injection or repository pattern/Unit of work is same in repository class?
@mishamajid I do not understand what you mean by that last comment. Can you rephrase to clarify what it is you want.
I wanted to ask that we make constructor in Repository class to pass context. So the implementation for constructor will be same in case of Unit of work, repository pattern and dependency injection. Like here in my code the you have corrected constructor of repository class. This will be implemented both for unit of work and repository pattern?
And which is the efficient and reliable approach? Unit of work or dependency injection?
@mishamajid Those are completely separate concepts but Your UoW can rely on DI to get its dependencies.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.