Welcome to the build wiki!
Prerequisites:
- .NET Core 3 SDK, 3.0.100-preview-010184 or later
- VS2019 Preview 3 or later
Build.DependencyInjection.Abstractions(dependency_injection_build_abstractions):
- Added support for .NET Core 3 preview 2 (3.0.100-preview-010184)
- Removed support for .NET Standart 1.5/1.6
- Removed support for .NET Core 1.0/1.1
Build.DependencyInjection(dependency_injection_build)
- Added support for .NET Core 3 preview 2 (3.0.100-preview-010184)
- Added support for .NET Core 2.0
- Added Build.DependencyInjection.Abstractions (v1.0.0.25)
Build.DependencyInjection.Abstractions:
- Added support for .NET Core 3 preview 2 (3.0.100-preview-010184)
- Added support for .NET Standart 1.5/1.6
- Added support for .NET Core 1.0/1.1/2.0/2.1
- Added API functions
- All CreateInstance, GetInstance, RegisterType now have the same semantics
- Exposed public Container API to IContainer
Container.CreateInstance(string)
Container.CreateInstance(string, object[])
Container.CreateInstance(string, string[])
Container.CreateInstance(string, System.Type[])
Container.CreateInstance(System.Type)
Container.CreateInstance(System.Type, object[])
Container.CreateInstance(System.Type, string[])
Container.CreateInstance(System.Type, System.Type[])
Container.CreateInstance<T>()
Container.CreateInstance<T>(object[])
Container.CreateInstance<T>(string[])
Container.CreateInstance<T>(System.Type[])
Container.GetInstance(string)
Container.GetInstance(string, object[])
Container.GetInstance(string, string[])
Container.GetInstance(string, System.Type[])
Container.GetInstance(System.Type)
Container.GetInstance(System.Type, object[])
Container.GetInstance(System.Type, string[])
Container.GetInstance(System.Type, System.Type[])
Container.RegisterType(string)
Container.RegisterType(string, object[])
Container.RegisterType(string, string[])
Container.RegisterType(string, System.Type[])
Container.RegisterType(System.Type)
Container.RegisterType(System.Type, object[])
Container.RegisterType(System.Type, string[])
Container.RegisterType(System.Type, System.Type[])
Container.RegisterType<T>()
Container.RegisterType<T>(object[])
Container.RegisterType<T>(string[])
Container.RegisterType<T>(System.Type[])
- Added API functions
- Added value types support
- Added
TypeBuilderOptions
class - Added sample implementation of property-based type dependency injector
- Added ASP.NET Core 2.1 middleware extensibility
- Added
System.Func<T>
type initialization - Added
TypeBuilder.GetInstance(...)
methods
- Added type registration parameters
- Added support for generic parameter types
- Use standart type definiton (Type.ToString())
Example:
//TestSet18
var container = new Container();
var type = new Type();
System.Func<Type> func = () => type;
container.RegisterType<Lazy<Type>>(func);
var factory = (IFactory<Type>)container.CreateInstance("Build.Tests.TestSet18.Lazy`1[Build.Tests.TestSet18.Type]");
Assert.Equal(type, factory.GetInstance());
Definition:
public interface IFactory<T>
{
T GetInstance();
}
public class Lazy<T> : IFactory<T>
{
public Lazy(Func<T> func) => Func = func;
public Func<T> Func { get; }
public T GetInstance() => Func();
}
- Added runtimes for .NET Standart 2.0, .NET Framework 4.5/4.5.1/4.5.2/4.6/4.6.1/4.6.2/4.7/4.7.1/4.7.2
- Added runtime default checks, to avoid System.MissingMethodException and System.Reflection.AmbiguousMatchException.
- Added automatic type attribute overwrite optional parameter to the build system
- By default, type runtime attributes overrides each other and optionally can be turned off. Exception will be trown.
Example:
[Interface]
interface IInterfaceRuleSet2
{
[InterfaceDependency(RuntimeInstance.Singleton)]
SqlDataRepository Rule(int repositoryId);
}
[Interface]
interface IInterfaceRuleSet2_Overwrite
{
[InterfaceDependency(RuntimeInstance.Singleton)]
SqlDataRepository Rule(int repositoryId);
}
- Added method CanRegisterParameter to ITypeFilter to control parameter registration
- Added public sealed classes TypeDependencyObject, TypeInjectionObject
- Added totally customizable type system
- Added ability to use interfaces as first-class objects
- You can use interfaces as first-class objects instead of classes fot typeholders attributes
- Using interfaces as first-class objects, you will be able to eliminate the need to inject attributes into existing code
- By implemeting type system interfaces, you will get a fully customzable type system, easily
- Registration is bound to interfaces only, which can have a several metods named "Rule" (it is evil, i knew it)
- Type is being instantiatied is actually a return parameter of "Rule" method
- Arguments passed to the constructor is simply arguments of that particular rule
- You definetly can break some more rules, just do not try to build inconsistent type system.
Definition:
interface IInterfaceRuleSet
{
Type1 Rule(Arg1 arg1, Arg2 arg2);
}
class Type1
{
public Type1(Arg1 arg1, Arg2 arg2)
{
Arg1 = arg1;
Arg2 = arg2;
}
public Arg1 Arg1 { get; }
public Arg2 Arg2 { get; }
}
Usage:
var container = new Container(new InterfaceTypeConstructor(), new InterfaceTypeFilter(), new InterfaceTypeParser(), new InterfaceTypeResolver());
container.RegisterType<IInterfaceRuleSet>();
var type1 = container.CreateInstance("Build.Tests.TestSet21.Type1(Build.Tests.TestSet21.Arg1,Build.Tests.TestSet21.Arg2)");
Assert.NotNull(type1);
- Enable/disable automatic type resolution
- Enable/disable automatic type instantiation
Parameter is set to true if automatic type resolution for reference types option enabled (does not throws exceptions for reference types contains type dependencies to non-registered types). If automatic type resolution for reference types is enabled, type will defaults to null if not resolved and no exception will be thrown.
Usage:
// Automatic type resolution disabled
// Instantiation reqires SqlDataRepository to be resolved
var container = new Container(false, true);
container.RegisterType<ServiceDataRepository>();
container.RegisterType<WebServiceDataRepository>();
Assert.Throws<TypeInstantiationException>(() => container.CreateInstance("Build.Tests.TestSet15.WebServiceDataRepository(Build.Tests.TestSet15.SqlDataRepository)"));
/// Automatic type resolution enabled
var container = new Container(true, false);
container.RegisterType<WebServiceDataRepository>();
var sql = (WebServiceDataRepository)container.CreateInstance("Build.Tests.TestSet15.WebServiceDataRepository(Build.Tests.TestSet15.SqlDataRepository)");
Assert.Equal(2019, ((SqlDataRepository)sql.Repository).PersonId);
Definition:
public class ServiceDataRepository : IPersonRepository
{
public ServiceDataRepository([Injection(typeof(SqlDataRepository), 2018)]IPersonRepository repository)
{
Repository = repository;
}
public IPersonRepository Repository { get; }
public Person GetPerson(int personId)
{
// get the data from Web service and return Person instance.
return new Person(this);
}
}
public class WebServiceDataRepository : IPersonRepository
{
public WebServiceDataRepository([Injection(typeof(SqlDataRepository), 2019)]IPersonRepository repository)
{
Repository = repository;
}
public IPersonRepository Repository { get; }
public Person GetPerson(int personId)
{
// get the data from Web service and return Person instance.
return new Person(this);
}
}
Parameter is set to true if automatic type instantiation for reference types option enabled (does not throws exceptions for reference types defaults to null). If automatic type instantiation for reference types is enabled, type will defaults to null if not resolved and no exception will be thrown.
Usage:
// Automatic type instantiation disabled
var container = new Container(true, false);
container.RegisterType<ServiceDataRepository2>();
Assert.Throws<TypeInstantiationException>(() => container.CreateInstance<ServiceDataRepository2>());
// Automatic type instantiation enabled
// ServiceDataRepository2 depends upon non existent Build.Tests.Fail_TestSet1.Other2
// and resolved to null
var container = new Container(false, true);
container.RegisterType<ServiceDataRepository2>();
var sql = container.CreateInstance<ServiceDataRepository2>();
Assert.Null(sql.Repository);
Definition:
public class ServiceDataRepository2 : IPersonRepository
{
public ServiceDataRepository2([Injection("Build.Tests.Fail_TestSet1.Other2")]IPersonRepository repository)
{
Repository = repository;
}
public IPersonRepository Repository { get; }
public Person GetPerson(int personId)
{
// get the data from Web service and return Person instance.
return new Person(this);
}
}
- Automatic parameters cleanup
- Registered ValueType instantiation
- RuntimeAliasedTypes,RuntimeNonAliasedTypes,RuntimeTypes & RuntimeTypeAliases properties
- Reset() function
Usage:
var instance1 = (SqlDataRepository)container.CreateInstance("Build.Tests.TestSet3.SqlDataRepository(System.Int32)", 123);
var instance2 = (SqlDataRepository)container.CreateInstance("Build.Tests.TestSet3.SqlDataRepository(System.Int32)");
Assert.Equal(0, instance2.PersonId);
Usage:
var instance = (int)container.CreateInstance("System.Int32()");
Assert.True(instance == 0);
Usage:
var instances = container.RuntimeAliasedTypes.Select(p => container.CreateInstance(p));
Assert.True(instances.All(p => p != null));
var instances = container.RuntimeNonAliasedTypes.Select(p => container.CreateInstance(p));
Assert.True(instances.All(p => p != null));
var instances = container.RuntimeTypes.Select(p => container.CreateInstance(p));
Assert.True(instances.All(p => p != null));
var instances = container.RuntimeTypeAliases.Select(p => container.CreateInstance(p));
Assert.True(instances.All(p => p != null));
Usage:
container.RegisterType<SqlDataRepository>();
container.RegisterType<ServiceDataRepository>();
bool exception = false;
try
{
container.Reset();
}
catch (Exception)
{
exception = true;
}
Assert.False(exception);
- Removed public type specificator requirement
Usage:
container.RegisterType<PrivateSqlDataRepository>();
var srv1 = container.CreateInstance("Build.Tests.TestSet1.PrivateSqlDataRepository");
Assert.NotNull(srv1);
Definition:
class PrivateSqlDataRepository : IPersonRepository
{
public PrivateSqlDataRepository()
{
}
public PrivateSqlDataRepository(int personId)
{
PersonId = personId;
}
public int PersonId { get; }
public Person GetPerson(int personId)
{
// get the data from SQL DB and return Person instance.
return new Person(this);
}
}
- Added support for automatic type resolution (type dependency resolution)
Usage:
// ServiceDataRepository depends upon SqlDataRepository
container.RegisterType<ServiceDataRepository>();
var sql = (ServiceDataRepository)container.CreateInstance("Build.Tests.TestSet16.ServiceDataRepository(Build.Tests.TestSet16.SqlDataRepository)");
Assert.NotNull(sql.Repository);
Definition:
public class SqlDataRepository : IPersonRepository
{
[Dependency(RuntimeInstance.Singleton)]
public SqlDataRepository(int repositoryId) => RepositoryId = repositoryId;
public int RepositoryId { get; }
public Person GetPerson(int personId)
{
// get the data from SQL DB and return Person instance.
return new Person(this);
}
}
public class ServiceDataRepository : IPersonRepository
{
public ServiceDataRepository(int repositoryId) => RepositoryId = repositoryId;
public ServiceDataRepository([Injection(typeof(SqlDataRepository), 2018)]IPersonRepository repository)
{
Repository = repository;
}
public IPersonRepository Repository { get; }
public int RepositoryId { get; }
public Person GetPerson(int personId)
{
// get the data from Web service and return Person instance.
return new Person(this);
}
}
- Added support for multiple dependency injection attributes
Usage:
var sql1 = (WebServiceDataRepository)container.CreateInstance("Build.Tests.TestSet16.WebServiceDataRepository(Build.Tests.TestSet16.ServiceDataRepository)");
Assert.Equal(2019, ((ServiceDataRepository)sql1.RepositoryA).RepositoryId);
var sql2 = (WebServiceDataRepository)container.CreateInstance("Build.Tests.TestSet16.WebServiceDataRepository(Build.Tests.TestSet16.IPersonRepository, Build.Tests.TestSet16.IPersonRepository)");
Assert.Equal(2020, ((ServiceDataRepository)sql2.RepositoryA).RepositoryId);
var sql3 = (WebServiceDataRepository)container.CreateInstance("Build.Tests.TestSet16.WebServiceDataRepository(Build.Tests.TestSet16.IPersonRepository, Build.Tests.TestSet16.IPersonRepository)");
Assert.Equal(2021, ((SqlDataRepository)sql3.RepositoryB).RepositoryId);
Definition:
public class WebServiceDataRepository : IPersonRepository
{
public WebServiceDataRepository(int repositoryId) => RepositoryId = repositoryId;
public WebServiceDataRepository([Injection(typeof(ServiceDataRepository), 2019)]IPersonRepository repository)
{
RepositoryA = repository;
}
public WebServiceDataRepository(
[Injection("Build.Tests.TestSet16.ServiceDataRepository", 2020)]IPersonRepository repositoryA,
[Injection("Build.Tests.TestSet16.SqlDataRepository", 2021)]IPersonRepository repositoryB)
{
RepositoryA = repositoryA;
RepositoryB = repositoryB;
}
public IPersonRepository RepositoryA { get; }
public IPersonRepository RepositoryB { get; }
public int RepositoryId { get; }
public Person GetPerson(int personId)
{
// get the data from Web service and return Person instance.
return new Person(this);
}
}
- Added support for default parameterless constructor with parameters injection using attributes
Usage:
var container = new Container();
container.RegisterType<SqlDataRepository>();
container.RegisterType<ServiceDataRepository>();
var srv = (ServiceDataRepository)container.CreateInstance(
"UnitTests.TestSet15.ServiceDataRepository(UnitTests.TestSet15.IPersonRepository)");
Definition:
public class SqlDataRepository : IPersonRepository
{
public int PersonId { get; }
public SqlDataRepository(int personId)
{
PersonId = personId;
}
public Person GetPerson(int personId)
{
// get the data from SQL DB and return Person instance.
return new Person(this);
}
}
public class ServiceDataRepository : IPersonRepository
{
public ServiceDataRepository([Injection(typeof(SqlDataRepository), 2018)]
IPersonRepository repository)
{
Repository = repository;
}
public IPersonRepository Repository { get; }
public Person GetPerson(int personId)
{
// get the data from Web service and return Person instance.
return new Person(this);
}
}
- Interface first-class support (instantiation, strong typing, external assembly)
- Circular references detection in type registration
- Automatic type resolution for all supported types
- Pure type instantiation (weak coupling)
- Declarative metadata attribute driven initialization
- Lazy type resolution and initialization (supports pure dependency injection decoupling anti-pattern)
- Singleton initialization support
- Automated and manual type registration
- Types aliases (user-friendly type identification)
- External assembly types support
- Elimination of overlapped attribute specificators (removes violation of the SOLID principles)
- Circular references detection phase moved to type registration rather instantiation
- Automatic type resolution for all supported types if used in type instantiation
- Pure type instantiation as descriptive string with particular constructor and passing corresponding parameters
- Declarative metadata attribute driven initialization
- Lazy type resolution and initialization (supports pure dependency decoupling anti-pattern)
- Circular references detection
- Singleton initialization
- Automated and manual type registration
- Type aliases
- External assembly types
.NET Dependency Injection framework is framework to build automation of complex types. Build can use declarative approach to define dependencies between types and their requirements. Constructor injection uses type resolution to resolve dependencies
Usage:
var container = new Container();
container.RegisterType<SqlDataRepository>();
container.RegisterType<ServiceDataRepository>();
var sql = new SqlDataRepository();
var srv1 = (ServiceDataRepository)container.CreateInstance(
"UnitTests.TestSet14.ServiceDataRepository(UnitTests.TestSet14.SqlDataRepository)", sql);
Usage:
IContainer container = new Container();
container.RegisterType<SqlDataRepository>();
container.RegisterType<ServiceDataRepository>();
var srv1 = container.CreateInstance<ServiceDataRepository>();
Definition:
public interface IPersonRepository
{
Person GetPerson(int personId);
}
public class Person
{
readonly IPersonRepository _personRepository;
public Person(IPersonRepository personRepository)
{
_personRepository = personRepository;
}
}
public class SqlDataRepository : IPersonRepository
{
public SqlDataRepository()
{
}
public Person GetPerson(int personId)
{
// get the data from SQL DB and return Person instance.
return new Person(this);
}
}
public class ServiceDataRepository : IPersonRepository
{
public ServiceDataRepository(SqlDataRepository repository)
{
Repository = repository;
}
public IPersonRepository Repository { get; }
public Person GetPerson(int personId)
{
// get the data from Web service and return Person instance.
return new Person(this);
}
}
Dependency Injection for Programming by Optimization, Zoltan A. Kocsis and Jerry Swan
- School of Mathematics, University of Manchester, Oxford Road, Manchester M13 9PL, UK. [email protected]
- Computer Science, University of York, Deramore Lane, York, YO10 5GH, UK.
- Explicit Dependencies Principle
- Inversion of Control Containers and the Dependency Injection pattern
- Dependency injection in ASP.NET Core
- ASP.NET Core Middleware
- Middleware activation with a third-party container in ASP.NET Core.
- Simple Injector sample for ASP.NET
Please, feel free to donate me 5$ to expand project development (wiki, samples, etc.)