Smart.Resolver .NET is simplified resolver library, degradation version of Ninject.
- ASP.NET Core / Generic Host support
- Xamarin support (Code generation mode and Reflection mode both supported)
- 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 named)
- Missing handler supported (For automatic registration, Xamarin integration, open generic type, ...)
- Customization-first implementation, but not too late (see benchmark)
public interface IService
{
}
public class Service : IService
{
}
public 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>();
Package | Note |
---|---|
Core libyrary | |
Microsoft.Extensions.DependencyInjection integration | |
Xamarin DependencyService integration |
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
- Named
- WithConstructorArgument
- WithPropertyValue
- WithMetadata
Supported scope.
- New instance created each time
- Lifecycle is not managed by resolver
config.Bind<TransientObject>().ToSelf().InTransientScope();
or
config.Bind<TransientObject>().ToSelf();
- Single instance created and same instance returned
- Lifecycle managed by resolver (IScopeStorage) and Dispose called when resolver disposed
config.Bind<SingletonObject>().ToSelf().InSingletonScope();
- 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();
- You can create a custom scope
config.Bind<CustomeScopeObject>().ToSelf().InScope(new CustomeScope());
Prepared by standard.
Naming constraint for lookup binding.
public class Child
{
}
public class Parent
{
pulbic Child Child { get; }
public Parent([Named("foo")] Child child)
{
Child = child;
}
}
// Usage
var config = new ResolverConfig();
config.Bind<Child>().ToSelf().InSingletonScope().Named("foo");
config.Bind<Child>().ToSelf().InSingletonScope().Named("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);
Mark of property injection target or select constructor.
public class HasPropertyObject
{
[Inject]
public Target Target { get; set; }
}
Set constructor argument or property value.
public class Sceduler
{
public Sceduler(ITimer timer, int timeout)
{
}
}
// Usage
config.Bind<ITimer>().To<Timer>().InSingletonScope();
config.Bind<Sceduler>().ToSelf().InSingletonScope().WithConstructorArgument("timeout", 30);
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());
See the sample project for details.
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 class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void ConfigureContainer(ResolverConfig config)
{
// Add component
}
...
}
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
}
}
var config = new ResolverConfig()
.UseDependencyService();
var resolver = config.ToResolver();
Ohter topics.
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);
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 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();
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19042
AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores
.NET Core SDK=5.0.101
[Host] : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT
MediumRun : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT
Job=MediumRun IterationCount=15 LaunchCount=2
WarmupCount=10
Method | Mean | Error | StdDev | Median | Min | Allocated |
---|---|---|---|---|---|---|
Singleton | 3.895 ns | 0.0105 ns | 0.0153 ns | 3.893 ns | 3.865 ns | - |
Transient | 16.482 ns | 0.0389 ns | 0.0571 ns | 16.471 ns | 16.378 ns | 24 B |
Combined | 21.625 ns | 0.2003 ns | 0.2998 ns | 21.602 ns | 21.021 ns | 24 B |
Complex | 41.882 ns | 0.2201 ns | 0.3013 ns | 41.863 ns | 41.361 ns | 136 B |
Generics | 4.847 ns | 0.0169 ns | 0.0242 ns | 4.844 ns | 4.799 ns | 24 B |
MultipleSingleton | 3.043 ns | 0.0308 ns | 0.0432 ns | 3.067 ns | 2.991 ns | - |
MultipleTransient | 91.884 ns | 0.5413 ns | 0.7934 ns | 91.724 ns | 90.466 ns | 184 B |
AspNet | 124.819 ns | 0.8491 ns | 1.2178 ns | 125.113 ns | 122.766 ns | 256 B |
- AOP( ゚д゚)、ペッ
- Method Injection (I don't need but it is possible to cope)
- Circular reference detection (Your design bug)