/
EndpointConfig.cs
72 lines (62 loc) · 3.34 KB
/
EndpointConfig.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Reflection;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using log4net.Appender;
using log4net.Core;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using NServiceBus;
using NServiceBus.ObjectBuilder;
using StructureMap;
namespace NHibernateSample.PersisterService
{
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
public void Init()
{
Configure.With()
.Log4Net<ConsoleAppender>(a=>a.Threshold = Level.Debug)
.StructureMapBuilder()
.XmlSerializer();
ISessionFactory sessionFactory = ConfigureSessionFactory();
Configure.Instance.Configurer.RegisterSingleton(typeof(ISessionFactory), sessionFactory);
//the repository is used for "method 2 and 3"
Configure.Instance.Configurer.ConfigureComponent<SomeRepository>(ComponentCallModelEnum.Singlecall);
//metod 3 uses the thread static cache mode of the container(structuremap in this case) to do the work for us
ObjectFactory.Configure(x=>
x.For<ISession>()
.LifecycleIs(new NServiceBusThreadLocalStorageLifestyle())
.Use(ctx =>ctx.GetInstance<ISessionFactory>().OpenSession()));
}
private static ISessionFactory ConfigureSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c =>
{
c.Database(
"NServiceBus.NHibernateSample");
c.Server("localhost\\sqlexpress");
c.TrustedConnection();
}
))
.Mappings(m => m.AutoMappings.Add(() =>
{
var model = new AutoPersistenceModel();
model.AddEntityAssembly(Assembly.GetExecutingAssembly())
.Where(t => t.Namespace.EndsWith("Entities"));
return model;
}
))
.ExposeConfiguration(c =>
{
c.SetProperty("current_session_context_class",
"thread_static");
c.SetProperty("proxyfactory.factory_class",
"NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu");
new SchemaUpdate(c).Execute(true, true);
})
.BuildSessionFactory();
}
}
}