Dependency Injection - HeilOliver/Timeify GitHub Wiki

Dependency Injection, DI for short, is a procedure that provides different dependencies in objects and manages their lifetime.

As DI Framework AutoFack is used which replaces the internal Net.Core DI Framework.

To make the manual registration of each component as easy as possible, an attribute is created for each component. [Injectable(typeof(FacadeType),InjectableAttribute.LifeTimeType.Hierarchical)] This attribute allows you to provide interfaces to objects, objects to objects, or objects as insertable components. The attribute also allows you to specify different lifetimes for the objects created. The "LifeTimeType" specifies the lifetime in the attribute.

  • Hierarchical - A new instance is created for each API controller call.
  • Container - A single object will be created which will last the whole lifetime of the program => Singleton

In addition to the lifetime of the object, a mapping interface/class can also be specified. This interface/class is then used for registration. The class with the corresponding attribute is used as the source object. This makes it possible to use interfaces as injectable types. During the start of the application, all packages of the application are loaded and then the respective classes are added to the DI framework using Reflection.

Attribut Example

  • [Injectable] If no parameters are specified, the labeled class is registered as the injectable type and as an singleton.
  • [Injectable(Hierarchical)] If only the lifetime is entered, the labeled class is used as injectable type and the specified lifetime is used.
  • [Injectable(typeof(ISampleInterface))] If only the register type is specified, the provided class is registered as base class and the specified type is used as injectable type. This combination is registered as singelton.
  • [Injectable(typeof(ISampleInterface),Hierarchical)] If both type and lifetime are specified, the provided class is registered as base class and the specified type is used as injectable type with the specified lifetime.