-
Notifications
You must be signed in to change notification settings - Fork 1
/
Global.asax.cs
248 lines (222 loc) · 11.5 KB
/
Global.asax.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// /*
// Copyright (c) 2015 Andrew Newton (http://www.nootn.com.au)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// */
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Autofac;
using Autofac.Builder;
using Autofac.Integration.Mvc;
using AutofacContrib.DynamicProxy;
using DotNetAppStarterKit.Core.Command;
using DotNetAppStarterKit.Core.EventBroker;
using DotNetAppStarterKit.Core.Mapping;
using DotNetAppStarterKit.Core.Query;
using DotNetAppStarterKit.Mapping;
using DotNetAppStarterKit.SampleMvc.Application;
using DotNetAppStarterKit.SampleMvc.Application.Interceptors;
using DotNetAppStarterKit.SampleMvc.Application.Mapping;
using DotNetAppStarterKit.SampleMvc.DataProject.Context;
using DotNetAppStarterKit.SampleMvc.DataProject.Event;
using DotNetAppStarterKit.Web.Caching;
using DotNetAppStarterKit.Web.Logging;
using DotNetAppStarterKit.Web.Security;
namespace DotNetAppStarterKit.SampleMvc
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : HttpApplication
{
public static AutofacDependencyResolver GlobalResolver { get; private set; }
protected void Application_Start()
{
//General MVC setup
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//Configure EF
if (Database.Exists("DummyDataContext"))
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DummyDataContext>());
}
//IOC Setup - example using AutoFac
ConfigureIoc();
//Ensure all our Mappers are OK
MappingSetup.AssertConfigurationIsValidInAllMappers(new MvcInstanceCreator());
}
private static void ConfigureIoc()
{
var builder = new ContainerBuilder();
//Web components
var webAssembly = typeof (MvcApplication).Assembly;
builder.RegisterControllers(webAssembly);
builder.RegisterFilterProvider();
builder.RegisterModule(new AutofacWebTypesModule());
//Components defined within this website
builder.RegisterType<MiniProfilerAndLoggerInterceptor>().AsSelf();
builder.RegisterType<DummyDataContext>().AsImplementedInterfaces().InstancePerHttpRequest();
//DotNetAppStarterKit components
builder.RegisterType<User>().AsImplementedInterfaces().InstancePerHttpRequest();
builder.RegisterGeneric(typeof (WebCacheProvider<>)).AsImplementedInterfaces().InstancePerHttpRequest();
builder.RegisterGeneric(typeof (TraceWithElmahErrorsAndCallerInfoLog<>))
.AsImplementedInterfaces()
.InstancePerHttpRequest();
RegisterGenericTypes(builder, webAssembly, typeof (ICommand<,>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
RegisterGenericTypes(builder, webAssembly, typeof (ICommand<>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
RegisterGenericTypes(builder, webAssembly, typeof (IAsyncCommand<,>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
RegisterGenericTypes(builder, webAssembly, typeof (IAsyncCommand<>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
RegisterGenericTypes(builder, webAssembly, typeof (IQuery<,>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
RegisterGenericTypes(builder, webAssembly, typeof (IQuery<>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
RegisterGenericTypes(builder, webAssembly, typeof (IAsyncQuery<,>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
RegisterGenericTypes(builder, webAssembly, typeof (IAsyncQuery<>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
RegisterGenericTypes(builder, webAssembly, typeof (ICachedQuery<,>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
RegisterGenericTypes(builder, webAssembly, typeof (ICachedQuery<>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
// Register Events
var eventsAssembly = typeof (ThingyChangedEvent).Assembly;
RegisterGenericTypes(builder, eventsAssembly, typeof (IHandle<>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
builder.RegisterType<DependencyResolverEventBroker>()
.AsImplementedInterfaces()
.AutoActivate()
.OnActivated(c => DomainEvents.SetEventBroker(c.Instance))
.SingleInstance();
//The mappers are interesting..
//This one is for the actual usages of the mappers
RegisterGenericTypes(builder, webAssembly, typeof (IMapper<,>), true)
.ForEach(
_ =>
_.InstancePerHttpRequest()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (MiniProfilerAndLoggerInterceptor)));
//This one is needed to get an instance of each mapper in the "MvcInstanceCreator"
RegisterGenericTypes(builder, webAssembly, typeof (MapperBase<,>), false)
.ForEach(
_ =>
_.InstancePerHttpRequest());
builder.RegisterType<LifetimeScopeAwareTaskFactory>()
.AsImplementedInterfaces()
.InstancePerHttpRequest();
//Build and set resolver
try
{
var container = builder.Build();
GlobalResolver = new AutofacDependencyResolver(container);
DependencyResolver.SetResolver(GlobalResolver);
}
catch (Exception ex)
{
if (ex is ReflectionTypeLoadException)
{
var typeLoadException = ex as ReflectionTypeLoadException;
var loaderExceptions = typeLoadException.LoaderExceptions;
throw new AggregateException(typeLoadException.Message, loaderExceptions);
}
throw;
}
}
private static bool IsAssignableToGenericType(Type givenType, Type genericType)
{
var interfaceTypes = givenType.GetInterfaces();
if (interfaceTypes.Where(it => it.IsGenericType).Any(it => it.GetGenericTypeDefinition() == genericType))
return true;
var baseType = givenType.BaseType;
if (baseType == null) return false;
return baseType.IsGenericType &&
baseType.GetGenericTypeDefinition() == genericType ||
IsAssignableToGenericType(baseType, genericType);
}
private static List<IRegistrationBuilder<object, object, object>> RegisterGenericTypes(ContainerBuilder builder,
Assembly parentAssembly,
Type
genericRepositoryType,
bool asInterfaces)
{
var types = parentAssembly.GetExportedTypes()
.Where(t => !t.IsInterface && !t.IsAbstract)
.Where(t => IsAssignableToGenericType(t, genericRepositoryType))
.ToArray();
var res = new List<IRegistrationBuilder<object, object, object>>();
foreach (var type in types)
{
if (type.IsGenericType)
res.Add(asInterfaces
? builder.RegisterGeneric(type).AsImplementedInterfaces()
: builder.RegisterGeneric(type).AsSelf());
else
res.Add(asInterfaces
? builder.RegisterType(type).AsImplementedInterfaces()
: builder.RegisterType(type).AsSelf());
}
return res;
}
}
}