Dependency Inversion Principle Applied To Controllers With ASP.NET MVC and StructureMap
The Dependency Inversion Principle is one of the five Solid Principles, this one state about making class dependent of abstraction instead of concrete object thus we obtain isolated and low coupling modules and low-level implementation details.
The first time I heard about the Dependency Inversion Principle I used to confused it with the Dependency Injection Design Pattern but I was wrong because they are not the same although has relations each other.
Since the Dependency Inversion is how we call the act of a class depending from an abstraction, the Dependency Injection instead is a design pattern where by object X is supplied the dependency of another object B.
There are two ways of doing the dependency Injection, by constructors or Setters, here is an example:
DI by Setter:
public Class ObjectB
{
// Internal reference to the ObjectC used by this ObjectB.
private ObjectC obj;
// Set the objectC that this ObjectB is to use.
public void setObjectC(ObjectC obj)
{
this.obj = obj;
}
}
DI by constructor:
public Class ObjectB
{
// Internal reference to the ObjectC used by this ObjectB.
private ObjectC obj;
// Set the objectC that this ObjectB is to use.
public ObjectB(ObjectC obj)
{
this.obj = obj;
}
}
After I understood the design pattern of the dependency injection, a doubt comes to me and it was how will a program instantiate a specific object if it depends totally from an abstraction like the Dependency Inversion said and that’s how I arrived to Inversion Of Control or IoC.
The Dependency Inversion Principle use Inversion of Control to handle the policies that will handle the injection about the specific implementation we want to use on our code to make it behave the way we wants to. That is the way the Dependency Inversion works in real world app implementations.
Now and after the brief explanation we'll see the implementation of the Dependency Inversion Principle on a simple MVC app.
First we create an MVC project on Visual Studio:
Then we select the MVC template:
Then on manage Nugget package we look for StructureMap which is the IoC we gonna use to feed the dependencies.
Now we start the configurations to make our Controllers be injected by the StructureMap IoC and apply the dependency inversion principle to each of our abstractions.
Here is the portion of code used by me to make the StructureMap know the concreate instance from abstraction we’ll use in order to be added at the repository:
public class StandardRegistry : Registry
{
public StandardRegistry()
{
Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AddAllTypesOf<IInfoManager>();
scan.AddAllTypesOf<IRepository<Inform>>();
});
}
}
The next two classes are used to make the repository knows about the Controllers objects:
namespace DependencyInversionAppliedControllerMVC.DependencyResolutions
{
public class ControllerRegistrationConvention : IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
if (type.IsSubclassOf((typeof(Controller))) && !type.IsAbstract)
{
registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
}
}
public void ScanTypes(StructureMap.Graph.Scanning.TypeSet types, Registry registry)
{
}
}
}
namespace DependencyInversionAppliedControllerMVC.DependencyResolutions
{
public class ControllerRegistry : Registry
{
public ControllerRegistry()
{
Scan(scan =>
{
scan.TheCallingAssembly();
scan.With(new ControllerRegistrationConvention());
});
}
}
}
Here is the Dependency Inversion Principle implemented:
public class HomeController : Controller
{
private IRepository<Inform> _manager;
public HomeController(IRepository<Inform> manager)
{
_manager = manager;
}
[Home("Home")]
public ActionResult Index()
{
var model = _manager.Get();
return View(model);
}
[Home("About")]
public ActionResult About()
{
var model = _manager.Get();
return View(model);
}
[Home("Contact")]
public ActionResult Contact()
{
var model = _manager.Get();
return View(model);
}
}
The dependency inversion principle as one of the Solid 5 principles is really useful to develop isolated and low-coupled applications that help us with the maintenance and future changes to the software. The programs gain more independency and flexibility from the others modules while it depends on abstraction instead of concreate objects.
Here I let you the link of the example that run with de Structure map IoC.
Link: https://github.com/edgarleonardo/DependencyInversionAppliedControllerMVC
The Solid principles helps us to code more friendly software while makes our life easier minimizing the work we have to do when the days of changes comes through. Applying those principles help us coding more efficient and cleaner.
Thanks for reading,
The first time I heard about the Dependency Inversion Principle I used to confused it with the Dependency Injection Design Pattern but I was wrong because they are not the same although has relations each other.
Since the Dependency Inversion is how we call the act of a class depending from an abstraction, the Dependency Injection instead is a design pattern where by object X is supplied the dependency of another object B.
There are two ways of doing the dependency Injection, by constructors or Setters, here is an example:
DI by Setter:
public Class ObjectB
{
// Internal reference to the ObjectC used by this ObjectB.
private ObjectC obj;
// Set the objectC that this ObjectB is to use.
public void setObjectC(ObjectC obj)
{
this.obj = obj;
}
}
DI by constructor:
public Class ObjectB
{
// Internal reference to the ObjectC used by this ObjectB.
private ObjectC obj;
// Set the objectC that this ObjectB is to use.
public ObjectB(ObjectC obj)
{
this.obj = obj;
}
}
After I understood the design pattern of the dependency injection, a doubt comes to me and it was how will a program instantiate a specific object if it depends totally from an abstraction like the Dependency Inversion said and that’s how I arrived to Inversion Of Control or IoC.
The Dependency Inversion Principle use Inversion of Control to handle the policies that will handle the injection about the specific implementation we want to use on our code to make it behave the way we wants to. That is the way the Dependency Inversion works in real world app implementations.
Now and after the brief explanation we'll see the implementation of the Dependency Inversion Principle on a simple MVC app.
First we create an MVC project on Visual Studio:
Then we select the MVC template:
Then on manage Nugget package we look for StructureMap which is the IoC we gonna use to feed the dependencies.
Now we start the configurations to make our Controllers be injected by the StructureMap IoC and apply the dependency inversion principle to each of our abstractions.
Here is the portion of code used by me to make the StructureMap know the concreate instance from abstraction we’ll use in order to be added at the repository:
public class StandardRegistry : Registry
{
public StandardRegistry()
{
Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AddAllTypesOf<IInfoManager>();
scan.AddAllTypesOf<IRepository<Inform>>();
});
}
}
The next two classes are used to make the repository knows about the Controllers objects:
namespace DependencyInversionAppliedControllerMVC.DependencyResolutions
{
public class ControllerRegistrationConvention : IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
if (type.IsSubclassOf((typeof(Controller))) && !type.IsAbstract)
{
registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
}
}
public void ScanTypes(StructureMap.Graph.Scanning.TypeSet types, Registry registry)
{
}
}
}
namespace DependencyInversionAppliedControllerMVC.DependencyResolutions
{
public class ControllerRegistry : Registry
{
public ControllerRegistry()
{
Scan(scan =>
{
scan.TheCallingAssembly();
scan.With(new ControllerRegistrationConvention());
});
}
}
}
Here is the Dependency Inversion Principle implemented:
public class HomeController : Controller
{
private IRepository<Inform> _manager;
public HomeController(IRepository<Inform> manager)
{
_manager = manager;
}
[Home("Home")]
public ActionResult Index()
{
var model = _manager.Get();
return View(model);
}
[Home("About")]
public ActionResult About()
{
var model = _manager.Get();
return View(model);
}
[Home("Contact")]
public ActionResult Contact()
{
var model = _manager.Get();
return View(model);
}
}
The dependency inversion principle as one of the Solid 5 principles is really useful to develop isolated and low-coupled applications that help us with the maintenance and future changes to the software. The programs gain more independency and flexibility from the others modules while it depends on abstraction instead of concreate objects.
Here I let you the link of the example that run with de Structure map IoC.
Link: https://github.com/edgarleonardo/DependencyInversionAppliedControllerMVC
The Solid principles helps us to code more friendly software while makes our life easier minimizing the work we have to do when the days of changes comes through. Applying those principles help us coding more efficient and cleaner.
Thanks for reading,
Comments
Post a Comment