Skip to content

The goal of Faster.Ioc.Zero is to provide the fastest ioc container with zero overhead.

License

Notifications You must be signed in to change notification settings

Wsm2110/Faster.Ioc.Zero

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Faster.Ioc.Zero - fastest ioc container

The goal of Faster.Ioc.Zero is to provide the fastest ioc container with zero overhead.

Faster.Ioc.Zero uses the following:

  • SourceGenerators
  • Static code injection
  • Transient and Singleton lifetimes

About

Faster.Ioc.Zero is a small ioc container with zero overhead and incredibly fast runtime speed. See benchmarks, or try it out yourself. Faster.Ioc.Zero evolved from the fact that almost all ioc containers have some major downsize which is wiring all registration together and everything is basically a lookup in a custom hashmap or dictionary. This creates an unpleasant overhead we dont want. Faster.Ioc.Zero uses the newly added sourcegenerators implemented in .net5 and allows us to generate code ahead-Of-Time.

How to use

  1. Install nuget package Faster.Ioc.Zero to your project
dotnet add package Faster.Ioc.Zero
  1. Create a ContainerBuilder which is inherited from IContainerBuilder. It's possible to have more than one ContainerBuilder in your project
    public interface ITransient{}
	
    public interface ISingleton{}

    public class TransientOne : ITransient
    { 
        public TransientOne(ISingleton singleton){}
    }
	
	public class SingletonOne: ISingleton
	{	
		public SingletonOne(){}
	}
	
    public class ContainerBuilder : IContainerBuilder
    {
       public void Bootstrap(Builder builder)
       {
	   builder.Register<ITransient, TransientOne>();
	   builder.Register<ISingleton, SingletonOne>(Lifetime.Singleton);
       }
    }
  1. Compile your project which will generate all the needed registrations in a static class. Generated code is located at Dependencies->Analyzers->Faster.Ioc.Zero.SourceGenerator
  
  var transient = Container.TransientOne;
  var singleton = Container.SingletonOne;

Advanced

By default Faster.Ioc.Zero will create an IEnumerable<> for each registration

   public class ContainerBuilder : IContainerBuilder
    {
       public void Bootstrap(Builder builder)
       {
	      builder.Register<ISingleton, SingletonOne>(Lifetime.Singleton);
	      builder.Register<ISingleton, SingletonTwo>(Lifetime.Singleton);
              builder.Register<ISingleton, SingletonThree>(Lifetime.Singleton);
       }
    }

	var singletons = Container.GetAllISingleton();
    
  1. Supports params of type IEnumerable<>, ICollections and Arrays
  2. Override param while multiple concrete classes are registered. By default when multiple concrete classes are active and no override is given. the first registered concrete class is used. The
    public interface ITransient{}
	
	public interface ISingleton{}

    public class TransientOne : ITransient
    { 
        public TransientOne(ISingleton singleton){}
    }
	
	public class SingletonOne: ISingleton
	{	
		public SingletonOne(){}
	}

   public class ContainerBuilder : IContainerBuilder
    {
       public void Bootstrap(Builder builder)
       {
	      // override registration with a custom implementation using a different concrete type
	   builder.Register<ITransient, TransientOne>(() => new TransientOne(new SingletonTwo()) );
	   builder.Register<ISingleton, SingletonOne>(Lifetime.Singleton);
           builder.Register<ISingleton, SingletonTwo>(Lifetime.Singleton);
           builder.Register<ISingleton, SingletonThree>(Lifetime.Singleton);
       }
    }

	var transientWithSingletonTwo = Container.TransientOne();
    
  1. the constructor with the largest param count is always resolved unless the resolver is overridden

Benchmark

According to the benchmark https://github.com/danielpalme/IocPerformance/blob/main/README.md grace is one of the faster ioc frameworks.

Each Benchmark resolves 1500000 objects

Basic Features

Container Singleton Transient Combined Complex Generics IEnumerable
Faster.Ioc.Zero 1
1
0
0
11
11
33
33
10
10
Grace 7.2.1 20
31
39
55
52
84
73
83
50
80
250
210

Prepare

Container Prepare And Register Prepare And Register And Simple Resolve
Faser.Ioc.Zero 0
0
Grace 7.2.1 157
966

About

The goal of Faster.Ioc.Zero is to provide the fastest ioc container with zero overhead.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published