AssemblyInject 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AssemblyInject --version 1.0.0
NuGet\Install-Package AssemblyInject -Version 1.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="AssemblyInject" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AssemblyInject --version 1.0.0
#r "nuget: AssemblyInject, 1.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 AssemblyInject as a Cake Addin
#addin nuget:?package=AssemblyInject&version=1.0.0

// Install AssemblyInject as a Cake Tool
#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

Wiki Home

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 netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 181 6/24/2023
2.1.1 614 6/6/2021
2.1.0 272 5/15/2021
2.0.0 476 11/7/2020
1.0.6 400 8/29/2020
1.0.4 430 6/11/2020
1.0.3 419 3/8/2020
1.0.2 430 2/14/2020
1.0.0 482 2/8/2020