Reputation: 93
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
Upvotes: 1
Views: 558
Reputation: 246998
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;
}
//...
Upvotes: 1