-
Notifications
You must be signed in to change notification settings - Fork 55
/
UnityConfig.cs
61 lines (56 loc) · 2.63 KB
/
UnityConfig.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
using System;
using System.Collections.Generic;
using System.Data.Entity;
using CommonServiceLocator;
using Microsoft.Practices.Unity;
using Northwind.Entities.Models;
using Northwind.Service;
using Repository.Pattern.DataContext;
using Repository.Pattern.Ef6;
using Repository.Pattern.Repositories;
using Repository.Pattern.UnitOfWork;
namespace Northwind.Web
{
/// <summary>
/// Specifies the Unity configuration for the main container.
/// </summary>
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
/// <summary>
/// Gets the configured Unity container.
/// </summary>
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion
/// <summary>Registers the type mappings with the Unity container.</summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
/// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
// container.LoadConfiguration();
container
// Register DbContext instead of IDataDataContext, which is now obsolete.
//.RegisterType<IDataContextAsync, NorthwindContext>(new PerRequestLifetimeManager())
.RegisterType<DbContext, NorthwindContext>(new PerRequestLifetimeManager())
.RegisterType<IUnitOfWorkAsync, UnitOfWork>(new PerRequestLifetimeManager())
.RegisterType<IRepositoryAsync<Customer>, Repository<Customer>>()
.RegisterType<IRepositoryAsync<Product>, Repository<Product>>()
.RegisterType<IProductService, ProductService>()
.RegisterType<ICustomerService, CustomerService>()
.RegisterType<INorthwindStoredProcedures, NorthwindContext>(new PerRequestLifetimeManager())
.RegisterType<IStoredProcedureService, StoredProcedureService>();
ServiceLocator.SetLocatorProvider(() => new UnityServiceLocatorAdapter(container));
}
}
}