Skip to content

usausa/Smart-Net-Resolver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

604 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smart.Resolver .NET - resolver library for .NET

NuGet

What is this?

Smart.Resolver .NET is simplified resolver library, degradation version of Ninject.

  • ASP.NET Core / Generic Host support
  • Transient, Singleton, Container(child) and custom scope supported
  • Callback, Constant provider supported
  • Property injection supported (optional)
  • Custom initialize processor supported
  • Construct with parameter supported
  • Constraint supported (like keyed)
  • Missing handler supported (For automatic registration, open generic type, ...)
  • Customization-first implementation, but not too late (see benchmark)

Usage example

public interface IService
{
}

public sealed class Service : IService
{
}

public sealed class Controller
{
    private IService Service { get; }

    public Controller(IService service)
    {
        Service = service;
    }
}

// Usage 
var config = new ResolverConfig();
config.Bind<IService>().To<Service>().InSingletonScope();
config.Bind<Controller>().ToSelf();

var resolver = config.ToResolver();

var controller = resolver.Get<Controller>();

NuGet

Package Note
NuGet Badge Core libyrary
NuGet Badge Microsoft.Extensions.DependencyInjection integration
NuGet Badge Configuration extension

Bindings

Supported binding syntax.

  • Bind
  • To
// Type IService to Service Type instance
config.Bind<IService>().To<Service>();
  • ToSelf
// Type Controller to Controller Type instance
config.Bind<Controller>().ToSelf();
  • ToMethod
// Type IScheduler to factory method
config.Bind<IScheduler>().ToMethod(x => x.Get<ISchedulerFactory>().GetScheduler());
  • ToConstant
// Type Messenger to instance
config.Bind<Messenger>().ToConstant(Messenger.Default);
  • InTransientScope
  • InSingletonScope
  • InScope
  • Keyed
  • WithConstructorArgument
  • WithPropertyValue
  • WithMetadata

Scope

Supported scope.

Transient (default)

  • New instance created each time
  • Lifecycle is not managed by resolver
config.Bind<TransientObject>().ToSelf().InTransientScope();

or

config.Bind<TransientObject>().ToSelf();

Singleton

  • Single instance created and same instance returned
  • Lifecycle managed by resolver (IScopeStorage) and Dispose called when resolver disposed
config.Bind<SingletonObject>().ToSelf().InSingletonScope();

Container

  • Single instance created and same instance returned per child container
  • Lifecycle managed by child container and Dispose called when resolver disposed
config.Bind<ScopeObject>().ToSelf().InContainerScope();

Custom

  • You can create a custom scope
config.Bind<CustomeScopeObject>().ToSelf().InScope(new CustomeScope());

Attribute

Prepared by standard.

ResolveByAttribute

Key constraint for lookup binding.

public sealed class Child
{
}

public sealed class Parent
{
    pulbic Child Child { get; }

    public Parent([ResolveBy("foo")] Child child)
    {
        Child = child;
    }
}

// Usage
var config = new ResolverConfig();
config.Bind<Child>().ToSelf().InSingletonScope().Keyed("foo");
config.Bind<Child>().ToSelf().InSingletonScope().Keyed("bar");
config.Bind<Parent>().ToSelf();

var resolver = config.ToResolver();

var parent = resolver.Get<Parent>();
var foo = resolver.Get<Child>("foo");
var bar = resolver.Get<Child>("bar");

Debug.Assert(parent.Child == foo);
Debug.Assert(parent.Child != bar);

InjectAttribute

Mark of property injection target or select constructor.

public sealed class HasPropertyObject
{
    [Inject]
    public Target Target { get; set; }
}

Parameter

Set constructor argument or property value.

public sealed class Sceduler
{
    public Sceduler(ITimer timer, int timeout)
    {
    }
}

// Usage
config.Bind<ITimer>().To<Timer>().InSingletonScope();
config.Bind<Sceduler>().ToSelf().InSingletonScope().WithConstructorArgument("timeout", 30);

Configuration

StandardResolver is constructed from sub-components. Change the sub-components in ResolverConfig, can be customized StandardResolver.

// Add custom processor to pipeline
public sealed class CustomInitializeProcessor : IProcessor
{
    public void Initialize(object instance)
    {
...
    }
}

config.UseProcessor<CustomInitializeProcessor>();
// Add custome scope
public sealed class CustomScope : IScope
{
    private static readonly ThreadLocal<Dictionary<IBinding, object>> Cache =
        new ThreadLocal<Dictionary<IBinding, object>>(() => new Dictionary<IBinding, object>());

    public IScope Copy(IComponentContainer components)
    {
        return this;
    }

    public Func<IResolver, object> Create(IBinding binding, Func<object> factory)
    {
        return resolver =>
        {
            if (Cache.Value.TryGetValue(binding, out var value))
            {
                return value;
            }

            value = factory();
            Cache.Value[binding] = value;

            return value;
        };
    }
}

config.Components.Add<CustomScopeStorage>();
config.Bind<SimpleObject>().ToSelf().InScope(new CustomScope());

Integration

See the sample project for details.

ASP.NET Core 3.1

public static class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseServiceProviderFactory(new SmartServiceProviderFactory())
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
public sealed class Startup
{
...
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    public void ConfigureContainer(ResolverConfig config)
    {
        // Add component
    }
...
}

Generic Host

public static class Program
{
    public static async Task Main(string[] args)
    {
        await new HostBuilder()
            .UseServiceProviderFactory(new SmartServiceProviderFactory())
            .ConfigureContainer<ResolverConfig>(ConfigureContainer)
            .RunAsync();
    }

    private static void ConfigureContainer(ResolverConfig config)
    {
        // Add component
    }
}

Other

Ohter topics.

IInitializable

If the class implements Initializable, Initialized called after construct.

protected class InitializableObject : IInitializable
{
    public bool Initialized { get; private set; }

    public void Initialize()
    {
        Initialized = true;
    }
}

// Usage
config.Bind<InitializableObject>().ToSelf().InSingletonScope();

var obj = resolver.Get<InitializableObject>();

Debug.Assert(obj.Initialized);

Constraint

If custom constraints want is as follows:

// Create IConstraint implement
public sealed class HasMetadataConstraint : IConstraint
{
    public string Key { get; }

    public HasMetadataConstraint(string key)
    {
        Key = key;
    }

    public bool Match(IBindingMetadata metadata)
    {
        return metadata.Has(Key);
    }
}

// Create ConstraintAttribute derived class
public sealed class HasMetadataAttribute : ConstraintAttribute
{
    public string Key { get; }

    public HasMetadataAttribute(string key)
    {
        Key = key;
    }

    public override IConstraint CreateConstraint()
    {
        return new HasMetadataConstraint(Key);
    }
}

// Usage
public sealed class Parent
{
    pulbic Child Child { get; }

    public Parent([HasMetadata("hoge")] Child child)
    {
        Child = child;
    }
}

config.Bind<Child>().ToSelf().InSingletonScope();
config.Bind<Child>().ToSelf().InSingletonScope().WithMetadata("hoge", null);
config.Bind<Parent>().ToSelf();

Benchmark (for reference purpose only)

BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8246/25H2/2025Update/HudsonValley2)
AMD Ryzen 9 5900X 3.70GHz, 1 CPU, 24 logical and 12 physical cores
.NET SDK 10.0.202
  [Host]    : .NET 10.0.6 (10.0.6, 10.0.626.17701), X64 RyuJIT x86-64-v3
  MediumRun : .NET 10.0.6 (10.0.6, 10.0.626.17701), X64 RyuJIT x86-64-v3

Job=MediumRun  Jit=RyuJit  Platform=X64  
IterationCount=15  LaunchCount=2  WarmupCount=10  
Method Mean Error StdDev Min Max P90 Gen0 Allocated
Singleton 2.121 ns 0.0306 ns 0.0458 ns 2.061 ns 2.262 ns 2.180 ns - -
Transient 12.752 ns 0.9961 ns 1.4600 ns 11.022 ns 14.951 ns 14.481 ns 0.0014 24 B
Combined 21.474 ns 0.2398 ns 0.3588 ns 20.795 ns 22.366 ns 21.935 ns 0.0014 24 B
Complex 33.642 ns 0.3751 ns 0.5380 ns 32.895 ns 34.699 ns 34.433 ns 0.0081 136 B
Generics 3.819 ns 0.0658 ns 0.0985 ns 3.699 ns 4.132 ns 3.926 ns 0.0014 24 B
MultipleSingleton 2.145 ns 0.0396 ns 0.0593 ns 2.067 ns 2.280 ns 2.212 ns - -
MultipleTransient 38.031 ns 1.5301 ns 2.2901 ns 35.366 ns 45.711 ns 40.442 ns 0.0110 184 B
AspNet 80.771 ns 0.8595 ns 1.2864 ns 78.424 ns 82.946 ns 82.537 ns 0.0153 256 B

Unsupported

  • AOP( ゚д゚)、ペッ
  • Method Injection (I don't need but it is possible to cope)
  • Circular reference detection (Your design bug)

Packages

 
 
 

Contributors

Languages