Welcome to the build wiki!

.NET Core 2.1 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

#slack

Repository

build

Docs

v1.0.0.19

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

v1.0.0.17

v1.0.0.16

v1.0.0.15

v1.0.0.14

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

v1.0.0.12

v1.0.0.11

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

v1.0.0.9

v1.0.0.8

Examples

Customizable 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

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

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

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

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

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

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

v1.0.0.0

Features

Goal

The goal of development of this framework is to build automation of complex types initialization. Build can use declarative approach to define dependencies between types and their requirements. Constructor injection uses type resolution to resolve devendencies

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.

Support via PayPal

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