Dependency Injection Using Microsoft Unity Framework

Let's have a look at where it starts. It begins with the SOLID principle last Letter “D”, the Dependency Inversion Principle.

Agenda

  • Dependency Inversion Principle.
  • Inversion of Control.
  • Dependency Injection.
  • Creating a console application.
  • Adding Reference to Microsoft Unity Framework.
  • Adding BL class.
  • Adding DL class.
  • Adding Interface.
  • How to configure the Unity Container.
  • Running and debugging an application.
  • Final output.
  • Dependency Injection Pros and Cons.

Flow of Dependency Inversion Principal

Inversion Principal

Dependency Inversion Principal

The Dependency Inversion principle says that components that depend on each other should interact via an abstraction, not directly with a concrete implementation.

Example

If we have a data access layer and a business layer then they should not directly depend on each other, they should depend on an interface or abstract for object creation.

Advantages

  1. Using abstraction allows various components to be developed and changed independently of each other.
  2. And easy for testing components.

Inversion of Control (IOC)

Inversion of Control is a design principle that promotes loosely coupled layers, components, and classes by inverting the control flow of the application.

Dependency Injection (DI)

  • Dependency Injection is defined as a design pattern that allows for removing hard-coded dependencies from an application.
  • There is one major point to remember.
  • “Inversion of control is principal and Dependency Injection is implementation”
  • Now let's start with implementing Dependency Injection using the Microsoft Unity Framework.

The Microsoft Unity Framework helps us to inject external dependencies into software components. To use it in a project we just need to add a reference for the Unity Container DLLs to our project. To download it click on this link.

There are the following three types of dependencies.

  1. Constructor
  2. Setter (Properties)
  3. Method.

Creating a console application

I am creating a Console application named DIinject.

I am showing Constructor Injection with Unity Framework (which is the most widely used).

See the following snapshot.

Unity Framework

Adding Reference to Microsoft Unity Framework

Now I have added a reference for the Microsoft Unity Framework to my application.

See the following snapshot.

Microsoft Unity Framework

Adding BL class

I have added BL.cs (a Business Logic Layer) class to the application.

See the following snapshot.

Adding BL class

Adding DL class

After adding BL.cs I have added the DL class to the application (Data Access Layer).

See the following snapshot.

Adding DL class

Adding Interface

For decoupling both classes, I have added an interface with the name IProduct.

See the following snapshot.

Adding Interface

In the interface, I have declared a method with the name Insertdata().

See the following snapshot.

 Insertdata

Now the DL (Data access layer) will implement the interface IProduct.

See it in the following snapshot.

IProduct

After implementing the interface, now in the BL (Business Logic) layer, I am injecting a dependency from the Constructor.

See the following snapshot.

BL

How to configure the Unity Container

In the Program.cs add the following code to register the dependency and resolve the BL (Business Logic) layer instance as shown below. This will automatically take care of injecting the DA (Data Access) layer object into the BL (Business Logic) layer Constructor.

For that, we need to register a type and resolve an instance of the default requested type from the container.

  1. Creating a Unity Container.
    Unity Container
  2. Here I have created a Unity Container object with a named IU.
    Unity Container object
  3. Register a type.
  4. Here I registered the types that I am using for the injection.
  5. In this, I am injecting a DA (Data Access) layer into the BL (Business Logic) layer. That is why I registered both types.
  6. Register a type with specific members to be injected.
    Specific members
    RegisterType<From , To>( );
  7. From: Type that will be requested.
  8. To: Type that will actually be returned
  9. You call the RegisterType method to specify the registered type as an interface or object type and the target type you want to be returned in response to a query for that type. The target type must implement the interface, or inherit from the class, that you specify as the registered type.
  10. Resolve. (Resolve an instance of the default requested type from the container.)
    Resolve
  11. We want to resolve the BL by injecting a DL. That is why I wrote Resolve<BL>( );.
  12. Finally calling the method.
    Resolve BL

See the following snapshot containing the entire Program. cs.

Program

Running and debugging application

Now just run the application.

I show below a snapshot of running the BL during debugging that clearly shows the DL injecting.

See in the following snapshot the Data Access layer injecting using the constructor.

Running

See in the following snapshot a method of BL has been called.

Method of BL

See in the following snapshot, it is calling a method of a DA (Data Access) layer.

Calling Method of DL

See the following snapshot it is called the DA (Data Access) layer.

Data access Layer

Final output

It has finally injected dependency.

Output

Dependency Injection Pros and Cons

  1. Pros
    • Loosely coupling.
    • Increase Testability.
  2. Cons
    • Increase code complexity.
    • Complicated debugging of code.

The preceding is a simple example for developers. After this, you will find Dependency Injection to be an easy concept.


Similar Articles