Skip to content
forked from alexAgyapong/build

.NET Dependency Injection framework

License

Notifications You must be signed in to change notification settings

paulriley/build

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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. [email protected]
  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.)

About

.NET Dependency Injection framework

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 91.0%
  • Batchfile 3.7%
  • CSS 2.8%
  • HTML 1.2%
  • JavaScript 0.6%
  • PowerShell 0.6%
  • Dockerfile 0.1%