Library that helps to register services into the IServiceCollection, Microsoft's dependency injection facade, by enabling the developer to scan assemblies for service attributes or configurators.
Some of the reasons to use this little library:
- Enables assembly scan;
- Use
IServiceConfiguratorto aggregate related registrations;
This library can be installed via NuGet package. Just run the following command:
Install-Package SimpleSoft.Extensions.DependencyInjectionThis library is compatible with the folowing frameworks:
- .NET Framework 4.5
- .NET Standard 1.0
usingSystem;usingSystem.Linq;usingMicrosoft.Extensions.DependencyInjection;usingSimpleSoft.Extensions.DependencyInjection;publicclassProgram{publicstaticvoidMain(string[]args){varprovider=newServiceCollection().AddServicesFrom<Program>().BuildServiceProvider();using(varscope=provider.GetRequiredService<IServiceScopeFactory>().CreateScope()){varrepositories=scope.ServiceProvider.GetServices<IRepository>();Console.WriteLine("Total repositories: '{0}'",repositories.Count());varusers=scope.ServiceProvider.GetRequiredService<IUserRepository>();Console.WriteLine(users);varroles=scope.ServiceProvider.GetRequiredService<IRoleRepository>();Console.WriteLine(roles);}}}publicinterfaceILogger{voidLog(stringmessage);}publicinterfaceIRepository{}publicinterfaceIUserRepository:IRepository{}publicinterfaceIRoleRepository:IRepository{}// Equivalent code:// services.TryAddSingleton<ILogger, Logger>();[Service(TryAdd=true,TypesToRegister=new[]{typeof(ILogger)})]publicclassLogger:ILogger,IDisposable{privatereadonlyLoggerOptions_options;publicLogger(LoggerOptionsoptions){_options=options;}publicvoidLog(stringmessage){if(_options.Enabled)Console.WriteLine(message);}publicvoidDispose(){// do nothing}}publicclassLoggerOptions{publicboolEnabled{get;set;}}publicabstractclassRepository:IRepository{protectedRepositoryOptionsOptions{get;}protectedRepository(RepositoryOptionsoptions){Options=options;}}// Equivalent code:// services.AddScoped<IRepository, UserRepository>();// services.AddScoped<IUserRepository, UserRepository>();// services.AddScoped<UserRepository>();[Service(ServiceLifetime.Scoped,Registration=RegistrationType.All)]publicclassUserRepository:Repository,IUserRepository{publicUserRepository(RepositoryOptionsoptions,ILoggerlogger):base(options){logger.Log(string.Concat("Created new ",nameof(UserRepository)));}}// Equivalent code:// services.AddScoped<IRepository, RoleRepository>();// services.AddScoped<IRoleRepository, RoleRepository>();// services.AddScoped<RoleRepository>();[Service(ServiceLifetime.Scoped,Registration=RegistrationType.All)]publicclassRoleRepository:Repository,IRoleRepository{publicRoleRepository(RepositoryOptionsoptions,ILoggerlogger):base(options){logger.Log(string.Concat("Created new ",nameof(RoleRepository)));}}publicclassRepositoryOptions{publicstringConnectionString{get;set;}}// Class will also be scaned Configure will be invokedpublicclassServiceConfigurator:IServiceConfigurator{publicvoidConfigure(IServiceCollectionservices){services.AddSingleton(k =>newRepositoryOptions{ConnectionString="some connection string"});}}// Class will also be scaned Configure will be invokedpublicclassLoggerConfigurator:IServiceConfigurator{publicvoidConfigure(IServiceCollectionservices){services.AddSingleton(k =>newLoggerOptions{Enabled=true});}}