I need some sample example with parameterized constructor using reprository and web-API
I tried this:
public class TestUserController : ApiController
{
private readonly IUserRepository UserRepository;
public TestUsersController()
{
}
public TestUsersController(IUserRepository UserRepository)
{
this.UserRepository = UserRepository;.........this parameterized constructor is not hitting and getting always null
}
{
private readonly IUserRepository UserRepository;
public TestUsersController()
{
}
public TestUsersController(IUserRepository UserRepository)
{
this.UserRepository = UserRepository;.........this parameterized constructor is not hitting and getting always null
}
Anjali KumariPosted Jun 29, 2017, 3:08 AM
Saravanan VPosted Jun 28, 2017, 10:42 AM
Thought you had already know some basics about DI by seeing your code.
Dependency Injection (DI) is a design pattern that demonstrates how to create loosely coupled classes. It is a technique for achieving loose coupling between objects and their collaborators, or dependencies.
You should be able to get hold of DI after reading this article.
http://www.c-sharpcorner.com/UploadFile/ff2f08/dependency-injection-pattern/
In your code, you are trying to inject an object of a class that implements the "IUserRepository" interface.
The advantage of using DI is if you want to change the implementation logic for user repository, you could just create another class which has the new implementation.
In this regard, you don't have change all of your controllers class code ( which has IUserRepository as parameter in constructor) since the dependency or the new implementation will be dynamically injected by the DI framework.
Lot of DI frameworks are available in the market. Unity (from Microsoft), Autofac, StructureMap, Ninject are few of the popular frameworks in the market. You need to figure out which one best suits for your project.
Other Useful Links for references:
https://msdn.microsoft.com/en-us/library/hh323705(v=vs.100).aspx
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection
http://www.c-sharpcorner.com/UploadFile/33b051/dependency-injection-using-microsoft-unity/
http://www.c-sharpcorner.com/UploadFile/eec362/dependency-injection-in-mvc/
When You Shouldn’t Use Dependency Injection
https://msdn.microsoft.com/en-us/library/dn178469(v=pandp.30).aspx
Please mark this answer as accepted if it fullfills your question.
Anjali KumariPosted Jun 28, 2017, 10:01 AM
Saravanan VPosted Jun 28, 2017, 9:51 AM
It looks like you are trying to inject the dependency for Web API controller. You need to remove the parameterless constructor and keep the parameterized constructor.
Then, you have to make sure the dependency injection is rightly configured for all of your repositories before actually using them in the controllers.
Having all of these in place, the dependency for the constructor will be properly injected by the framework when an object for the controller is created.
Please find below links for your reference on implementing Dependency Injection in Web API.
http://www.c-sharpcorner.com/UploadFile/1492b1/restful-day-2-inversion-of-control-using-dependency-injecti/
https://stackoverflow.com/questions/33614296/mvc-web-api-and-unity-for-dependency-injection
https://www.devtrends.co.uk/blog/using-unity.mvc5-and-unity.webapi-together-in-a-project
Hope this helps you.