-
Notifications
You must be signed in to change notification settings - Fork 6
/
AbstractBootStrapper.cs
113 lines (87 loc) · 4.69 KB
/
AbstractBootStrapper.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using Autofac;
using Mozu.Api.Contracts.AppDev;
using Mozu.Api.Events;
using Mozu.Api.Logging;
using Mozu.Api.Security;
using Mozu.Api.ToolKit.Config;
using Mozu.Api.ToolKit.Events;
using Mozu.Api.ToolKit.Handlers;
using Mozu.Api.ToolKit.Logging;
namespace Mozu.Api.ToolKit
{
public abstract class AbstractBootstrapper
{
protected readonly ContainerBuilder _containerBuilder = new ContainerBuilder();
public IContainer Container;
public AbstractBootstrapper Bootstrap()
{
InitDependencyResolvers();
return this;
}
private void InitDependencyResolvers()
{
var appName = ConfigurationManager.AppSettings["AppName"];
var configPath = ConfigurationManager.AppSettings["ConfigPath"];
var environment = ConfigurationManager.AppSettings["Environment"];
if (!string.IsNullOrEmpty(appName) && !string.IsNullOrEmpty(configPath) &&
!String.IsNullOrEmpty(environment))
{
var appParams = new List<NamedParameter>
{
new NamedParameter("configPath", configPath),
new NamedParameter("appName", appName),
new NamedParameter("environment", environment)
};
_containerBuilder.RegisterType<AppSetting>().As<IAppSetting>().SingleInstance().WithParameters(appParams);
}
else
{
_containerBuilder.RegisterType<AppSetting>().As<IAppSetting>().SingleInstance();
}
_containerBuilder.RegisterType<Log4NetServiceFactory>().As<ILoggingServiceFactory>().SingleInstance();
if (ConfigurationManager.AppSettings.AllKeys.Contains("UseGenericEventService") &&
bool.Parse(ConfigurationManager.AppSettings["UseGenericEventService"]))
_containerBuilder.RegisterType<Events.GenericEventService>().As<IEventService>();
else
_containerBuilder.RegisterType<Events.EventService>().As<IEventService>();
_containerBuilder.RegisterType<Events.EventServiceFactory>().As<IEventServiceFactory>();
_containerBuilder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where(t => t.Name.EndsWith("Handler"))
.AsImplementedInterfaces();
_containerBuilder.RegisterType<EntitySchemaHandler>().As<IEntitySchemaHandler>();
_containerBuilder.RegisterType<ReturnEventProcessor>().Keyed<IEventProcessor>(EventCategory.Return);
_containerBuilder.RegisterType<ProductEventProcessor>().Keyed<IEventProcessor>(EventCategory.Product);
_containerBuilder.RegisterType<OrderEventProcessor>().Keyed<IEventProcessor>(EventCategory.Order);
_containerBuilder.RegisterType<ApplicationEventProcessor>().Keyed<IEventProcessor>(EventCategory.Application);
_containerBuilder.RegisterType<CustomerAccountEventProcessor>().Keyed<IEventProcessor>(EventCategory.CustomerAccount);
_containerBuilder.RegisterType<DiscountEventProcessor>().Keyed<IEventProcessor>(EventCategory.Discount);
_containerBuilder.RegisterType<CustomerSegmentEventProcessor>().Keyed<IEventProcessor>(EventCategory.CustomerSegment);
_containerBuilder.RegisterType<TenantEventProcessor>().Keyed<IEventProcessor>(EventCategory.Tenant);
_containerBuilder.RegisterType<EmailEventProcessor>().Keyed<IEventProcessor>(EventCategory.Email);
InitializeContainer(_containerBuilder);
Container = _containerBuilder.Build();
LogManager.LoggingService = Container.Resolve<ILoggingServiceFactory>().GetLoggingService();
var appSetting = Container.Resolve<IAppSetting>();
if (!string.IsNullOrEmpty(appSetting.ApplicationId) && !string.IsNullOrEmpty(appSetting.SharedSecret))
{
if (!string.IsNullOrEmpty(appSetting.BaseUrl))
MozuConfig.BaseAppAuthUrl = appSetting.BaseUrl;
if (!string.IsNullOrEmpty(appSetting.BasePCIUrl))
MozuConfig.BasePciUrl = appSetting.BasePCIUrl;
var appAuthenticator = AppAuthenticator.InitializeAsync(new AppAuthInfo { ApplicationId = appSetting.ApplicationId, SharedSecret = appSetting.SharedSecret }).Result;
}
PostInitialize();
}
public virtual void InitializeContainer(ContainerBuilder containerBuilder)
{
}
public virtual void PostInitialize()
{
}
}
}