Created
July 3, 2012 14:58
-
-
Save kstenson/3040253 to your computer and use it in GitHub Desktop.
Using modules with autofac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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