Skip to content

Repository files navigation

SimpleSoft - DependencyInjection Extensions

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.

Reasons

Some of the reasons to use this little library:

  1. Enables assembly scan;
  2. Use IServiceConfigurator to aggregate related registrations;

Installation

This library can be installed via NuGet package. Just run the following command:

Install-Package SimpleSoft.Extensions.DependencyInjection

Compatibility

This library is compatible with the folowing frameworks:

  • .NET Framework 4.5
  • .NET Standard 1.0

Tipical usage

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});}}

About

Utility classes to extend the Microsoft dependency injection namespaces

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages