Samhammer.DependencyInjection 6.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Samhammer.DependencyInjection --version 6.0.0                
NuGet\Install-Package Samhammer.DependencyInjection -Version 6.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Samhammer.DependencyInjection" Version="6.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Samhammer.DependencyInjection --version 6.0.0                
#r "nuget: Samhammer.DependencyInjection, 6.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Samhammer.DependencyInjection as a Cake Addin
#addin nuget:?package=Samhammer.DependencyInjection&version=6.0.0

// Install Samhammer.DependencyInjection as a Cake Tool
#tool nuget:?package=Samhammer.DependencyInjection&version=6.0.0                

Samhammer.DependencyInjection

Usage

How to add this to your project:
services.ResolveDependencies();
How to register a class as service
  • add Inject attribute to the class
  • set target optionally (default: Matching)
  • set servicelifetime optionally (default: Scoped)
Target Matching
  • 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
{
}
Target All
  • 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
{
}
How to register a class as service with specific type
  • 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
{
}
How to register a instance as service
  • 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();
    }
}

Configuration

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

How to enable logging?

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.

Sample with microsoft console logger.
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
services.ResolveDependencies(options => options.SetLogging(loggerFactory));
Sample with serilog logger. (you need to setup serilog before)
services.ResolveDependencies(options => options.SetLogging(new SerilogLoggerFactory()));
How to change assembly resolving strategy?

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.SetStrategy(new MyAssemblyResolvingStrategy()));
How to add additional service provider

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, strategy) => new MyServiceDescriptorProvider(logger, strategy)));

Contribute

How to publish package
  • Create a tag and let the github action do the publishing for you
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Samhammer.DependencyInjection:

Package Downloads
Samhammer.DependencyInjection.Override

Resolving Services for .NET Core projects

Samhammer.SocketServer.ApiClient

Authentication client for .NET Core projects

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0 3,968 3/8/2024
6.1.1 5,711 4/12/2023
6.1.0 9,324 1/27/2022
6.0.0 2,976 1/20/2022
3.1.6 812 1/27/2022
3.1.5.2 24,555 10/2/2020
3.1.5.1 443 10/1/2020
3.1.4 669 8/12/2020
3.1.3 545 4/28/2020
3.1.0 1,521 3/18/2020
2.2.1 4,298 9/30/2019
2.2.0 479 9/30/2019
2.1.0 605 6/25/2019
2.0.0 3,306 4/15/2019
1.0.0 579 4/10/2019