Skip to content

Instantly share code, notes, and snippets.

@aaronolds
Created June 8, 2018 19:29
Show Gist options
  • Save aaronolds/ee7c8d1aa765a3583ce58c28c90768d9 to your computer and use it in GitHub Desktop.
Save aaronolds/ee7c8d1aa765a3583ce58c28c90768d9 to your computer and use it in GitHub Desktop.
How to use a Service Factory in ASP.net Core
// 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