A micro dependency injection framework for .net C#.
MIT License
var container = new Container();
container.Bind<ISomeType, SomeType>();
var instance = container.Resolve<ISomeType>();
You can pass additional arguments to the constructor via ResolveWith.
public class SomeType : ISomeType
{
public string Text { get; set; }
SomeType(string text) { Text = text; }
}
var container = new Container();
container.Bind<ISomeType, SomeType>();
// no tag supplied `tag`...
var instance = container.ResolveWith<ISomeType>(null, "Hello World");
Assert.AreEqual(instance.Text, "Hello World");
Binding multiple variations of the same key type with tags.
var container = new Container();
container.Bind<ISomeType, SomeTypeA>().WithTag("a");
container.Bind<ISomeType, SomeTypeB>().WithTag("b");
var instanceA = container.Resolve<ISomeType>("a");
var instanceB = container.ResolveWith<ISomeType>("b", "Hello World");
You can bind a type as a 'singleton' so that it will persist.
var container = new Container();
container.Bind<ISomeType, SomeType>().AsSingleton();
var instance1 = container.Resolve<ISomeType>();
var instance2 = container.Resolve<ISomeType>();
Assert.AreEqual(instance1, instance2);
If you can't inject via a constructor, use [Inject] attributes to inject into Fields or Properties.
class SomeType : ISomeType
{
[Inject] private IOtherType _otherType;
}
Use WithFactory to pass a factory delegate to be used when creating a new instance.
var container = new Container();
container.Bind<ISomeType, SomeType>().WithFactory(delegate(IContainer container, IBinding binding, object[] args)
{
return new SomeType();
});
var instanceA = container.Resolve<ISomeType>();
var instanceB = container.ResolveWith<ISomeType>(null, "Hello World");