Plain DI library without the need for containers.
using PlainDI.Attribute;
[Injectable(typeof(Service))]
public interface IService {
//...
}
public class Service : IService {
//...
}
using PlainDI;
using PlainDI.Attribute;
public class Client {
[Inject] IService service;
//...
}
static void Main(string[] args) {
Client client = Injector.Get<Client>();
//...
}
Namespace: PlainDI
Exposes all the functionality of the DI library.
public sealed class Injector
Inheritance Object → Injector
Signature | Description |
---|---|
T Get<T>() | Instantiate and return object of type T with injected services. |
object Get(Type) | Instantiate and return object with injected services. |
void InjectExisting(object) | Inject services into an existing object. Only injects to its fields and properties. |
Create and return an object of type T with injected services.
T
The type of the object to create.
T
The instantiated object.
AttributeException
Thrown if [Injectable]
does not decorate either an interface or an abstract class.
AttributeException
Thrown if there are multiple constructors with [Default]
attributes.
NoImplementationException
Thrown if [Injectable]
targetInstance does not implement T.
CircularDependencyException
Thrown if a dependency is found referencing an earlier service.
MissingMethodException
Thrown if DI library cannot find a suitable constructor for T.
Create and return an object of type with injected services.
Type type
The type of the object to create.
object
The instantiated object.
AttributeException
Thrown if [Injectable]
does not decorate either an interface or an abstract class.
AttributeException
Thrown if there are multiple constructors with [Default]
attributes.
NoImplementationException
Thrown if [Injectable]
targetInstance does not implement type.
CircularDependencyException
Thrown if a dependency is found referencing an earlier service.
MissingMethodException
Thrown if DI library cannot find a suitable constructor for type.
Inject services into an existing object. Only injects to its fields and properties.
object obj
The object to inject fields and properties.
AttributeException
Thrown if [Injectable]
does not decorate either an interface or an abstract class.
AttributeException
Thrown if there are multiple constructors with [Default]
attributes.
NoImplementationException
Thrown if [Injectable]
targetInstance does not implement type.
CircularDependencyException
Thrown if a dependency is found referencing an earlier service.
MissingMethodException
Thrown if DI library cannot find a suitable constructor for type.
Calls a function and injects objects into its parameters.
Delegate function
The function to call.
object?
The object returned from the function else null.
AttributeException
Thrown if [Injectable]
does not decorate either an interface or an abstract class.
AttributeException
Thrown if there are multiple constructors with [Default]
attributes.
NoImplementationException
Thrown if [Injectable]
targetInstance does not implement type.
CircularDependencyException
Thrown if a dependency is found referencing an earlier service.
MissingMethodException
Thrown if DI library cannot find a suitable constructor for type.
using PlainDI.Attribute;
[Injectable(typeof(Service))]
public interface IService {
//...
}
public class Service : IService {
//...
}
using PlainDI;
using PlainDI.Attribute;
public class Client {
public void Foo(IService service) {
//...
}
}
static void Main(string[] args) {
//...
Injector.Invoke((Action<IService>)client.Foo);
}
Namespace: PlainDI.Attribute
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class InjectableAttribute : Attribute
Inheritance Object → Attribute → InjectableAttribute
Attributes AttributeUsageAttribute
InjectableAttribute(Type targetInstance, Lifetime lifetime = Lifetime.Transient)
Parameter | Description |
---|---|
targetInstance |
Chosen implementation of the interface to use that will be injected to clients. |
lifetime |
Specifies when a service is created for each client. Default transient. |
[Injectable]
can only be applied to aninterface
or abstract classes.- Requiring to specify
targetInstance
solves the ambiguity of which implementation to use in the case of multiple implementations. - There are three types of lifetimes:
Lifetimes | Description |
---|---|
Scoped | Same instance for each client but different between clients. |
Transient | Different instances for every request. |
Singleton | Single instance shared across all clients. |
Namespace: PlainDI.Attribute
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class InjectAttribute : Attribute
Inheritance Object → Attribute → InjectAttribute
Attributes AttributeUsageAttribute
InjectAttribute()
[Inject]
can only be applied to fields or properties.- Decorated fields and properties will be injected regardless of access modifiers
- Static fields and properties can also be injected
Namespace: PlainDI.Attribute
[AttributeUsage(AttributeTargets.Constructor)]
public class DefaultAttribute : Attribute
Inheritance Object → Attribute → DefaultAttribute
Attributes AttributeUsageAttribute
DefaultAttribute()
- The
[Default]
attribute is used to mark which constructor to use when assembling an object. - Decorating a constructor is necessary if there are multiple declared constructors.
- If there are no declared constructors or multiple undecorated constructors, the DI library will use the empty parameter constructor.