Same Interface used in different Repository, how can access repository object in controller when we use dependency injection.?
Loading
Same Interface used in different Repository, how can access repository object in controller when we use dependency injection.?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Oct 7, 2023, 4:40 AM EDT
An interface can point to any concrete object at run time, the behavior is decided by the dependency container if you have registered your IAnimal interface with Cat then IAnimal will be replaced by an object of Cat.
public class MyController:IController { private readonly IAnimal _animal; public MyController(IAnimal animal) { _animal=animal; //animal= new Cat() by DI; } }Tahir AnsariRank #366 · 6.8K reputationPosted Oct 7, 2023, 4:42 AM EDT
public class MyController : Controller { private readonly IFirstRepository _firstRepository; private readonly ISecondRepository _secondRepository; public MyController(IFirstRepository firstRepository, ISecondRepository secondRepository) { _firstRepository = firstRepository; _secondRepository = secondRepository; } // Your controller actions here... }The Dependency Injection container will resolve and provide the correct repository implementation based on the constructor parameter types.
Use the Correct Repository:
You can now use
_firstRepositoryand_secondRepositoryin your controller actions to access the methods and data provided by each repository. The DI container ensures that you get the appropriate repository instance based on the interface you requested in the constructor.Ajay KumarRank #56 · 29K reputationPosted Oct 7, 2023, 8:49 AM EDT
In the context of dependency injection, if you have multiple implementations of the same interface and you want to use a specific implementation in your controller, you can achieve this by configuring your dependency injection container (such as ASP.NET Core's built-in DI container) to inject the correct implementation of the interface into your controller.
Step 1: Register Implementations with Dependency Injection Container
First, you need to register your interface and its implementations with the dependency injection container. This is usually done in the
Startup.csfile (for ASP.NET Core applications) within theConfigureServicesmethod.public void ConfigureServices(IServiceCollection services) { // Register the implementations of your interface services.AddTransient(); services.AddTransient(); // Add other services and configurations as needed }In this example,
Implementation1andImplementation2are two different classes implementingIYourInterface.Step 2: Inject the Interface into Your Controller
Next, you can inject the interface into your controller constructor. The DI container will handle the rest, providing the appropriate implementation based on your registration.
public class YourController : Controller { private readonly IYourInterface _yourInterface; public YourController(IYourInterface yourInterface) { _yourInterface = yourInterface; } // Now you can use _yourInterface in your controller actions }