/
TurbineApplication.cs
152 lines (124 loc) · 4.63 KB
/
TurbineApplication.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
namespace MvcTurbine.Web {
using System;
using System.Collections.Generic;
using System.Web;
using ComponentModel;
using MvcTurbine.Web.Modules;
using Properties;
using Config;
/// <summary>
/// Class that provides the simple IoC support for ASP.NET MVC.
/// </summary>
public class TurbineApplication : HttpApplication, ITurbineApplication {
private static IRotorContext rotorContext;
private static IServiceLocator serviceLocator;
/// <summary>
/// Gets or sets the current implementation of <see cref="IServiceLocator"/>
/// the application instance will use.
/// </summary>
public IServiceLocator ServiceLocator {
get { return serviceLocator; }
set { serviceLocator = value; }
}
/// <summary>
/// Gets or sets the current <see cref="RotorContext"/> for the application instance to use.
/// </summary>
public IRotorContext CurrentContext {
get { return rotorContext; }
set { rotorContext = value; }
}
/// <summary>
/// Turns the current context
/// </summary>
public void TurnRotor() {
CurrentContext = GetContext();
ExecuteContext();
}
/// <summary>
/// Sets up the engine with the specified pieces.
/// </summary>
public virtual void SetupEngine() {
Engine
.Initialize
.ConfigureWithServiceLocator(ServiceLocator);
}
/// <summary>
/// Performs any startup processing.
/// </summary>
public virtual void Startup() {
}
/// <summary>
/// Shuts down the current application.
/// </summary>
public virtual void Shutdown() {
}
/// <summary>
/// Sets up one-time only execution for the application.
/// </summary>
protected void Application_Start(object sender, EventArgs e) {
Startup();
ServiceLocator = GetServiceLocator();
PostServiceLocatorAcquisition();
SetupEngine();
TurnRotor();
}
/// <summary>
/// Executed after the <see cref="IServiceLocator"/> has been acquired.
/// </summary>
protected virtual void PostServiceLocatorAcquisition() {
}
/// <summary>
/// Initializes and execute the current <see cref="RotorContext"/>.
/// </summary>
protected virtual void ExecuteContext() {
CurrentContext.Turn();
}
/// <summary>
/// Initializes the current <see cref="HttpApplication"/>.
/// </summary>
public override void Init() {
base.Init();
if (CurrentContext == null) return;
CurrentContext.Initialize(this);
}
/// <summary>
/// Tears down, one-time only, the application.
/// </summary>
protected virtual void Application_End() {
Shutdown();
ShutdownContext();
}
/// <summary>
/// Shuts down the <see cref="CurrentContext"/> and handles all pieces of cleanup.
/// </summary>
protected virtual void ShutdownContext() {
if (CurrentContext == null) return;
CurrentContext.Dispose();
CurrentContext = null;
ServiceLocator = null;
}
/// <summary>
/// Gets the instance of <see cref="RotorContext"/> that is registered with the
/// <see cref="IServiceLocator"/>.
/// </summary>
/// <returns>The registered <see cref="RotorContext"/>, otherwise a default <see cref="RotorContext"/> is used.</returns>
protected virtual IRotorContext GetContext() {
try {
return ServiceLocator.Resolve<IRotorContext>();
} catch {
return new RotorContext(ServiceLocator);
}
}
/// <summary>
/// Gets the instance of <see cref="IServiceLocator"/> that is registered with
/// <see cref="ServiceLocatorManager.SetLocatorProvider"/>.
/// </summary>
protected virtual IServiceLocator GetServiceLocator() {
var locator = ServiceLocatorManager.Current;
if (locator == null) {
throw new InvalidOperationException(Resources.ServiceLocatorExceptionMessage);
}
return locator;
}
}
}