{"id":20605188,"url":"https://github.com/arnab-developer/dynamicdi","last_synced_at":"2026-04-20T02:38:40.308Z","repository":{"id":68915137,"uuid":"248436108","full_name":"Arnab-Developer/DynamicDI","owner":"Arnab-Developer","description":"Example of dynamic dependency injection with asp.net core.","archived":false,"fork":false,"pushed_at":"2021-04-09T11:53:00.000Z","size":674,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-06T16:54:48.258Z","etag":null,"topics":["asp-net-core","dependency-injection"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Arnab-Developer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-19T07:18:03.000Z","updated_at":"2021-12-05T05:48:17.000Z","dependencies_parsed_at":"2023-03-24T11:20:36.998Z","dependency_job_id":null,"html_url":"https://github.com/Arnab-Developer/DynamicDI","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Arnab-Developer/DynamicDI","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2FDynamicDI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2FDynamicDI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2FDynamicDI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2FDynamicDI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Arnab-Developer","download_url":"https://codeload.github.com/Arnab-Developer/DynamicDI/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2FDynamicDI/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32030568,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T00:18:06.643Z","status":"online","status_checked_at":"2026-04-20T02:00:06.527Z","response_time":94,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp-net-core","dependency-injection"],"created_at":"2024-11-16T09:26:58.928Z","updated_at":"2026-04-20T02:38:40.288Z","avatar_url":"https://github.com/Arnab-Developer.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dynamic dependency injection with ASP.NET Core\n\nExample of dynamic depency injection with asp.net core. The tutorial is found at https://edi.wang/post/2018/12/28/dependency-injection-with-multiple-implementations-in-aspnet-core.\n\nASP.NET Core has dependency injection is built in. So you can mention the interface type in your controller and ASP.NET Core will provide the actual object of the interface at runtime. For this to work you need to write some configuration in the startup class.\n\nI have a simple controller and service class. I want to use the service class inside the controller. I can mention the service interface type in the constructor of the controller. Also I need to register the service in the startup class so that ASP.NET Core can provide the actual type in the runtime.\n\n```csharp\npublic interface IService\n{\n    string GetHello(string name);\n}\n\npublic class Service : IService\n{\n    public string GetHello(string name)\n    {\n        return $\"Hello {name}\";\n    }\n}\n\npublic class Startup\n{\n    public void ConfigureServices(IServiceCollection services)\n    {\n        services.AddTransient(typeof(IService), typeof(Service));\n    }\n}\n\npublic class HomeController : Controller\n{\n    private readonly IService _s;\n\n    public HomeController(IService s)\n    {\n        _s = s;\n    }\n\n    public IActionResult Index(string name)\n    {\n        var message = _s.GetHello(name);\n        return View(message);\n    }\n}\n```\n\nBut if you have multiple implimentation of your IService interface then how you instruct ASP.NET Core to provide correct object in runtime? The process is very simple, you can actually get a collection of all implimentations and then choose the appropriate from them.\n\n```csharp\npublic interface IService\n{\n    string GetHello(string name);\n}\n\npublic class ServiceA : IService\n{\n    public string GetHello(string name)\n    {\n        return $\"Hello {name}\";\n    }\n}\n\npublic class ServiceB : IService\n{\n    public string GetHello(string name)\n    {\n        return $\"Hi {name}\";\n    }\n}\n\npublic class Startup\n{\n    public void ConfigureServices(IServiceCollection services)\n    {\n        services.AddTransient(typeof(IService), typeof(ServiceA));\n        services.AddTransient(typeof(IService), typeof(ServiceB));\n    }\n}\n\npublic class HomeController : Controller\n{\n    private readonly IEnumerable\u003cIService\u003e _services;\n\n    public HomeController(IEnumerable\u003cIService\u003e services)\n    {\n        _services = services;\n    }\n\n    public IActionResult Index(string name)\n    {\n        var service = _services.First(s =\u003e s.GetType().Name == \"ServiceA\")\n        var message = service.GetHello(name);\n        return View(message);\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farnab-developer%2Fdynamicdi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farnab-developer%2Fdynamicdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farnab-developer%2Fdynamicdi/lists"}