Class DependencyContainer
The concrete implementation of a simple IoC container based largely on TinyIoC (https://github.com/grumpydev/TinyIoC).
Implements
Inherited Members
Namespace: Swan.DependencyInjection
Syntax
public class DependencyContainer : IDisposable
Constructors
DependencyContainer()
Initializes a new instance of the DependencyContainer class.
Declaration
public DependencyContainer()
Properties
Current
Lazy created Singleton instance of the container for simple scenarios.
Declaration
public static DependencyContainer Current { get; }
Property Value
Type | Description |
---|---|
DependencyContainer |
Methods
AutoRegister(DependencyContainerDuplicateImplementationAction, Func<Type, Boolean>)
Attempt to automatically register all non-generic classes and interfaces in the current app domain. Types will only be registered if they pass the supplied registration predicate.
Declaration
public void AutoRegister(DependencyContainerDuplicateImplementationAction duplicateAction = DependencyContainerDuplicateImplementationAction.RegisterSingle, Func<Type, bool> registrationPredicate = null)
Parameters
Type | Name | Description |
---|---|---|
DependencyContainerDuplicateImplementationAction | duplicateAction | What action to take when encountering duplicate implementations of an interface/base class. |
Func<Type, Boolean> | registrationPredicate | Predicate to determine if a particular type should be registered. |
AutoRegister(IEnumerable<Assembly>, DependencyContainerDuplicateImplementationAction, Func<Type, Boolean>)
Attempt to automatically register all non-generic classes and interfaces in the specified assemblies Types will only be registered if they pass the supplied registration predicate.
Declaration
public void AutoRegister(IEnumerable<Assembly> assemblies, DependencyContainerDuplicateImplementationAction duplicateAction = DependencyContainerDuplicateImplementationAction.RegisterSingle, Func<Type, bool> registrationPredicate = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Assembly> | assemblies | Assemblies to process. |
DependencyContainerDuplicateImplementationAction | duplicateAction | What action to take when encountering duplicate implementations of an interface/base class. |
Func<Type, Boolean> | registrationPredicate | Predicate to determine if a particular type should be registered. |
BuildUp(Object, DependencyContainerResolveOptions)
Attempts to resolve all public property dependencies on the given object using the given resolve options.
Declaration
public void BuildUp(object input, DependencyContainerResolveOptions resolveOptions = null)
Parameters
Type | Name | Description |
---|---|---|
Object | input | Object to "build up". |
DependencyContainerResolveOptions | resolveOptions | Resolve options to use. |
CanResolve(Type, String, DependencyContainerResolveOptions)
Attempts to predict whether a given type can be resolved with the supplied constructor parameters options. Parameters are used in conjunction with normal container resolution to find the most suitable constructor (if one exists). All user supplied parameters must exist in at least one resolvable constructor of RegisterType or resolution will fail. Note: Resolution may still fail if user defined factory registrations fail to construct objects when called.
Declaration
public bool CanResolve(Type resolveType, string name = null, DependencyContainerResolveOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
Type | resolveType | Type to resolve. |
String | name | The name. |
DependencyContainerResolveOptions | options | Resolution options. |
Returns
Type | Description |
---|---|
Boolean | Bool indicating whether the type can be resolved. |
CanResolve<TResolveType>(String, DependencyContainerResolveOptions)
Attempts to predict whether a given named type can be resolved with the supplied constructor parameters options.
Parameters are used in conjunction with normal container resolution to find the most suitable constructor (if one exists). All user supplied parameters must exist in at least one resolvable constructor of RegisterType or resolution will fail.
Note: Resolution may still fail if user defined factory registrations fail to construct objects when called.
Declaration
public bool CanResolve<TResolveType>(string name = null, DependencyContainerResolveOptions options = null)
where TResolveType : class
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of registration. |
DependencyContainerResolveOptions | options | Resolution options. |
Returns
Type | Description |
---|---|
Boolean | Bool indicating whether the type can be resolved. |
Type Parameters
Name | Description |
---|---|
TResolveType | Type to resolve. |
Dispose()
Declaration
public void Dispose()
GetChildContainer()
Gets the child container.
Declaration
public DependencyContainer GetChildContainer()
Returns
Type | Description |
---|---|
DependencyContainer | A new instance of the DependencyContainer class. |
Register(Type, Func<DependencyContainer, Dictionary<String, Object>, Object>, String)
Creates/replaces a container class registration with a user specified factory.
Declaration
public RegisterOptions Register(Type registerType, Func<DependencyContainer, Dictionary<string, object>, object> factory, string name = "")
Parameters
Type | Name | Description |
---|---|---|
Type | registerType | Type to register. |
Func<DependencyContainer, Dictionary<String, Object>, Object> | factory | Factory/lambda that returns an instance of RegisterType. |
String | name | Name of registration. |
Returns
Type | Description |
---|---|
RegisterOptions | RegisterOptions for fluent API. |
Register(Type, Object, String)
Creates/replaces a named container class registration with a specific, strong referenced, instance.
Declaration
public RegisterOptions Register(Type registerType, object instance, string name = "")
Parameters
Type | Name | Description |
---|---|---|
Type | registerType | Type to register. |
Object | instance | Instance of RegisterType to register. |
String | name | Name of registration. |
Returns
Type | Description |
---|---|
RegisterOptions | RegisterOptions for fluent API. |
Register(Type, String)
Creates/replaces a named container class registration with default options.
Declaration
public RegisterOptions Register(Type registerType, string name = "")
Parameters
Type | Name | Description |
---|---|---|
Type | registerType | Type to register. |
String | name | Name of registration. |
Returns
Type | Description |
---|---|
RegisterOptions | RegisterOptions for fluent API. |
Register(Type, Type, Object, String)
Creates/replaces a named container class registration with a specific, strong referenced, instance.
Declaration
public RegisterOptions Register(Type registerType, Type registerImplementation, object instance, string name = "")
Parameters
Type | Name | Description |
---|---|---|
Type | registerType | Type to register. |
Type | registerImplementation | Type of instance to register that implements RegisterType. |
Object | instance | Instance of RegisterImplementation to register. |
String | name | Name of registration. |
Returns
Type | Description |
---|---|
RegisterOptions | RegisterOptions for fluent API. |
Register(Type, Type, String)
Creates/replaces a named container class registration with a given implementation and default options.
Declaration
public RegisterOptions Register(Type registerType, Type registerImplementation, string name = "")
Parameters
Type | Name | Description |
---|---|---|
Type | registerType | Type to register. |
Type | registerImplementation | Type to instantiate that implements RegisterType. |
String | name | Name of registration. |
Returns
Type | Description |
---|---|
RegisterOptions | RegisterOptions for fluent API. |
Register<TRegister>(TRegister, String)
Creates/replaces a named container class registration with a specific, strong referenced, instance.
Declaration
public RegisterOptions Register<TRegister>(TRegister instance, string name = "")
where TRegister : class
Parameters
Type | Name | Description |
---|---|---|
TRegister | instance | Instance of RegisterType to register. |
String | name | Name of registration. |
Returns
Type | Description |
---|---|
RegisterOptions | RegisterOptions for fluent API. |
Type Parameters
Name | Description |
---|---|
TRegister | Type to register. |
Register<TRegister>(Func<DependencyContainer, Dictionary<String, Object>, TRegister>, String)
Creates/replaces a named container class registration with a user specified factory.
Declaration
public RegisterOptions Register<TRegister>(Func<DependencyContainer, Dictionary<string, object>, TRegister> factory, string name = "")
where TRegister : class
Parameters
Type | Name | Description |
---|---|---|
Func<DependencyContainer, Dictionary<String, Object>, TRegister> | factory | Factory/lambda that returns an instance of RegisterType. |
String | name | Name of registration. |
Returns
Type | Description |
---|---|
RegisterOptions | RegisterOptions for fluent API. |
Type Parameters
Name | Description |
---|---|
TRegister | Type to register. |
Register<TRegister>(String)
Creates/replaces a named container class registration with default options.
Declaration
public RegisterOptions Register<TRegister>(string name = "")
where TRegister : class
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of registration. |
Returns
Type | Description |
---|---|
RegisterOptions | RegisterOptions for fluent API. |
Type Parameters
Name | Description |
---|---|
TRegister | Type to register. |
Register<TRegister, TRegisterImplementation>(TRegisterImplementation, String)
Creates/replaces a named container class registration with a specific, strong referenced, instance.
Declaration
public RegisterOptions Register<TRegister, TRegisterImplementation>(TRegisterImplementation instance, string name = "")
where TRegister : class where TRegisterImplementation : class, TRegister
Parameters
Type | Name | Description |
---|---|---|
TRegisterImplementation | instance | Instance of RegisterImplementation to register. |
String | name | Name of registration. |
Returns
Type | Description |
---|---|
RegisterOptions | RegisterOptions for fluent API. |
Type Parameters
Name | Description |
---|---|
TRegister | Type to register. |
TRegisterImplementation | Type of instance to register that implements RegisterType. |
Register<TRegister, TRegisterImplementation>(String)
Creates/replaces a named container class registration with a given implementation and default options.
Declaration
public RegisterOptions Register<TRegister, TRegisterImplementation>(string name = "")
where TRegister : class where TRegisterImplementation : class, TRegister
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of registration. |
Returns
Type | Description |
---|---|
RegisterOptions | RegisterOptions for fluent API. |
Type Parameters
Name | Description |
---|---|
TRegister | Type to register. |
TRegisterImplementation | Type to instantiate that implements RegisterType. |
RegisterMultiple(Type, IEnumerable<Type>)
Register multiple implementations of a type.
Internally this registers each implementation using the full name of the class as its registration name.
Declaration
public MultiRegisterOptions RegisterMultiple(Type registrationType, IEnumerable<Type> implementationTypes)
Parameters
Type | Name | Description |
---|---|---|
Type | registrationType | Type that each implementation implements. |
IEnumerable<Type> | implementationTypes | Types that implement RegisterType. |
Returns
Type | Description |
---|---|
MultiRegisterOptions | MultiRegisterOptions for the fluent API. |
RegisterMultiple<TRegister>(IEnumerable<Type>)
Register multiple implementations of a type.
Internally this registers each implementation using the full name of the class as its registration name.
Declaration
public MultiRegisterOptions RegisterMultiple<TRegister>(IEnumerable<Type> implementationTypes)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Type> | implementationTypes | Types that implement RegisterType. |
Returns
Type | Description |
---|---|
MultiRegisterOptions | MultiRegisterOptions for the fluent API. |
Type Parameters
Name | Description |
---|---|
TRegister | Type that each implementation implements. |
Resolve(Type, String, DependencyContainerResolveOptions)
Attempts to resolve a named type using specified options and the supplied constructor parameters.
Parameters are used in conjunction with normal container resolution to find the most suitable constructor (if one exists). All user supplied parameters must exist in at least one resolvable constructor of RegisterType or resolution will fail.
Declaration
public object Resolve(Type resolveType, string name = null, DependencyContainerResolveOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
Type | resolveType | Type to resolve. |
String | name | Name of registration. |
DependencyContainerResolveOptions | options | Resolution options. |
Returns
Type | Description |
---|---|
Object | Instance of type. |
Exceptions
Type | Condition |
---|---|
DependencyContainerResolutionException | Unable to resolve the type. |
Resolve<TResolveType>(String, DependencyContainerResolveOptions)
Attempts to resolve a named type using specified options and the supplied constructor parameters.
Parameters are used in conjunction with normal container resolution to find the most suitable constructor (if one exists). All user supplied parameters must exist in at least one resolvable constructor of RegisterType or resolution will fail.
Declaration
public TResolveType Resolve<TResolveType>(string name = null, DependencyContainerResolveOptions options = null)
where TResolveType : class
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of registration. |
DependencyContainerResolveOptions | options | Resolution options. |
Returns
Type | Description |
---|---|
TResolveType | Instance of type. |
Type Parameters
Name | Description |
---|---|
TResolveType | Type to resolve. |
Exceptions
Type | Condition |
---|---|
DependencyContainerResolutionException | Unable to resolve the type. |
ResolveAll(Type, Boolean)
Returns all registrations of a type.
Declaration
public IEnumerable<object> ResolveAll(Type resolveType, bool includeUnnamed = false)
Parameters
Type | Name | Description |
---|---|---|
Type | resolveType | Type to resolveAll. |
Boolean | includeUnnamed | Whether to include un-named (default) registrations. |
Returns
Type | Description |
---|---|
IEnumerable<Object> | IEnumerable. |
ResolveAll<TResolveType>(Boolean)
Returns all registrations of a type.
Declaration
public IEnumerable<TResolveType> ResolveAll<TResolveType>(bool includeUnnamed = true)
where TResolveType : class
Parameters
Type | Name | Description |
---|---|---|
Boolean | includeUnnamed | Whether to include un-named (default) registrations. |
Returns
Type | Description |
---|---|
IEnumerable<TResolveType> | IEnumerable. |
Type Parameters
Name | Description |
---|---|
TResolveType | Type to resolveAll. |
TryResolve(Type, DependencyContainerResolveOptions, out Object)
Attempts to resolve a type using the given options.
Declaration
public bool TryResolve(Type resolveType, DependencyContainerResolveOptions options, out object resolvedType)
Parameters
Type | Name | Description |
---|---|---|
Type | resolveType | Type to resolve. |
DependencyContainerResolveOptions | options | Resolution options. |
Object | resolvedType | Resolved type or default if resolve fails. |
Returns
Type | Description |
---|---|
Boolean |
|
TryResolve(Type, out Object)
Attempts to resolve a type using the default options.
Declaration
public bool TryResolve(Type resolveType, out object resolvedType)
Parameters
Type | Name | Description |
---|---|---|
Type | resolveType | Type to resolve. |
Object | resolvedType | Resolved type or default if resolve fails. |
Returns
Type | Description |
---|---|
Boolean |
|
TryResolve(Type, String, DependencyContainerResolveOptions, out Object)
Attempts to resolve a type using the given options and name.
Declaration
public bool TryResolve(Type resolveType, string name, DependencyContainerResolveOptions options, out object resolvedType)
Parameters
Type | Name | Description |
---|---|---|
Type | resolveType | Type to resolve. |
String | name | Name of registration. |
DependencyContainerResolveOptions | options | Resolution options. |
Object | resolvedType | Resolved type or default if resolve fails. |
Returns
Type | Description |
---|---|
Boolean |
|
TryResolve(Type, String, out Object)
Attempts to resolve a type using the default options and given name.
Declaration
public bool TryResolve(Type resolveType, string name, out object resolvedType)
Parameters
Type | Name | Description |
---|---|---|
Type | resolveType | Type to resolve. |
String | name | Name of registration. |
Object | resolvedType | Resolved type or default if resolve fails. |
Returns
Type | Description |
---|---|
Boolean |
|
TryResolve<TResolveType>(out TResolveType)
Attempts to resolve a type using the default options.
Declaration
public bool TryResolve<TResolveType>(out TResolveType resolvedType)
where TResolveType : class
Parameters
Type | Name | Description |
---|---|---|
TResolveType | resolvedType | Resolved type or default if resolve fails. |
Returns
Type | Description |
---|---|
Boolean |
|
Type Parameters
Name | Description |
---|---|
TResolveType | Type to resolve. |
TryResolve<TResolveType>(DependencyContainerResolveOptions, out TResolveType)
Attempts to resolve a type using the given options.
Declaration
public bool TryResolve<TResolveType>(DependencyContainerResolveOptions options, out TResolveType resolvedType)
where TResolveType : class
Parameters
Type | Name | Description |
---|---|---|
DependencyContainerResolveOptions | options | Resolution options. |
TResolveType | resolvedType | Resolved type or default if resolve fails. |
Returns
Type | Description |
---|---|
Boolean |
|
Type Parameters
Name | Description |
---|---|
TResolveType | Type to resolve. |
TryResolve<TResolveType>(String, out TResolveType)
Attempts to resolve a type using the default options and given name.
Declaration
public bool TryResolve<TResolveType>(string name, out TResolveType resolvedType)
where TResolveType : class
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of registration. |
TResolveType | resolvedType | Resolved type or default if resolve fails. |
Returns
Type | Description |
---|---|
Boolean |
|
Type Parameters
Name | Description |
---|---|
TResolveType | Type to resolve. |
TryResolve<TResolveType>(String, DependencyContainerResolveOptions, out TResolveType)
Attempts to resolve a type using the given options and name.
Declaration
public bool TryResolve<TResolveType>(string name, DependencyContainerResolveOptions options, out TResolveType resolvedType)
where TResolveType : class
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of registration. |
DependencyContainerResolveOptions | options | Resolution options. |
TResolveType | resolvedType | Resolved type or default if resolve fails. |
Returns
Type | Description |
---|---|
Boolean |
|
Type Parameters
Name | Description |
---|---|
TResolveType | Type to resolve. |
Unregister(Type, String)
Remove a named container class registration.
Declaration
public bool Unregister(Type registerType, string name = "")
Parameters
Type | Name | Description |
---|---|---|
Type | registerType | Type to unregister. |
String | name | Name of registration. |
Returns
Type | Description |
---|---|
Boolean |
|
Unregister<TRegister>(String)
Remove a named container class registration.
Declaration
public bool Unregister<TRegister>(string name = "")
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of registration. |
Returns
Type | Description |
---|---|
Boolean |
|
Type Parameters
Name | Description |
---|---|
TRegister | Type to unregister. |