Created
June 8, 2018 19:29
-
-
Save aaronolds/ee7c8d1aa765a3583ce58c28c90768d9 to your computer and use it in GitHub Desktop.
How to use a Service Factory in ASP.net Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Startup.cs | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddTransient<Func<key, IRepository<model>>>(serviceProvider => key => | |
{ | |
var repositoryServiceFactory = new RepositoryServiceFactory(); | |
return repositoryServiceFactory.Build(key, Configuration); | |
}); | |
} | |
. | |
. | |
. | |
. | |
. | |
// In the controller | |
public MyController(Func<key, IRepository<model>> repository) | |
{ | |
_repository = repository("ModelA"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment