AssemblyInject 1.0.0
See the version list below for details.
dotnet add package AssemblyInject --version 1.0.0
NuGet\Install-Package AssemblyInject -Version 1.0.0
<PackageReference Include="AssemblyInject" Version="1.0.0" />
<PackageVersion Include="AssemblyInject" Version="1.0.0" />
<PackageReference Include="AssemblyInject" />
paket add AssemblyInject --version 1.0.0
#r "nuget: AssemblyInject, 1.0.0"
#addin nuget:?package=AssemblyInject&version=1.0.0
#tool nuget:?package=AssemblyInject&version=1.0.0
What is it?
AutoInject is an extension to the .Net Core Dependency Injection framework designed for quick and easy setup. Instead of requiring an exact specification of which type needs to be injected for which service, AutoInject scans the specified assemblies and injects all services it can find.
Why use it?
AutoInject automatically resolves dependencies for the most common use cases. This reduces the amount of development required to introduce and maintain dependency injection in your application.
Example:
Let's say we have a class 'SomeInterfaceImplementation' which implements 'ISomeInterface'.
public interface ISomeInterface
{
void DoSomething();
}
public class SomeInterfaceImplementation
{
public void DoSomething()
{
Console.WriteLine("Something was done.");
}
}
And we have another class that requires 'ISomeInterface' during construction.
public class ARandomClass
{
private ISomeInterface someInterface;
public ARandomClass(ISomeInterface someInterface)
{
this.someInterface = someInterface;
}
public void DoSomething()
{
someInterface.DoSomething();
}
}
It's pretty obvious that if I'm resolving 'ARandomClass', the system should inject an instance of 'SomeInterfaceImplementation' in the constructor. However if you want this injection to happen, you will have to explicitly state this in service collection configuration. Case in point:
public class Program
{
public static void Main()
{
// create service provider
var serviceCollection= new ServiceCollection();
serviceCollection.AddTransient<ISomeInterface, SomeInterfaceImplementation>();
serviceCollection.AddTransient<ARandomClass>();
var serviceProvider = serviceCollection.BuildServiceProvider();
// resolve service
var aRandomClass = serviceProvider.GetService<ARandomClass>();
// run code
aRandomClass.DoSomething();
}
}
In fact, as you can see, you will even need to specify that 'ARandomClass' is a service that can be resolved using itself as the implementation.
There are advantages to requiring an explicit setup like this:
- It requires your attention and prevents any unexpected behaviour from occurring.
The downsides are:
- It adds another step to your development process for every service you create
- The information about your services is separate from your actual classes
- You will need to add some organization once the list service definitions becomes too long
AutoInject removes the need to explicitly state every individual dependency separately. Instead you just need to specify which assembly contains your services, and it will configure the ServiceCollection for you.
public class Program
{
public static void Main()
{
// create service provider
var serviceCollection = new ServiceCollection();
serviceCollection.AutoInject(Assembly.GetExecutingAssembly());
var serviceProvider = serviceCollection.BuildServiceProvider();
// resolve service
var aRandomClass = serviceProvider.GetService<ARandomClass>();
// run code
aRandomClass.DoSomething();
}
}
More information
Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp3.0 is compatible. netcoreapp3.1 was computed. |
-
.NETCoreApp 3.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.