- reference this package to your project: https://www.nuget.org/packages/Samhammer.DependencyInjection/
- call ResolveDependencies on the IServiceCollection
services.ResolveDependencies();
- add Inject attribute to the class
- set target optionally (default: Matching)
- set servicelifetime optionally (default: Scoped)
- will register as service to interface with same name as class (IClassname)
[Inject(Target.Matching, ServiceLifetime.Scoped)]
public class Class : IClass
{
}
public interface IClass : IBaseClass
{
}
public interface IBaseClass
{
}
- will register as service to all implemented interfaces of class
- ATTENTION with lifetime singleton / scoped. Each interface service returns his own instance of the class
[Inject(Target.All, ServiceLifetime.Scoped)]
public class Class : IClass
{
}
public interface IClass : IBaseClass
{
}
public interface IBaseClass
{
}
- add InjectAs attribute to the class
- add interface or class as paramter is required (class will register to this type)
[InjectAs(typeof(IServiceToRegister))]
public class ClassWithSpecificService: IServiceToRegister, IServiceNotRegister
{
}
public interface IServiceToRegister
{
}
public interface IServiceNotRegister
{
}
- add Factory attribute to the factory class
- add at least one public static method with parameter IServiceProvider
- the returned instance will be registered to the return type of method
[Factory]
public class Factory
{
public static IClassFromFactory Build(IServiceProvider serviceProvider)
{
return new ClassFromFactory();
}
}
Starting with version 3.1.5 all customizations needs to be done with the options action.
The registrations to servicecollection will no longer be used because we dont want to use ioc to setup ioc. @see also https://docs.microsoft.com/de-de/dotnet/core/compatibility/2.2-3.1#hosting-generic-host-restricts-startup-constructor-injection
By default the project will not do any logging, but you can activate it. This will require that you provide an ILoggerFactory from Microsoft.Extensions.Logging.
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
services.ResolveDependencies(options => options.SetLogging(loggerFactory));
services.ResolveDependencies(options => options.SetLogging(new SerilogLoggerFactory()));
By default the project will only resolve types of project assemblies, but not on packages or binaries. But you can replace the default strategy with your own implementation.
services.ResolveDependencies(options => options.SetAssemblyStrategy(new MyAssemblyResolvingStrategy()));
It is possible to change the way types are found by the DependencyInjectionAttribute. Also one could change the naming schema of interfaces by using a custom type resolving strategy.
services.ResolveDependencies(options => options.SetTypeStrategy(new MyTypeResolvingStrategy()));
By default the project will only resolve types with Inject attributes. But you can add additonal resolving provider with your own implementation.
services.ResolveDependencies(options => options.AddProvider<MyServiceDescriptorProvider>((logger, o) => new MyServiceDescriptorProvider(logger, o)));
You can use overrides to have a different behavior depending on the configuration used on resolving dependencies.
Just add the override attribute to a new class that inherits a class with one of the dependency injection attributes. As soon as the configuration matches "MyConfigName" your original implementation will be overwritten by the implementation of this class.
Classes with non matching configuration names are not registered.
[Override("MyConfigName")]
public class MyConfigNameOverride : InjectParentClass
{
}
[Override("OtherConfigName")]
public class OtherConfigNameOverride : InjectParentClass
{
}
[Inject]
public class InjectParentClass : IInjectParentClass
{
}
public interface IInjectParentClass
{
}
Add this nuget package to your project: https://www.nuget.org/packages/Samhammer.DependencyInjection.Override/
serviceCollection.ResolveDependencies(o => { o.UseOverride("MyConfigName"); });
- Create a tag and let the github action do the publishing for you