-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
PrismAutofacApplication.cs
271 lines (247 loc) · 10.7 KB
/
PrismAutofacApplication.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Globalization;
using Windows.UI.Xaml;
using Autofac;
using Autofac.Features.ResolveAnything;
using Microsoft.Practices.ServiceLocation;
using Prism.Events;
using Prism.Logging;
using Prism.Windows;
using Prism.Windows.AppModel;
using Prism.Windows.Navigation;
namespace Prism.Autofac.Windows
{
/// <summary>
/// Provides the base class for the Windows Store Application object which
/// includes the automatic creation and wiring of the Autofac container and
/// the bootstrapping process for Prism services in the container.
/// </summary>
public abstract class PrismAutofacApplication : PrismApplication, IDisposable
{
/// <summary>
/// Allow strongly typed access to the Application as a global
/// </summary>
public static new PrismAutofacApplication Current => (PrismAutofacApplication)Application.Current;
/// <summary>
/// Get the IoC Autofac Container
/// </summary>
public IContainer Container { get; private set; }
/// <summary>
/// Implements and seals the Resolves method to be handled by the Autofac Container.
/// Use the container to resolve types (e.g. ViewModels and Flyouts) so their dependencies get injected
/// </summary>
/// <param name="type">The type.</param>
/// <returns>A concrete instance of the specified type.</returns>
protected override sealed object Resolve(Type type)
{
return Container.Resolve(type);
}
/// <summary>
/// Creates the <see cref="ContainerBuilder"/> that will be used to create the default container.
/// </summary>
/// <returns>A new instance of <see cref="ContainerBuilder"/>.</returns>
protected virtual ContainerBuilder CreateContainerBuilder()
{
return new ContainerBuilder();
}
/// <summary>
/// Creates the <see cref="IContainer"/> that will be used as the default container.
/// For optimal performance, configuration should be completed before creating the container.
/// </summary>
/// <returns>A new instance of <see cref="IContainer"/>.</returns>
protected virtual IContainer CreateContainer(ContainerBuilder containerBuilder)
{
return containerBuilder.Build();
}
/// <summary>
/// Configures Prism services in the container
/// </summary>
/// <param name="builder">The ContainerBuilder instance that is used to</param>
protected virtual void ConfigureContainer(ContainerBuilder builder)
{
Logger.Log("Registering Prism services with container", Category.Debug, Priority.Low);
builder.RegisterInstance<ILoggerFacade>(Logger);
RegisterTypeIfMissing<ISessionStateService, SessionStateService>(builder, true);
RegisterTypeIfMissing<IDeviceGestureService, DeviceGestureService>(builder, true);
RegisterTypeIfMissing<IEventAggregator, EventAggregator>(builder, true);
}
/// <summary>
/// Creates the nav service through the base class and gets it registered with the container
/// </summary>
/// <param name="rootFrame">The frame where nav happens</param>
/// <param name="sessionStateService">The session state service that stores nav state on suspend.</param>
/// <returns>The NavigationService instance</returns>
protected override INavigationService CreateNavigationService(IFrameFacade rootFrame, ISessionStateService sessionStateService)
{
var svc = base.CreateNavigationService(rootFrame, sessionStateService);
RegisterInstance(svc, typeof(INavigationService), registerAsSingleton: true);
return svc;
}
/// <summary>
/// Creates the DeviceGestureService through the container
/// </summary>
/// <returns>DeviceGestureService</returns>
protected override IDeviceGestureService OnCreateDeviceGestureService()
{
var svc = Container.Resolve<IDeviceGestureService>();
svc.UseTitleBarBackButton = true;
return svc;
}
/// <summary>
/// Creates the IEventAggregator through the container
/// </summary>
/// <returns>IEventAggregator</returns>
protected override IEventAggregator OnCreateEventAggregator()
{
return Container.Resolve<IEventAggregator>();
}
/// <summary>
/// Creates the SessionStateService through the container
/// </summary>
/// <returns>SessionStateService</returns>
protected override ISessionStateService OnCreateSessionStateService()
{
return Container.Resolve<ISessionStateService>();
}
/// <summary>
/// Creates and configures the Autofac container
/// </summary>
protected override void CreateAndConfigureContainer()
{
Logger.Log("Creating and Configuring Container", Category.Debug, Priority.Low);
ContainerBuilder builder = CreateContainerBuilder();
// Make sure any not specifically registered concrete type can resolve.
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
ConfigureContainer(builder);
Container = CreateContainer(builder);
if (Container == null)
{
throw new InvalidOperationException("Autofac container is null");
}
ConfigureServiceLocator();
}
/// <summary>
/// Sets up the ServiceLocator to use the Autofac container for any creation of types
/// </summary>
protected override void ConfigureServiceLocator()
{
var serviceLocator = new AutofacServiceLocatorAdapter(Container);
ServiceLocator.SetLocatorProvider(() => serviceLocator);
RegisterInstance(serviceLocator, typeof (IServiceLocator), registerAsSingleton: true);
}
/// <summary>
/// Registers a type in the container only if that type was not already registered.
/// </summary>
/// <typeparam name="TFrom">The interface type to register.</typeparam>
/// <typeparam name="TTarget">The type implementing the interface.</typeparam>
/// <param name="builder">The <see cref="ContainerBuilder"/> instance.</param>
/// <param name="registerAsSingleton">Registers the type as a singleton.</param>
protected void RegisterTypeIfMissing<TFrom, TTarget>(ContainerBuilder builder, bool registerAsSingleton = false)
{
if (Container != null && Container.IsRegistered<TFrom>())
{
Logger.Log(String.Format(CultureInfo.CurrentCulture, "Type {0} already registered with container", typeof(TFrom).Name),
Category.Debug, Priority.Low);
}
else
{
if (registerAsSingleton)
{
builder.RegisterType<TTarget>().As<TFrom>().SingleInstance();
}
else
{
builder.RegisterType<TTarget>().As<TFrom>();
}
}
}
/// <summary>
/// Registers a type in the container only if that type was not already registered,
/// after the container is already created.
/// Uses a new ContainerBuilder instance to update the Container.
/// </summary>
/// <param name="fromType">The interface type to register.</param>
/// <param name="toType">The type implementing the interface.</param>
/// <param name="registerAsSingleton">Registers the type as a singleton.</param>
protected void RegisterTypeIfMissing(Type fromType, Type toType, bool registerAsSingleton)
{
if (fromType == null)
{
throw new ArgumentNullException(nameof(fromType));
}
if (toType == null)
{
throw new ArgumentNullException(nameof(toType));
}
if (Container.IsRegistered(fromType))
{
Logger.Log(String.Format(CultureInfo.CurrentCulture,
"Type {0} already registered with container",
fromType.Name), Category.Debug, Priority.Low);
}
else
{
ContainerBuilder builder = CreateContainerBuilder();
if (registerAsSingleton)
{
builder.RegisterType(toType).As(fromType).SingleInstance();
}
else
{
builder.RegisterType(toType).As(fromType);
}
builder.Update(Container);
}
}
/// <summary>
/// Registers an object instance in the container after the container is already created.
/// </summary>
/// <param name="instance">Object instance.</param>
/// <param name="fromType">The interface type to register.</param>
/// <param name="key">Optional key for registration.</param>
/// <param name="registerAsSingleton">Registers the type as a singleton.</param>
protected void RegisterInstance<T>(T instance, Type fromType, string key = "", bool registerAsSingleton = false)
where T : class
{
if (instance == null)
{
throw new ArgumentNullException(nameof(instance));
}
if (fromType == null)
{
throw new ArgumentNullException(nameof(fromType));
}
ContainerBuilder containerUpdater = CreateContainerBuilder();
var registration = containerUpdater.RegisterInstance(instance);
// named instance
if (!string.IsNullOrEmpty(key))
{
registration = registration.Named(key, fromType);
}
else
{
registration = registration.As(fromType);
}
// singleton
if (registerAsSingleton)
{
registration.SingleInstance();
}
containerUpdater.Update(Container);
}
#region IDisposable
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
if (Container != null)
{
Container.Dispose();
Container = null;
}
GC.SuppressFinalize(this);
}
#endregion
}
}