All Projects → byme8 → ZeroIoC

byme8 / ZeroIoC

Licence: MIT license
ZeroIoC is reflectionless IoC Container for .NET

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to ZeroIoC

ThunderboltIoc
One of the very first IoC frameworks for .Net that has no reflection. An IoC that casts its services before thunder casts its bolts.
Stars: ✭ 40 (+81.82%)
Mutual labels:  ioc, source-generation
DIONS
IOC is a privacy based Proof of Stake Blockchain platform for Smart Contracts, multi level dns identities, AES 256 encrypted data communications, storage and payments.
Stars: ✭ 36 (+63.64%)
Mutual labels:  ioc
ioc-ts
Inversion of control container on TypeScript
Stars: ✭ 14 (-36.36%)
Mutual labels:  ioc
kekiri
A .NET framework that supports writing low-ceremony BDD tests using Gherkin language
Stars: ✭ 19 (-13.64%)
Mutual labels:  ioc
Asp.net MVC5 DDD EF6 IoC
Asp.net C# MVC5, EF6, DDD, IoC
Stars: ✭ 14 (-36.36%)
Mutual labels:  ioc
XPlatUtils
A set of helpers for cross platform apps. Right now has a ServiceContainer and Messenger implementation.
Stars: ✭ 37 (+68.18%)
Mutual labels:  ioc
iocgo
A lightweight Inversion of Control (IoC) (Dependency Injection) container for Golang
Stars: ✭ 36 (+63.64%)
Mutual labels:  ioc
inject
[Archived] See https://github.com/goava/di.
Stars: ✭ 49 (+122.73%)
Mutual labels:  ioc
Mimir
OSINT Threat Intel Interface - CLI for HoneyDB
Stars: ✭ 104 (+372.73%)
Mutual labels:  ioc
firmeve
a out-of-the-box, full-featured go framework supporting http, http2, websocket, tcp, udp, rpc and microservice
Stars: ✭ 36 (+63.64%)
Mutual labels:  ioc
Caliburn.Light
The magic-free Caliburn.Light, a powerful framework designed for building applications across current XAML platforms.
Stars: ✭ 64 (+190.91%)
Mutual labels:  ioc
thinking-in-spring
Spring source code reading
Stars: ✭ 105 (+377.27%)
Mutual labels:  ioc
coronavirus-covid-19-SARS-CoV-2-IoCs
All the IOC's I have gathered which are used directly involved coronavirus / covid-19 / SARS-CoV-2 cyber attack campaigns
Stars: ✭ 67 (+204.55%)
Mutual labels:  ioc
containerx
⚡简单的Java依赖注入框架,代码量少,实现了依赖注入和AOP。适合Spring源码的初学者掌握其核心原理
Stars: ✭ 76 (+245.45%)
Mutual labels:  ioc
vue3-oop
使用类和依赖注入写vue组件
Stars: ✭ 90 (+309.09%)
Mutual labels:  ioc
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (+0%)
Mutual labels:  ioc
Reflex
Minimal dependency injection framework for Unity
Stars: ✭ 263 (+1095.45%)
Mutual labels:  ioc
UdonRabbit.Analyzer
DEPRECATED (U#1.0 not supported) .NET Roslyn Analyzer for VRChat Udon and UdonSharp.
Stars: ✭ 44 (+100%)
Mutual labels:  roslyn-analyzer
di
🛠 A full-featured dependency injection container for go programming language.
Stars: ✭ 156 (+609.09%)
Mutual labels:  ioc
dioc
A dart simple dependency container based on code generation.
Stars: ✭ 47 (+113.64%)
Mutual labels:  ioc

The main goal of the ZeroIoC is to provide IoC for AOT platforms such as Xamarin, Unity, and Native AOT. It is powered by Roslyn Source Generator as a result executed on build and doesn't require Reflection.Emit to function.

Get Started

  1. Install nuget package ZeroIoC to your project.
dotnet add package ZeroIoC
  1. Declare your container that is inherited from ZeroIoCContainer as a partial class
    public interface IUserService
    {
    }

    public class UserService : IUserService
    {
        public Guid Id { get; } = Guid.NewGuid();

        public UserService(Helper helper)
        {
        }
    }

    public class Helper
    {
        public Guid Id { get; } = Guid.NewGuid();
    }

    public partial class Container : ZeroIoCContainer
    {
        protected override void Bootstrap(IZeroIoCContainerBootstrapper bootstrapper)
        {
            bootstrapper.AddSingleton<Helper>();
            bootstrapper.AddTransient<IUserService, UserService>();
        }
    }
  1. Use your container:
  var container = new Container();
  var userService = container.Resolve<IUserService>();
  1. You can override a constructor argument or some dependency:
  var container = new Container();
  var userService = container.Resolve<IUserService>(
    Overrides.Create()
        .Constructor(("helper", new Helper())));
  // ...
  var userService = container.Resolve<IUserService>(
    Overrides.Create()
        .Dependency<Helper>(() => new Helper()));

Features

I would say it is in the MVP stage. Under MVP, I mean that the set of features is big enough to be helpful in real projects. This set contains:

  • Multiple IoC containers can be active at the same time.
  • Support for the singleton, scoped, and transient lifetimes => basic things that cover 99% of all needs.
  • Support for overrides, you can override a constructor argument or some dependency for one call => usefull in some advanced scenarios and during testing.
  • Powered by source generation to avoid reflection and Reflection.Emit => you can use it inside the AOT Xamarin/Unity app.
  • Fast enough, with minimal overhead => the end-user of the Xamarin app will not notice a difference.

How it works

The NuGet is deployed with the source generator and analyzer. Then it looks for class declarations that are inherited from the ZeroIoCContainer. Inside the generator looks for the ZeroIoCContainer.Bootstrap method. Based on its content, the source generator will generate another part of a partial class. For the case described above, it will look like that(skipping the performance magic):

public partial class Container
{

    public Container()
    {
        Resolvers = Resolvers.AddOrUpdate(typeof(global::Helper), new SingletonResolver(static resolver => new global::Helper()));
        Resolvers = Resolvers.AddOrUpdate(typeof(global::IUserService), new TransientResolver(static resolver => new global::UserService(resolver.Resolve<global::Helper>())));
    }

    protected Container(ImTools.ImHashMap<Type, InstanceResolver> resolvers, ImTools.ImHashMap<Type, InstanceResolver> scopedResolvers, bool scope = false)
        : base(resolvers, scopedResolvers, scope)
    {
    }

    public override IZeroIoCResolver CreateScope()
    {
        var newScope = ScopedResolvers
            .Enumerate()
            .Aggregate(ImHashMap<Type, InstanceResolver>.Empty, (acc, o) => acc.AddOrUpdate(o.Key, o.Value.Duplicate()));
        
        return new Container(Resolvers, newScope, true);
    }
}

It is pretty simple stuff. The logic is based on a dictionary with Type as a key and instance resolver as a value. Such a class is generated for each separate class declaration, and because there is no static logic, you can safely define as many containers as you like.

Limitations

Let's talk about the ZeroIoCContainer.Bootstrap method. It is not an ordinary method. It is a magic one. It allows you to define the relations between interface and implementation, but the .net will never execute it at the runtime. The ZeroIoCContainer.Bootstrap is just a declaration that will be parsed by source generation, and based on it, the mapping will be generated. It means that there is no point to use statements like that:

 public partial class Container : ZeroIoCContainer
    {
        protected override void Bootstrap(IZeroIoCContainerBootstrapper bootstrapper)
        {
            if(Config.Release)
            {
              bootstrapper.AddSingleton<IHelper, ReleaseHelper>();
            }
            else 
            {
              bootstrapper.AddSingleton<IHelper, DebugHelper>();
            }
            
            bootstrapper.AddTransient<IUserService, UserService>();
        }
    }

All of them will be just ignored. To prevent the bunch of WTF situations(and introduce a new one), I added a special analyzer that will warn you about it if you forget.

But If you want to do something at runtime, you can do it like that:

var container = new Container();
if(Config.Release)
{
    container.AddInstance<IHelper>(new ReleaseHelper());
}
else 
{
    container.AddInstance<IHelper>(new DebugHelper());
}

var userService = container.Resolve<IUserService>();

Such an approach doesn't use any reflection underhood and can be safely used inside the AOT environment.

Plans

  • Performance improvements(it is already fast but can be better)
  • Improve extensibility
  • Create separate easy-to-use bootstrap nugets for common runtimes like Asp.Net Core, Xamarin, Unity3D.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].