/Code/Source/Teaching.Wpf/AutofacBootstrapper.cs
C# | 113 lines | 92 code | 21 blank | 0 comment | 6 complexity | db573620b5efcc3807c7ff77083adfb2 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using Autofac;
- using Caliburn.Micro;
- using NLog.Config;
- using NLog.Targets;
- using Teaching.Wpf.Controls;
- using Teaching.Wpf.Diagnostics;
-
- namespace Teaching.Wpf
- {
- public class AutofacBootstrapper<TRootViewModel> : Bootstrapper<TRootViewModel>
- {
- private ILifetimeScope rootLifetimeScope;
-
- protected override void Configure()
- {
- if (!Debugger.IsAttached)
- {
- Application.DispatcherUnhandledException += NBug.Handler.DispatcherUnhandledException;
- AppDomain.CurrentDomain.UnhandledException += NBug.Handler.UnhandledException;
- TaskScheduler.UnobservedTaskException += NBug.Handler.UnobservedTaskException;
- }
-
- var modules =
- AssemblySource.Instance
- .SelectMany(a => a.GetExportedTypes())
- .Where(t => typeof (Autofac.Core.IModule).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract)
- .Select(Activator.CreateInstance).Cast<Autofac.Core.IModule>();
-
- var containerBuilder = new ContainerBuilder();
-
- foreach( var module in modules )
- {
- containerBuilder.RegisterModule(module);
- }
-
- containerBuilder.Register(c => new WindowManager()).As<IWindowManager>().SingleInstance();
- containerBuilder.Register(c => new EventAggregator()).As<IEventAggregator>().SingleInstance();
- containerBuilder.Register(c => new CommonDialogService()).As<ICommonDialogService>().SingleInstance();
-
- rootLifetimeScope = containerBuilder.Build();
- }
-
- protected override object GetInstance(Type serviceType, string key)
- {
- if ( !string.IsNullOrWhiteSpace(key))
- {
- object result = null;
- if (rootLifetimeScope.TryResolveNamed(key, serviceType,out result))
- {
- return result;
- }
- }
-
- return rootLifetimeScope.Resolve(serviceType);
- }
-
- protected override IEnumerable<object> GetAllInstances(Type serviceType)
- {
- var type = typeof(IEnumerable<>).MakeGenericType(new[] { serviceType });
- return (IEnumerable<object>) rootLifetimeScope.Resolve(type);
- }
-
- protected override void BuildUp(object instance)
- {
- rootLifetimeScope.InjectProperties(instance);
- }
-
- protected override void OnExit(object sender, EventArgs e)
- {
- rootLifetimeScope.Dispose();
- }
-
- public AutofacBootstrapper()
- {
- const string loggingLayout = "${level}: ${message} ${exception} at ${date}";
-
- var loggingConfiguration = new LoggingConfiguration();
-
- var fileTarget = new FileTarget
- {
- EnableFileDelete = true,
- CreateDirs = true,
- FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Assembly.GetEntryAssembly().GetAssemblyName() + ".log"),
- Layout = loggingLayout
- };
-
- loggingConfiguration.AddTarget("file",fileTarget);
-
- var fileTargetRule = new LoggingRule("*", NLog.LogLevel.Debug, fileTarget);
- loggingConfiguration.LoggingRules.Add(fileTargetRule);
-
- if ( Debugger.IsAttached )
- {
- var debuggerTarget = new DebuggerTarget {Layout = loggingLayout};
- loggingConfiguration.AddTarget("debugger",debuggerTarget);
-
- var debuggerTargetRule = new LoggingRule("*", NLog.LogLevel.Debug, debuggerTarget);
- loggingConfiguration.LoggingRules.Add(debuggerTargetRule);
- }
-
- NLog.LogManager.Configuration = loggingConfiguration;
-
- LogManager.GetLog = type => new NLogAdapter(NLog.LogManager.GetLogger(type.Name));
- }
- }
- }