Skip to content

Instantly share code, notes, and snippets.

@kstenson
Created July 3, 2012 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kstenson/3040253 to your computer and use it in GitHub Desktop.
Save kstenson/3040253 to your computer and use it in GitHub Desktop.
Using modules with autofac
builder.RegisterModule(new DataModule()
{
AssemblyMapper = typeof(AcquireConfig).Assembly,
BuildSchema = true,
ConnectionString = Properties.Settings.Default.DataAcquisitionStoreConnection,
Web = false
});
public class DataModule : Module
{
public Assembly AssemblyMapper { get; set; }
public string ConnectionString { get; set; }
public bool Web { get; set; }
public bool BuildSchema { get; set; }
protected override void Load(ContainerBuilder builder)
{
builder.RegisterInstance(SessionFactory.CreateSessionFactory(AssemblyMapper, ConnectionString, BuildSchema)).As<ISessionFactory>().SingleInstance();
if (Web)
{
builder.Register(x => x.Resolve<ISessionFactory>().OpenSession()).As<ISession>().InstancePerHttpRequest();
}
else
{
builder.Register(x => x.Resolve<ISessionFactory>().OpenSession()).As<ISession>().InstancePerLifetimeScope();
}
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment