Faster.Ioc 5.0.0

dotnet add package Faster.Ioc --version 5.0.0                
NuGet\Install-Package Faster.Ioc -Version 5.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="Faster.Ioc" Version="5.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Faster.Ioc --version 5.0.0                
#r "nuget: Faster.Ioc, 5.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 Faster.Ioc as a Cake Addin
#addin nuget:?package=Faster.Ioc&version=5.0.0

// Install Faster.Ioc as a Cake Tool
#tool nuget:?package=Faster.Ioc&version=5.0.0                

Faster.Ioc - fastest ioc container

Faster.Ioc is an IoC container for Microsoft .NET. It manages dependencies between classes so that applications stay easy to change as they grow in size and complexity.

About

Faster.Ioc is an incredibly fast Ioc container with endless possibilities. Faster.ioc supports the following:

  • Scoped lifetimes (Singleton, Transient, Scoped)
  • Multi registrations, no more pesky factories...
  • Conditional overrides
  • Ienumerable<T>
  • Asp.net core features
  • Open and closed generics
  • Child containers
  • Store registrations by key
  • Retrieve all registrations of the same interface
  • Will always resolve constructor with largest parameters

How to use

  1. Install nuget package Faster.Ioc to your project
dotnet add package Faster.Ioc
  1. Create a Container
var container = new Container();

//add registrations with different lifetimes
container.Register<IAnimal, Cow>(Lifetime.Singleton);
container.Register<IAnimal, Horse>(Lifetime.Transient);
container.Register<IAnimal, Bull>(Lifetime.Scoped);

// register by key
container.Register<IAnimal, Goose>(Lifetime.Scoped, "Goose")

// will always resolve the first registration of IAnimal, which in this case is a Cow
var cow = container.Resolve<IAnimal>();

// Resolve by key
var goose = container.Resolve("Goose");

//Cow, Horse and Bull
var animals = container.ResolveAll<IAnimal

Advanced

  1. Conditional - overriding a parameter with a different implementation

var container = new Container();

container.Register<IAnimal, Cow>(Lifetime.Singleton);
container.Register<IAnimal, Horse>(Lifetime.Transient);

//Create override of Farm which be default, since it has an IAnimal parameter would resolve a Cow, but now it will resolve a Horse
//Types used in an override need to be registered!
container.RegisterOverride<IFarm, Farm>(() => new Farm(New Horse()));

// will always resolve the first registration of IAnimal, which in this case is a Cow
var cow = container.Resolve<IFarm>();

public class Farm
{
	public Animal{get; set;}

	public Farm(IAnimal animal)
	{
		Animal = animal;
	}
}

  1. Ienumerable<T>
var container = new Container();

container.Register<IAnimal, Cow>(Lifetime.Singleton);
container.Register<IAnimal, Horse>(Lifetime.Singleton);

//Create override of Farm which be default, since it has an IAnimal parameter would resolve a Cow, but now it will resolve a Horse
//Types used in an override need to be registered!
container.Register<IFarm, Farm>();

public class Farm
{
	public IEnumerable<IAnimal> {get; private set;}

	public Farm(IEnumerable<IAnimal> animals)
	{
		Animals = animals;
	}
}
  1. OpenGenerics<T>
var container = new Container();

container.Register<>(typeof(IGeneric<>, Generic<>)Lifetime.Singleton);
container.Register<IFarm, Farm>();

public class Farm
{
	public Generic<IAnimal> { get; private set;}

	public Farm(IGeneric<IAnimal> animals)
	{
		Animals = animals;
	}
}

  1. Closed Generics<fixed>
var container = new Container();

container.Register<>(typeof(IGeneric<int>, Generic<int>)Lifetime.Singleton);
container.Register<IFarm, Farm>();

public class Farm
{
	public Generic<int> { get; private set;}

	public Farm(IGeneric<int> animals)
	{
		Animals = animals;
	}
}

  1. Scoped Lifetime
  Container container = new Container();

  container.Register<IDisposeableOne>(() => new DisposeableOne(), Lifetime.Scoped);

  using (var factory = container.CreateScope())
  {
     //Act
      var instance = factory.ServiceProvider.GetService(typeof(IDisposeableOne));
      var instance2 = factory.ServiceProvider.GetService(typeof(IDisposeableOne));

      //Assert
      Assert.IsTrue(instance == instance2);
      }

Disposable

The lifetime of objects implementing IDisposable interface is something to keep in mind. The Container will keep track of all objects implementing IDisposable (Singletons and transient). By default Faster.Map will dispose all objects at shutdown or when .Dispose() is called. This is also when the memory is released by the garbage collector, all references stored in the Container will be gone. If this is troublesome you can either use a Scoped lifetimes or a ChildContainer

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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 (1)

Showing the top 1 popular GitHub repositories that depend on Faster.Ioc:

Repository Stars
danielpalme/IocPerformance
Performance comparison of .NET IoC containers
Version Downloads Last updated
5.0.0 288 11/1/2023
1.0.0.4 444 12/9/2022
1.0.0.3 320 12/8/2022
1.0.0 583 10/7/2022

- Move to .net80