dazinator / Dazinator.Extensions

Useful additions to Microsoft.Extensions.DependencyInjection such as Named Services.
Other
38 stars 1 forks source link

Factory functions #3

Closed dazinator closed 4 years ago

dazinator commented 4 years ago

Allow named services to be registered with factory functions that take an SP


    var services = new ServiceCollection();
    services.AddNamed<AnimalService>(names =>
    {
        names.AddSingleton("A", (sp)=>new AnimalService()); 
        names.AddSingleton("B", (sp)=>new BearService());       

        names.AddTransient("C", (sp)=>new AnimalService());
        names.AddTransient("D", (sp)=>new LionService());       

        names.AddScoped("E", (sp)=>new AnimalService());
        names.AddScoped("F", (sp)=>new DisposableTigerService());
    });