-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectContainer.cs
40 lines (33 loc) · 1.35 KB
/
ObjectContainer.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
using System;
using NHibernateExample.DataAccess;
using NHibernateExample.Repositories;
using Spring.Context.Support;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
namespace NHibernateExample
{
public class ObjectContainer
{
static GenericApplicationContext context = InitializeContext();
private static GenericApplicationContext InitializeContext()
{
context = new GenericApplicationContext();
Configure(typeof(UnitOfWorkFactory), typeof(UnitOfWorkFactoryImpl));
Configure(typeof(PersistenceBroker), typeof(PersistenceBrokerImpl));
Configure(typeof(CatRepository), typeof(CatRepositoryImpl));
Configure(typeof(OwnerRepository), typeof(OwnerRepositoryImpl));
return context;
}
private static void Configure(Type objectNameType, Type implementationType)
{
var builder = ObjectDefinitionBuilder.RootObjectDefinition(new DefaultObjectDefinitionFactory(), implementationType)
.SetAutowireMode(AutoWiringMode.ByType)
.SetSingleton(true);
context.RegisterObjectDefinition(objectNameType.Name, builder.ObjectDefinition);
}
public static T Get<T>() where T : class
{
return context.GetObject(typeof(T).Name) as T;
}
}
}