Giter VIP home page Giter VIP logo

faster.ioc.zero's Introduction

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

faster.ioc.zero's People

Contributors

wsm2110 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.