Setting up StructureMap and The Unit Of Work ActionFilter
In my previous
I had created the repository implementations along with unit of work.
But how do we use this unit of work in our application. ASP.NET MVC has
a concept of ActionFilters. These could be applied to an action or a
controller in general. The two main methods that concern us are
OnActionExecuting and the OnActionExecuted. As seen in the code
below we call the initialize method of the unit of work which would
create a new transaction for us and this is held until the action is
complete after which we commit the transaction. We are getting a new
instance of _unitOfWork through an IoC container which I will talk
about in a bit.
We use this action filter either by decoration individual action with
the attribute or if we want all actions to use the unit of work, then we
can decorate the class itself with the attribute. In the following code
I am using the UseUnitOfWork filter on the Index action.
Attributes may look a bit intrusive (atleast for me). I like how
FubuMVC handles this. In
FubuMVC, we have something called
behaviors and this can be configured in the application start. So if we
want all our actions to use the unit of work, then we can set the
behavior as shown below and that’s it, no more attributes on the action
or controller. In the following code the
access_the_database_through_a_unit_of_work has the same
implementation as the filter we created.
Instead of accessing the repository directly from the controllers, I
have created a separate service which delegates the work to the
repository to the operations related. Following is an example of a blog
service. I have kept the methods to be minimal for brevity.
Dependency Injection using StructureMap
If we look at the all the code that has been shown till now, we see
that, there is almost always an interface declared and we can nowhere
see a direct instantiation of the object that is required. So how do we
get a new instance of an object. This is where an Inversion Of Control
(IoC/DI) container comes into play. Basically what it does is to give
you a concrete objects whenever we require.
I have used constructor injection where ever possible. How it works is,
by defining our own ControllerFactory and create the controller
ourselves instead of ASP.NET MVC doing it for us. In the below code I
have a StrucutreMapControllerFactory from where I am getting an instance
of the controller. This factory inherits from the
DefaultControllerFactory for ASP.NET MVC and we override the controller
We set this controller factory in our Application_Start method in the
global.asax file like so.
Now that we have controllerfactory ready, we can configure our
dependencies. The way to configure our dependency is through a registry
which provides a fluent interface. The following code shows all the
dependencies being loaded into the container through the
Once this is done whenever we ask for a instance of an object, the IoC
toll will lookup and give us the concerned concrete object.