Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 7.60 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Diagnostics.Contracts;
  4. using System.Windows.Controls;
  5. using System.Windows.Controls.Primitives;
  6. using Microsoft.Practices.Prism;
  7. using Microsoft.Practices.Prism.Events;
  8. using Microsoft.Practices.Prism.Logging;
  9. using Microsoft.Practices.Prism.Modularity;
  10. using Microsoft.Practices.Prism.Regions;
  11. using Microsoft.Practices.Prism.Regions.Behaviors;
  12. using Microsoft.Practices.ServiceLocation;
  13. using StructureMap;
  14. using StructureMap.Exceptions;
  15. using StructureMap.ServiceLocatorAdapter;
  16.  
  17. namespace CodeProgression.Common.Prism
  18. {
  19.     public abstract class StructureMapBootstrapper : Microsoft.Practices.Prism.Bootstrapper
  20.     {
  21.         private bool _useDefaultConfiguration = true;
  22.  
  23.         private readonly ILoggerFacade _loggerFacade = new TraceLogger();
  24.  
  25.         public ILoggerFacade LoggerFacade { get { return _loggerFacade; } }
  26.  
  27.         public IContainer Container { get; private set; }
  28.  
  29.         public override void Run(bool runWithDefaultConfiguration)
  30.         {
  31.             _useDefaultConfiguration = runWithDefaultConfiguration;
  32.  
  33.             Container = CreateContainer();
  34.  
  35.             Contract.Assert(Container != null);
  36.  
  37.             ConfigureContainer();
  38.             ConfigureRegionAdapterMappings();
  39.             ConfigureDefaultRegionBehaviors();
  40.             RegisterFrameworkExceptionTypes();
  41.  
  42.             var shell = CreateShell();
  43.             if (shell != null)
  44.             {
  45.                 RegionManager.SetRegionManager(shell, Container.GetInstance<IRegionManager>());
  46.                 RegionManager.UpdateRegions();
  47.             }
  48.  
  49.             InitializeModules();
  50.         }
  51.         /// <summary>
  52.         /// Configures the <see cref="IRegionBehaviorFactory"/>. This will be the list of default
  53.         /// behaviors that will be added to a region.
  54.         /// </summary>
  55.         protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
  56.         {
  57.             var defaultRegionBehaviorTypesDictionary = Container.TryGetInstance<IRegionBehaviorFactory>();
  58.             if (defaultRegionBehaviorTypesDictionary != null)
  59.             {
  60.                 Action<string, Type> addIfMissing = defaultRegionBehaviorTypesDictionary.AddIfMissing;
  61.  
  62.                 addIfMissing(AutoPopulateRegionBehavior.BehaviorKey, typeof(AutoPopulateRegionBehavior));
  63.                 addIfMissing(BindRegionContextToDependencyObjectBehavior.BehaviorKey, typeof(BindRegionContextToDependencyObjectBehavior));
  64.                 addIfMissing(RegionActiveAwareBehavior.BehaviorKey, typeof(RegionActiveAwareBehavior));
  65.                 addIfMissing(SyncRegionContextWithHostBehavior.BehaviorKey, typeof(SyncRegionContextWithHostBehavior));
  66.                 addIfMissing(RegionManagerRegistrationBehavior.BehaviorKey, typeof(RegionManagerRegistrationBehavior));
  67.             }
  68.             return defaultRegionBehaviorTypesDictionary;
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Registers in the <see cref="IContainer"/> the <see cref="Type"/> of the Exceptions
  73.         /// that are not considered root exceptions by the <see cref="ExceptionExtensions"/>.
  74.         /// </summary>
  75.         protected override void RegisterFrameworkExceptionTypes()
  76.         {
  77.             ExceptionExtensions.RegisterFrameworkExceptionType(
  78.                 typeof(ActivationException));
  79.  
  80.             ExceptionExtensions.RegisterFrameworkExceptionType(
  81.                 typeof(MissingPluginFamilyException));
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Configures the <see cref="IContainer"/>. May be overwritten in a derived class to add specific
  86.         /// type mappings required by the application.
  87.         /// </summary>
  88.         protected virtual void ConfigureContainer()
  89.         {
  90.             Container.Configure(reg =>
  91.             {
  92.                 reg.For<IContainer>().Use(() => Container);
  93.                 reg.For<ILoggerFacade>().Use(LoggerFacade);
  94.                 reg.ForSingletonOf<IModuleCatalog>().Use(GetModuleCatalog);
  95.  
  96.                 if (!_useDefaultConfiguration) return;
  97.  
  98.                 reg.RegisterSingletonType<IServiceLocator, StructureMapServiceLocator>();
  99.                 reg.RegisterSingletonType<IModuleInitializer, ModuleInitializer>();
  100.                 reg.RegisterSingletonType<Microsoft.Practices.Prism.Modularity.IModuleManager, Microsoft.Practices.Prism.Modularity.ModuleManager>();
  101.                 reg.RegisterSingletonType<RegionAdapterMappings, RegionAdapterMappings>();
  102.                 reg.RegisterSingletonType<IRegionManager, RegionManager>();
  103.                 reg.RegisterSingletonType<IEventAggregator, EventAggregator>();
  104.                 reg.RegisterSingletonType<IRegionViewRegistry, RegionViewRegistry>();
  105.                 reg.RegisterSingletonType<IRegionBehaviorFactory, RegionBehaviorFactory>();
  106.                 reg.RegisterSingletonType<IRegionNavigationContentLoader, RegionNavigationContentLoader>();
  107.  
  108.             });
  109.             ServiceLocator.SetLocatorProvider(() => Container.GetInstance<IServiceLocator>());
  110.         }
  111.  
  112.         protected override void ConfigureServiceLocator()
  113.         {
  114.             // Service Locator configured in ConfigureContainer()
  115.         }
  116.  
  117.         /// <summary>
  118.         /// Configures the default region adapter mappings to use in the application, in order
  119.         /// to adapt UI controls defined in XAML to use a region and register it automatically.
  120.         /// May be overwritten in a derived class to add specific mappings required by the application.
  121.         /// </summary>
  122.         /// <returns>The <see cref="RegionAdapterMappings"/> instance containing all the mappings.</returns>
  123.         protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
  124.         {
  125.             var regionAdapterMappings = Container.GetInstance<RegionAdapterMappings>();
  126.  
  127.             if (regionAdapterMappings != null)
  128.             {
  129.                 regionAdapterMappings.RegisterMapping(typeof(Selector), Container.GetInstance<SelectorRegionAdapter>());
  130.                 regionAdapterMappings.RegisterMapping(typeof(ItemsControl), Container.GetInstance<ItemsControlRegionAdapter>());
  131.                 regionAdapterMappings.RegisterMapping(typeof(ContentControl), Container.GetInstance<ContentControlRegionAdapter>());
  132.             }
  133.  
  134.             return regionAdapterMappings;
  135.         }
  136.  
  137.         /// <summary>
  138.         /// Creates the <see cref="IContainer"/> that will be used as the default container.
  139.         /// </summary>
  140.         /// <returns>A new instance of <see cref="IContainer"/>.</returns>
  141.         protected virtual IContainer CreateContainer()
  142.         {
  143.             return new Container(x => { });
  144.         }
  145.  
  146.         /// <summary>
  147.         /// Returns the module enumerator that will be used to initialize the modules.
  148.         /// </summary>
  149.         /// <remarks>
  150.         /// When using the default initialization behavior, this method must be overwritten by a derived class.
  151.         /// </remarks>
  152.         /// <returns>An instance of <see cref="IModuleCatalog"/> that will be used to initialize the modules.</returns>
  153.         [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
  154.         protected virtual IModuleCatalog GetModuleCatalog()
  155.         {
  156.             return new ModuleCatalog();
  157.         }
  158.     }
  159.  
  160.     public static class StructureMapPrismExtensions
  161.     {
  162.         public static void RegisterSingletonType<TFrom, TTo>(this ConfigurationExpression reg)
  163.             where TFrom : class
  164.             where TTo : class, TFrom
  165.         {
  166.             reg.ForSingletonOf<TFrom>().Use<TTo>();
  167.         }
  168.  
  169.         public static void RegisterTypeIfMissing<TFrom, TTo>(this ConfigurationExpression reg)
  170.             where TFrom : class
  171.             where TTo : class, TFrom
  172.         {
  173.             reg.For<TFrom>().Use<TTo>();
  174.         }
  175.  
  176.     }
  177. }