Skip to content
.NET Dependency Injection framework
Branch: master
Clone or download
hack2root Update coverage.ps1
exclude *AttributeProvider*
Latest commit cd4fbee Mar 6, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.appveyour Update Dockerfile Mar 3, 2019
.aws AWS Lambda function update for Slack channel May 28, 2018
.circleci Added gitter.im webhook for CircleCI Mar 4, 2019
.config v1.0.0.27 Mar 2, 2019
.github/ISSUE_TEMPLATE Update issue templates Jul 4, 2018
.myget v1.0.0.0.19 Jul 15, 2018
.nuget v1.0.0.27 Mar 2, 2019
Build.Abstractions fixed Mar 4, 2019
Build.Tests Update Build.Tests.csproj Mar 5, 2019
Build fixed Mar 4, 2019
aspnetcore/fundamentals/middleware added solution file Jun 25, 2018
common added public key Jul 4, 2018
docs Update README.md Mar 4, 2019
.gitattributes Merge branch 'master' of https://github.com/hack2root/build Apr 17, 2018
.gitignore added TypeActivator, TypeBuilderOptions Jul 1, 2018
.gitlab-ci.yml changed pipleine Jun 17, 2018
.travis.yml Update .travis.yml Mar 4, 2019
Build.sln update pipeline Mar 5, 2019
Dockerfile Update AppVeyour, Travis CI Mar 3, 2019
LICENSE Initial commit Apr 12, 2018
NuGet.config myget test Jul 4, 2018
appveyor.yml stick to version 2.5.1 Mar 5, 2019
build-workspace.code-workspace added scripts May 25, 2018
build.cmd updated build task Jul 25, 2018
cleanup.cmd added cleanup batch Jul 23, 2018
common.props commented out assembly sining Jul 4, 2018
coverage.cmd switched to taskkill Jul 25, 2018
coverage.ps1
docker-clean.ps1 Added AppVeyour cleanup step Mar 3, 2019
docker-init.ps1 Added code coverage step Mar 3, 2019
global.json Added support for .NET Core 3 preview 2 (3.0.100-preview-010184) Mar 2, 2019
md-styles.css added ms-styles for a [Markdown Editor](https://marketplace.visualstu… Apr 25, 2018
myget.ps1 cleanup pipeline Jul 4, 2018
nuget.ps1 cleanup pipeline Jul 4, 2018

README.md

Welcome to the build wiki!

.NET Dependency Injection framework

build-core MyGet Build Status VSTS CircleCI Build status Amazon AWS Travis CI CodeFactor

Maintainability

build build build build build Coverage Status

Community

Welcome to #build #slack

CodeFactor NuGet version NuGet version NuGet downloads NuGet downloads Discord

Channels

gitter

Repository

build

Docs

Build

Prerequisites:

v1.0.0.27

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

v1.0.0.25

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

v1.0.0.19

  • 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[])

v1.0.0.18

  • Added API functions

v1.0.0.17

  • Added value types support

v1.0.0.16

  • Added TypeBuilderOptions class
  • Added sample implementation of property-based type dependency injector

v1.0.0.15

v1.0.0.14

  • 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();
}

v1.0.0.13

  • 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

v1.0.0.12

  • Added runtime default checks, to avoid System.MissingMethodException and System.Reflection.AmbiguousMatchException.

v1.0.0.11

  • 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);
}

v1.0.0.10

  • Added method CanRegisterParameter to ITypeFilter to control parameter registration

v1.0.0.9

  • Added public sealed classes TypeDependencyObject, TypeInjectionObject

v1.0.0.8

  • Added totally customizable type system
  • Added ability to use interfaces as first-class objects

Examples

Customizable type system

  • 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);

v1.0.0.7

  • Enable/disable automatic type resolution
  • Enable/disable automatic type instantiation

Examples

Automatic type resolution disabled

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);
    }
}

Automatic type instantiation disabled

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);
    }
}

v1.0.0.5

  • Automatic parameters cleanup
  • Registered ValueType instantiation
  • RuntimeAliasedTypes,RuntimeNonAliasedTypes,RuntimeTypes & RuntimeTypeAliases properties
  • Reset() function

Examples

Automatic parameters cleanup

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);

Registered ValueType instantiation

Usage:

var instance = (int)container.CreateInstance("System.Int32()");
Assert.True(instance == 0);

RuntimeAliasedTypes,RuntimeNonAliasedTypes,RuntimeTypes & RuntimeTypeAliases properties

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));

Reset() function

Usage:

container.RegisterType<SqlDataRepository>();
container.RegisterType<ServiceDataRepository>();
bool exception = false;
try
{
    container.Reset();
}
catch (Exception)
{
    exception = true;
}
Assert.False(exception);

v1.0.0.4

  • Removed public type specificator requirement

Examples

Private type resolution (removed public type 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);
    }
}

v1.0.0.3

  • Added support for automatic type resolution (type dependency resolution)

Examples

Automatic 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);
    }
}

v1.0.0.2

  • Added support for multiple dependency injection attributes

Examples

Constrctor parametrization

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);
    }
}

v1.0.0.1

Major improvements

  • Added support for default parameterless constructor with parameters injection using attributes

Examples

Default constructor with parameters injection

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);
    }
}

Description

  • 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

v1.0.0.0

  • 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

Features

  • 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

Goal

.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

Examples

Create instance with parameters

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);

Load simple types (not attributes specified)

Usage:

IContainer container = new Container();
container.RegisterType<SqlDataRepository>();
container.RegisterType<ServiceDataRepository>();
var srv1 = container.CreateInstance<ServiceDataRepository>();

API

Classes

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);
    }
}

Continious Integration

CircleCI TravisCI

Academic papers:

Dependency Injection for Programming by Optimization, Zoltan A. Kocsis and Jerry Swan

  1. School of Mathematics, University of Manchester, Oxford Road, Manchester M13 9PL, UK. zoltan.kocsis@postgrad.manchester.ac.uk
  2. Computer Science, University of York, Deramore Lane, York, YO10 5GH, UK.

Links

Donate

Support via PayPal

Please, feel free to donate me 5$ to expand project development (wiki, samples, etc.)

You can’t perform that action at this time.