Unity Container implementation in MVC Application
I was reading about the dependency injection and unity container
for a while but never get a chance to implement it in real project. Luckily, I
got a task to design a small audit application and I thought to use the dependency
injection design pattern and unity container to inject the dependencies in Lazy
binding manner.
I just wanted to share my work with you guys so that you can
also take the advantages of unity container. This post will give you the idea
how to use the unity container in your future MVC project. Also, I would like
to mention that I used the property injection in the example.
Lets get started
Step 1 – First thing first – prerequisite
·
VS 2015
·
Unity.MVC (Get it from nuget. I used 5.0.3
version)
Step 2 – Create a MVC application
Step 3 – Install the Unity.MVC using nugget. After the
installation you will see below files get added in project reference
And 2 files in App_Start folder
Step 4 – Create an interface. In my case, I exposed 3
abstract method
public interface IAction
{
bool SaveData(OperationTypes oprType, object uObject);
bool UpdateData(OperationTypes oprType, object objLookup);
bool GetData(OperationTypes oprType);
}
Step 5 – Create a concrete class (Derived class). Give the method
declaration per your need.
public class LoadStatusConcrete : IAction
{
bool IAction.GetData(OperationTypes oprType)
{
throw new NotImplementedException();
}
bool IAction.SaveData(OperationTypes oprType, object uObject)
{
throw new NotImplementedException();
}
bool IAction.UpdateData(OperationTypes oprType, object objLookup)
{
throw new NotImplementedException();
}
}
Step 6 – If your plan is to use my code then you need the
Enum class as well. Otherwise it will complain
public enum OperationTypes
{
User,
File,
Process
}
Step 7 –We have created all the primary building blocks. We now
need to work on the code that is actually going to use these objects. I will
first start with Controller file.
Add a new controller to your project and create a property of
IAction interface. I am creating the object based on the condition they why I using
the “Dependency” attribute and passing the criteria.
Step 8 – Changes to the UnityConfig.cs file. Inside the
RegisterTypes method copy the below lines
container.RegisterType<IAction, LookupConcrete>("Lookup");
container.RegisterType<IAction, LoadStatusConcrete>("Load");
Just in case unity variables are not predefined. Add below
line of code inside the UniftConfig class.
#region Unity Container
private static Lazy<IUnityContainer> container =
new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
///
/// Configured Unity
Container.
///
public static IUnityContainer Container =>
container.Value;
#endregion
Step 9 – Build and run your MVC application. There are many
more options that unity container provide and many different ways you can use
it. But I found property injection straight forward and easy to use.



Comments