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
using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using SimpleSoft.Extensions.DependencyInjection;
public class Program
{
public static void Main(string[] args)
{
var provider =
new ServiceCollection()
.AddServicesFrom<Program>()
.BuildServiceProvider();
using (var scope = provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var repositories = scope.ServiceProvider.GetServices<IRepository>();
Console.WriteLine("Total repositories: '{0}'", repositories.Count());
var users = scope.ServiceProvider.GetRequiredService<IUserRepository>();
Console.WriteLine(users);
var roles = scope.ServiceProvider.GetRequiredService<IRoleRepository>();
Console.WriteLine(roles);
}
}
}
public interface ILogger
{
void Log(string message);
}
public interface IRepository { }
public interface IUserRepository : IRepository { }
public interface IRoleRepository : IRepository { }
// Equivalent code:
// services.TryAddSingleton<ILogger, Logger>();
[Service(TryAdd = true, TypesToRegister = new[] {typeof(ILogger)})]
public class Logger : ILogger, IDisposable
{
private readonly LoggerOptions _options;
public Logger(LoggerOptions options)
{
_options = options;
}
public void Log(string message)
{
if (_options.Enabled)
Console.WriteLine(message);
}
public void Dispose()
{
// do nothing
}
}
public class LoggerOptions
{
public bool Enabled { get; set; }
}
public abstract class Repository : IRepository
{
protected RepositoryOptions Options { get; }
protected Repository(RepositoryOptions options)
{
Options = options;
}
}
// Equivalent code:
// services.AddScoped<IRepository, UserRepository>();
// services.AddScoped<IUserRepository, UserRepository>();
// services.AddScoped<UserRepository>();
[Service(ServiceLifetime.Scoped, Registration = RegistrationType.All)]
public class UserRepository : Repository, IUserRepository
{
public UserRepository(RepositoryOptions options, ILogger logger) : 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)]
public class RoleRepository : Repository, IRoleRepository
{
public RoleRepository(RepositoryOptions options, ILogger logger) : base(options)
{
logger.Log(string.Concat("Created new ", nameof(RoleRepository)));
}
}
public class RepositoryOptions
{
public string ConnectionString { get; set; }
}
// Class will also be scaned Configure will be invoked
public class ServiceConfigurator : IServiceConfigurator
{
public void Configure(IServiceCollection services)
{
services.AddSingleton(k => new RepositoryOptions
{
ConnectionString = "some connection string"
});
}
}
// Class will also be scaned Configure will be invoked
public class LoggerConfigurator : IServiceConfigurator
{
public void Configure(IServiceCollection services)
{
services.AddSingleton(k => new LoggerOptions
{
Enabled = true
});
}
}