HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

AutoFac, NHibernate & ASP.NET Web API integration

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
nhibernatenetautofacwebintegrationapiasp

Problem

I've recently been learning (and struggling with) integrating AutoFac, NHibernate & ASP.NET Web API. I have found several tutorials with code that did not work as anticipated but managed to finally find a working solution.

Essentially what I'm doing is registering a singleton of my SessionFactory to the WebApiApplication.SessionFactory object, registering a per-API-request ISession & IClientRepository. After these are registered, I'm creating a controller with a DI'ed IClientRepository (which in turn has an ISession injected which creates a new session as seen in the code below).

My controllers are marked with a custom TranscationActionFilterAttribute to bind (or create) a session. The issue I was initially encountering while implementing this pattern is that considering a new session is created every time IClientRepository was injected with an ISession, the session was never automatically bound and the code in my Transcation action filter would always create an extraneous session, thereby making updates and deletes not function properly. I got around this by binding the ISession in the constructor of my ClientRepository.

Is this safe?

Global.asax:

```
public class WebApiApplication : System.Web.HttpApplication
{
public static ISessionFactory SessionFactory { get; set; }

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
}

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

var configuration = GlobalConfiguration.Configuration;
var builder = new ContainerBuilder();
// Register ISessionFactory as Singleton
builder.Register(x => NHibernateConfigurator.BuildSessionFactory()).SingleInstance();

// Register API controller types using assembly scan

Solution

This is not going to answer your question directly but..


By definition I suppose that Code Review allows me to suggest and
share subjective ideas (to be determined)...

By convention, stuff that needs to be done on the application start can be located in a App_Start folder.

Here's a template for AutoFac, ~/App_Start/AutofacMvcConfig.cs:

using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using Models;
using MvcApplication.App_Start;

[assembly: WebActivator.PreApplicationStartMethod(typeof(AutofacMvcConfig), "Start")]

namespace MvcApplication.App_Start
{
    public class AutofacMvcConfig
    {
        public static void Start()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterType().As().InstancePerHttpRequest();
            builder.RegisterType().As();

            var container = builder.Build();

            // Install-Package Autofac.Mvc3
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}


Readings

  • https://stackoverflow.com/questions/5335350/unitofwork-in-action-filter-seems-to-be-caching



  • http://jittuu.com/2012/2/9/UnitOfWork-pattern-and-asp.net-mvc/



  • http://slynetblog.blogspot.ca/2012/03/using-aspnet-mvc-4-webapi-with.html



  • http://ben.onfabrik.com/posts/yet-another-session-per-request-post



Keywords: mvc, unit of work, autofac, actionfilterattribute

Code Snippets

using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using Models;
using MvcApplication.App_Start;

[assembly: WebActivator.PreApplicationStartMethod(typeof(AutofacMvcConfig), "Start")]

namespace MvcApplication.App_Start
{
    public class AutofacMvcConfig
    {
        public static void Start()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterType<TodoCommands>().As<ITodoCommands>().InstancePerHttpRequest();
            builder.RegisterType<TodoQueries>().As<ITodoQueries>();

            var container = builder.Build();

            // Install-Package Autofac.Mvc3
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}

Context

StackExchange Code Review Q#17930, answer score: 3

Revisions (0)

No revisions yet.