﻿using System;
using Agiil.Data;
using Agiil.Domain;
using Autofac;
using NHibernate;
using NHibernate.Cfg;

namespace Agiil.Bootstrap.Data
{
  public class NHibernateModule : Module
  {
    protected override void Load(ContainerBuilder builder)
    {
      // Configuration
      builder
        .Register((ctx, parameters) => {
          var factory = ctx.Resolve<ISessionFactoryFactory>();
          return factory.GetConfiguration();
        })
        .SingleInstance();

      // ISessionFactory
      builder
        .Register((ctx, parameters) => {
          var config = ctx.Resolve<Configuration>();
          return config.BuildSessionFactory();
        })
        .SingleInstance();

      // ISession
      builder
        .Register((ctx, parameters) => {
          var factory = ctx.Resolve<ISessionFactory>();
          return factory.OpenSession();
        })
        .InstancePerMatchingLifetimeScope(ComponentScope.ApplicationConnection);
    }
  }
}

﻿using System;
using Agiil.Data;
using Agiil.Domain;
using Autofac;
using NHibernate;
using NHibernate.Cfg;

namespace Agiil.Bootstrap.Data
{
  public class NHibernateModule : Module
  {
    protected override void Load(ContainerBuilder builder)
    {
      builder
        .Register(BuildNHibernateConfiguration)
        .SingleInstance();

      builder
        .Register(BuildSessionFactory)
        .SingleInstance();

      builder
        .Register(BuildSession)
        .InstancePerMatchingLifetimeScope(ComponentScope.ApplicationConnection);
    }

    Configuration BuildNHibernateConfiguration(IComponentContext ctx)
    {
      var factory = ctx.Resolve<ISessionFactoryFactory>();
      return factory.GetConfiguration();
    }

    ISessionFactory BuildSessionFactory(IComponentContext ctx)
    {
      var config = ctx.Resolve<Configuration>();
      return config.BuildSessionFactory();
    }

    ISession BuildSession(IComponentContext ctx)
    {
      var factory = ctx.Resolve<ISessionFactory>();
      return factory.OpenSession();
    }
  }
}

Refactor NHibernate module for clarity
Refactor NHibernate module for clarity
