0

I am using Unity.WebAPI in my .net project to register and resolve dependencies. I have four projects, that are

i) interfaces
ii) UserService (Actual Implementation)
iii) ProductService (Actual Implementation)
iv) Web API Project which reference all above three, and also contains the unity configuration.

Now my userService and product service concrete classes are dependent on each other, so when i try to resolve them it gives me stack overflow error.

My code in unityConfig at web api project is as follows:

container.RegisterType<IUserService, UserService>(); //Userservice constructor injects IProductService

container.RegisterType<IProducService, ProductService>(); //ProductServices constructor injects IUserservice

I thought that creating separate Interface project would not cause circular dependency.

Looking forward.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Fast Mani
  • 171
  • 8
  • 3
    Why do they depend on each other? How would you instantiate them without dependency injection? – CodeCaster Oct 12 '16 at 21:49
  • 1
    What you describe there is exactly what is a circular dependency. Solution: redesign. – Nkosi Oct 12 '16 at 22:40
  • the solution i have found for now is, in case of circular dependency, i dont inject the IService but i inject container, and use it to resolve IService inside a member function rather than constructor. I don't know if it's a bad design or not, but this is the only way to solve it at the moment. I cannot redesign because i am creating a wrapper over an existing application – Fast Mani Oct 13 '16 at 05:52

0 Answers0