PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Code/Source/Teaching.Wpf/AutofacBootstrapper.cs

https://bitbucket.org/BernhardGlueck/teaching
C# | 113 lines | 92 code | 21 blank | 0 comment | 6 complexity | db573620b5efcc3807c7ff77083adfb2 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8. using Autofac;
  9. using Caliburn.Micro;
  10. using NLog.Config;
  11. using NLog.Targets;
  12. using Teaching.Wpf.Controls;
  13. using Teaching.Wpf.Diagnostics;
  14. namespace Teaching.Wpf
  15. {
  16. public class AutofacBootstrapper<TRootViewModel> : Bootstrapper<TRootViewModel>
  17. {
  18. private ILifetimeScope rootLifetimeScope;
  19. protected override void Configure()
  20. {
  21. if (!Debugger.IsAttached)
  22. {
  23. Application.DispatcherUnhandledException += NBug.Handler.DispatcherUnhandledException;
  24. AppDomain.CurrentDomain.UnhandledException += NBug.Handler.UnhandledException;
  25. TaskScheduler.UnobservedTaskException += NBug.Handler.UnobservedTaskException;
  26. }
  27. var modules =
  28. AssemblySource.Instance
  29. .SelectMany(a => a.GetExportedTypes())
  30. .Where(t => typeof (Autofac.Core.IModule).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract)
  31. .Select(Activator.CreateInstance).Cast<Autofac.Core.IModule>();
  32. var containerBuilder = new ContainerBuilder();
  33. foreach( var module in modules )
  34. {
  35. containerBuilder.RegisterModule(module);
  36. }
  37. containerBuilder.Register(c => new WindowManager()).As<IWindowManager>().SingleInstance();
  38. containerBuilder.Register(c => new EventAggregator()).As<IEventAggregator>().SingleInstance();
  39. containerBuilder.Register(c => new CommonDialogService()).As<ICommonDialogService>().SingleInstance();
  40. rootLifetimeScope = containerBuilder.Build();
  41. }
  42. protected override object GetInstance(Type serviceType, string key)
  43. {
  44. if ( !string.IsNullOrWhiteSpace(key))
  45. {
  46. object result = null;
  47. if (rootLifetimeScope.TryResolveNamed(key, serviceType,out result))
  48. {
  49. return result;
  50. }
  51. }
  52. return rootLifetimeScope.Resolve(serviceType);
  53. }
  54. protected override IEnumerable<object> GetAllInstances(Type serviceType)
  55. {
  56. var type = typeof(IEnumerable<>).MakeGenericType(new[] { serviceType });
  57. return (IEnumerable<object>) rootLifetimeScope.Resolve(type);
  58. }
  59. protected override void BuildUp(object instance)
  60. {
  61. rootLifetimeScope.InjectProperties(instance);
  62. }
  63. protected override void OnExit(object sender, EventArgs e)
  64. {
  65. rootLifetimeScope.Dispose();
  66. }
  67. public AutofacBootstrapper()
  68. {
  69. const string loggingLayout = "${level}: ${message} ${exception} at ${date}";
  70. var loggingConfiguration = new LoggingConfiguration();
  71. var fileTarget = new FileTarget
  72. {
  73. EnableFileDelete = true,
  74. CreateDirs = true,
  75. FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Assembly.GetEntryAssembly().GetAssemblyName() + ".log"),
  76. Layout = loggingLayout
  77. };
  78. loggingConfiguration.AddTarget("file",fileTarget);
  79. var fileTargetRule = new LoggingRule("*", NLog.LogLevel.Debug, fileTarget);
  80. loggingConfiguration.LoggingRules.Add(fileTargetRule);
  81. if ( Debugger.IsAttached )
  82. {
  83. var debuggerTarget = new DebuggerTarget {Layout = loggingLayout};
  84. loggingConfiguration.AddTarget("debugger",debuggerTarget);
  85. var debuggerTargetRule = new LoggingRule("*", NLog.LogLevel.Debug, debuggerTarget);
  86. loggingConfiguration.LoggingRules.Add(debuggerTargetRule);
  87. }
  88. NLog.LogManager.Configuration = loggingConfiguration;
  89. LogManager.GetLog = type => new NLogAdapter(NLog.LogManager.GetLogger(type.Name));
  90. }
  91. }
  92. }