Dependency Injection Using Simple Injector



Simple Injectors

A Simple Injector is an easy, flexible and fast dependency injection library.

The purpose of a Simple Injector is to provide .NET application developers with an easy, flexible and fast Inversion of Control library that uses best practices to steer developers to success.

Many of the existing IoC frameworks have a big complicated legacy API or are new, immature and lack the features to be used in large development projects. A Simple Injector fills this gap by supplying a simple implementation with carefully chosen features in the core library. File and attribute based configuration methods have been abandoned (they invariably result in brittle and highly maintained applications), favoring simple code based configuration instead. This is enough for most applications, requiring only that the configuration be done at the start of the program. The core library contains many features and allows nearly any advanced scenario.

The definition of a Simple Injector is referenced from:

Simple Injector

Tools required

VS2010 / VS2010 SP1 and above.

Simple Injector Dlls.

Features

Simple Injector is freeware.
Easy to use.
Fastest.

This Simple Injector tool is the fastest among other IOC Containers such as Unity, Ninject, Autofac and the others according to research done by Daniel Palme.

For more details on performance just visit this link:

IoC Container Benchmark - Performance comparison

To learn about other tools of Dependency just read the following content and links:

If you want to learn about the Microsoft Unity framework then refer to this link:

Dependency Injection Using Microsoft Unity Framework

If you want to learn about the Ninject framework then refer to this link:

Dependency Injection Using Ninject in .NET

Agenda

  • Creating a console application

  • Download Simple Injector

  • Adding Reference of the Simple Injector Framework

  • Adding a BussinessLayer class

  • Adding a DataAccessLayer class

  • Adding an interface

  • How to configure the Simple Injector Framework

  • Running and debugging applications

  • Final output

Creating a console application

Just open Visual Studio and click on File from the Menu then select New then inside that select Project .

Then a New Dialog will pop up.

Inside Templates select Windows and from Application select Console Application .

Name the Console application “DISimpleInjector”.

See that in the following snapshot:




Download Simple Injector and Adding Reference

To download Simple Injector just visit codeplex.com or check this link:

Simple Injector

To download from NuGet.

To download from NuGet just right-click on your application then select Manage NuGet Packages.

A new dialog will pop up. That dialog is a search box; enter “Simple Injector” into it.

Install the first result Simple Injector Version 2.7.1.

See that in the following image.





After installing you will find the result part marked with a Green correct sign.

See that in the following image:



Check out the Reference that we have just now added.



Adding BussinessLayer class

I have added a BussinessLayer class to the application. See that in the following snapshot.



Adding a DataAccessLayer class

After adding a BussinessLayer.cs I have added a DataAccessLayer class to the application.

See that in the following snapshot.




Adding Interface

For decoupling both classes I have added an interface with the name ICart. See that in the following snapshot.



In the interface I have declared a method with the name AddtoCart(). See that in the following snapshot.



Now the DataAccessLayer will implement the interface ICart.

See it in the following snapshot.



After implementing the interface, now in the Bussinesslayer I am injecting a dependency from the Constructor. See that in the following snapshot.



How to configure Simple Injector Framework

In Program.cs add the following code to register the dependency and resolve the BussinessLayer instance as shown below.

This will automatically take care of injecting the DataAccessLayer object into the BussinessLayer Constructor.

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

Here you can see that I have created a container and used the register method to configure a container. I have mapped ICart to DataAccessLayer so that whenever an instance of ICart is required then it will create an object of the DataAccessLayer class.

Creating a Simple Injector Container
  1. var container = new Container();  
Register a type

Container.Register is used to register a class and interface.


  1. container.Register<ICart, DataAccessLayer>();  
In this I am passing an ICart interface as the service and DataAccessLayer as the implementation.

Get the instance:
  1. var BL = container.GetInstance<BussinessLayer>();  
Finally calling the method:
  1. var BL = container.GetInstance<BussinessLayer>();  
For calling the method we first took an instance from the Container of ICart and from that we called the method of DataAccessLayer via BussinessLayer.

See that in the following snapshot containing the entire Program.cs.

methodcallingImage

To make it a Singleton

To make it a Singleton we just need to write lifestyle.Singleton and pass it to the registering type.
  1. var lifestyle = Lifestyle.Singleton;  
  1. container.Register<ICart, DataAccessLayer>(lifestyle);  
See that in the following snapshot containing the entire Program.cs with a Singleton.



Running and debugging the application


Now just run the application.

The following snapshot shows the running of the BussinessLayer during debugging that clearly shows the DataAccessLayer injecting.

See that in the following snapshot the DataAccessLayer injecting using the constructor.





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



See that in the following snapshot, it is calling a method of a DataAccessLayer.



See that in the following snapshot it has called the DataAccessLayer.


Final Output