百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

MicroResolver

> 编程语言
开源

极其快速的依赖注入库。

185 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

极其快速的依赖注入库。

MicroResolver

Extremely Fast Dependency Injection Library.

Features

MicroResolver is desgined for performance. I've released two fastest serializers ZeroFormatter and MessagePack for C#, this library is using there dynamic il code generation technique.

  • Dynamic IL Inlining
  • Generic Type Caching per resolver
  • Fast NonGeneric lookup table

MicroResolver achived fastest at some tests in IoCPerformance benchmark - Singleton(Multithread), Combined(Multithread), Property(Singlethread, Multithread) and other result also top level. This benchmark is nongeneric test(use (object Resove(Type type)), MicroResolver is focused to optimize for generic method(T Resolve()), if using it, faster than other libraries.

Support Features - Consturctor Injection, Field Injection, Property Injection, Method Injection, Collection resolver and Three lifetime support(Singleton, Transient and Scoped).

Quick Start

Install from NuGet(for .NET Framework 4.6, .NET Standard 1.4)

  • Install-Package MicroResolver
csharp
// Create a new container
var resolver = ObjectResolver.Create();

// Register interface->type map, default is transient(instantiate every request)
resolver.Register();

// You can configure lifestyle - Transient, Singleton or Scoped
resolver.Register(Lifestyle.Singleton);

// Compile and Verify container(this is required step)
resolver.Compile();

// Get instance from container
var userRepository = resolver.Resolve();
var logger = resolver.Resolve();

Notice: MicroResolver requests call Compile before use container.

InjectionAttribute and Resolve Collection

MicroResolver can resolve all public and private properties, fields, constructor and methods. Inject target have to mark [Inject] attribute.

…

Inject order is Constructor -> Field -> Property -> Method.

If register many types per type, you can use RegisterCollection and Resolve>.

csharp
// Register type -> many types
resolver.RegisterCollection(typeof(T1), typeof(T2), typeof(T3));

resolver.Compile();

// can resolve by IEnumerbale or T[] or IReadOnlyList.
resolver.Resolve>();
resolver.Resolve();
resolver.Resolve>();

// can resolve other type's inject target.
public class AnotherType
{
    public AnotherType(IMyType[] targets)
    {
    }
}

Scoped

Lifetime.Scoped is usually the same as Transient but within BeginScope it behaves like a singleton in the scope.

…

ScopeProvider has three option in default. ScopeProvider.Standard, ScopeProvider.ThreadLocal and ScopeProvider.AsyncLocal. If needs custom scope, you can create own ScopeProvider.

csharp
public class MyScopeProvider : ScopeProvider
{
    public override void Initialize(IObjectResolver resolver)
    {
        // when called from BeginScope().
    }

    protected override object GetValueFromScoped(Type type, out bool isFirstCreated)
    {
        // called per Resolve.
    }
}

Performance Technique - Dynamic IL Inlining

Everyone creates dynamic code generation for optimize performance. But if target is complex type?

…

The following way is not slow, but it is not fastest.

csharp
// This is `slow` example of complex type resolve
static IComplexType ResolveComplexType(IObjectResolver resolver)
{
    var a = resolver.Resolve();
    var b = resolver.Resolve();

    var result = new ComplexType(a);
    result.MyProperty = b;
    result.Initialize();

    return result;
}

MicroResolve choose inlining code generation, all dependencies are analyzed and inlined at compile time.

csharp
// This is actual code generation of MicroResolver, all dependency is inlined at il code generation
static IComplexType ResolveComposite()
{
    var a = new ForConstructorInjection();
    a.MyField = new ForFieldInjection();
    var b = new ForPropertyInjection();
    b.OnCreate();

    var result = new ComplexType(a);
    result.MyProperty = b;
    result.Initialize();

    return result;
}

Performance Technique - Generic Type Caching per resolver

The generated code is cached. And how to retrieve it? ConcurrentDictionary? Dictionary? They are slow. MicroResolve choose generic type caching.

This is ObjectResolver signature.

csharp
public abstract class ObjectResolver
{
    public abstract T Resolve();
}

If called ObjectResolver.Create, generate dynamic inherited type.

csharp
public class ObjectResolver_Generated : ObjectResolver
{
    public override T Resolve()
    {
        // too simple, of course simple is fastest.
        return Cache.factory();
    }

    Cache
    {
        // generated Func code is see 'Dynamic IL Inlining' section.
        public Func factory;
    }
}

The code path is too short, it means no overhead.

But generated container can not remove. This is a design constraint.

Performance Technique - Fast NonGeneric lookup table

Type Caching is require to use generics method. But often framework requests nongeneric type.

csharp
// fastest
resolver.Resolve();

// slower but framework requests this method
(T)resolver.Resolve(type);

MicroResolver use fast type lookup by own fixed hashtable.

…

non-generic lookup is slower than generic but still fast.

Author Info

Yoshifumi Kawai(a.k.a. neuecc) is a software developer in Japan.
He is the Director/CTO at Grani, Inc.
Grani is a mobile game developer company in Japan and well known for using C#.
He is awarding Microsoft MVP for Visual C# since 2011.
He is known as the creator of UniRx(Reactive Extensions for Unity)

Blog: https://medium.com/@neuecc (English)
Blog: http://neue.cc/ (Japanese)
Twitter: https://twitter.com/neuecc (Japanese)

License

This library is under the MIT License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#csharpdependency-injectionioc-containerservice-locator

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言