public void ShouldForwardResolveToInnerContainer() { object myInstance = new object(); var mockContainer = new Mock <IUnityContainer>(); mockContainer.Setup(c => c.Resolve(typeof(object), null, It.IsAny <ResolverOverride[]>())).Returns(myInstance); var containerExtension = new UnityContainerExtension(mockContainer.Object); ContainerLocator.ResetContainer(); ContainerLocator.SetContainerExtension(() => containerExtension); var resolved = ContainerLocator.Container.Resolve(typeof(object)); mockContainer.Verify(c => c.Resolve(typeof(object), null, It.IsAny <ResolverOverride[]>()), Times.Once); Assert.Same(myInstance, resolved); }
public void GetErrorsLocatesIssueWithBadView() { ContainerLocator.ResetContainer(); var container = Setup.CreateContainer(); ContainerLocator.SetContainerExtension(() => Setup.Extension); Setup.Registry.Register <object, BadView>("BadView"); var ex = Record.Exception(() => container.Resolve <object>("BadView")); Assert.IsType <ContainerResolutionException>(ex); var cre = ex as ContainerResolutionException; var errors = cre.GetErrors(); Assert.Contains(typeof(BadView), errors.Types); }
public void GetErrorsLocatesXamlParseException() { ContainerLocator.ResetContainer(); var container = Setup.CreateContainer(); ContainerLocator.SetContainerExtension(() => Setup.Extension); Setup.Registry.Register <object, BadView>("BadView"); var ex = Record.Exception(() => container.Resolve <object>("BadView")); Assert.IsType <ContainerResolutionException>(ex); var cre = ex as ContainerResolutionException; var errors = cre.GetErrors(); Assert.Contains(errors, x => x.Value is XamlParseException); }
public void ShouldForwardResolveToInnerContainer() { var mockContainer = new Mock <IContainer>(); Assert.Empty(mockContainer.Invocations); var containerExtension = new DryIocContainerExtension(mockContainer.Object); // We register the IContainerExtension and IContainerProvider directly with the container in the ctor. Assert.Equal(2, mockContainer.Invocations.Count); ContainerLocator.ResetContainer(); ContainerLocator.SetContainerExtension(() => containerExtension); var resolved = ContainerLocator.Container.Resolve(typeof(object)); Assert.Equal(3, mockContainer.Invocations.Count); }
/// <summary> /// Run the bootstrapper process. /// </summary> protected virtual void Initialize() { ContainerLocator.SetContainerExtension(CreateContainerExtension); _containerExtension = ContainerLocator.Current; RegisterRequiredTypes(_containerExtension); PlatformInitializer?.RegisterTypes(_containerExtension); RegisterTypes(_containerExtension); _containerExtension.FinalizeExtension(); _moduleCatalog = Container.Resolve <IModuleCatalog>(); ConfigureModuleCatalog(_moduleCatalog); _containerExtension.CreateScope(); NavigationService = _containerExtension.Resolve <INavigationService>(); InitializeModules(); }
public void ShouldForwardResolveToInnerContainer() { object myInstance = new object(); var mockContainer = new Mock <IContainer>(); mockContainer.Setup(c => c.Resolve(typeof(object), IfUnresolved.Throw)).Returns(myInstance); var containerExtension = new DryIocContainerExtension(mockContainer.Object); ContainerLocator.ResetContainer(); ContainerLocator.SetContainerExtension(() => containerExtension); var resolved = ContainerLocator.Container.Resolve(typeof(object)); mockContainer.Verify(c => c.Resolve(typeof(object), IfUnresolved.Throw)); Assert.Same(myInstance, resolved); }
public INavigationServiceExtensionsFixture() { ContainerLocator.ResetContainer(); var container = new Mock <IContainerExtension>(); container.Setup(x => x.CreateScope()).Returns(Mock.Of <IScopedProvider>()); ContainerLocator.SetContainerExtension(() => container.Object); PageNavigationRegistry.ClearRegistrationCache(); PageNavigationRegistry.Register("NavigationPage", typeof(NavigationPage)); PageNavigationRegistry.Register("Page1", typeof(NavigationPathPageMock)); PageNavigationRegistry.Register("Page2", typeof(NavigationPathPageMock2)); PageNavigationRegistry.Register("Page3", typeof(NavigationPathPageMock3)); PageNavigationRegistry.Register("Page4", typeof(NavigationPathPageMock4)); PageNavigationRegistry.Register("TabbedPage1", typeof(NavigationPathTabbedPageMock)); PageNavigationRegistry.Register("MasterDetailPage", typeof(MasterDetailPage)); }
public void GetErrorsDoesNotThrowException() { ContainerLocator.ResetContainer(); var container = Setup.CreateContainer(); ContainerLocator.SetContainerExtension(() => Setup.Extension); Setup.Registry.Register <object, BadView>("BadView"); var ex = Record.Exception(() => container.Resolve <object>("BadView")); Assert.IsType <ContainerResolutionException>(ex); var cre = ex as ContainerResolutionException; ContainerResolutionErrorCollection errors = null; var ex2 = Record.Exception(() => errors = cre.GetErrors()); Assert.Null(ex2); }
private void InternalInitialize() { // don't forget there is no logger yet if (_logStartingEvents) { _logger.Log($"{nameof(PrismApplicationBase)}.{nameof(InternalInitialize)}", Category.Info, Priority.None); } // dependecy injection ContainerLocator.SetContainerExtension(CreateContainerExtension); Debug.WriteLine("[App.RegisterTypes()]"); _containerExtension = ContainerLocator.Current; _moduleCatalog = CreateModuleCatalog(); RegisterRequiredTypes(_containerExtension); RegisterTypes(_containerExtension); if (_containerExtension is IContainerRegistry registry) { registry.RegisterSingleton <ILoggerFacade, DebugLogger>(); registry.RegisterSingleton <IEventAggregator, EventAggregator>(); RegisterInternalTypes(registry); } Debug.WriteLine("Dependency container has just been finalized."); _containerExtension.FinalizeExtension(); ConfigureModuleCatalog(_moduleCatalog); RegionAdapterMappings regionAdapterMappins = _containerExtension.Resolve <RegionAdapterMappings>(); ConfigureRegionAdapterMappings(regionAdapterMappins); IRegionBehaviorFactory defaultRegionBehaviors = _containerExtension.Resolve <IRegionBehaviorFactory>(); ConfigureDefaultRegionBehaviors(defaultRegionBehaviors); RegisterFrameworkExceptionTypes(); // now we can start logging instead of debug/write _logger = Container.Resolve <ILoggerFacade>(); // finalize the application ConfigureViewModelLocator(); }
/// <summary> /// Run the bootstrapper process. /// </summary> protected virtual void Initialize() { ContainerLocator.SetContainerExtension(CreateContainerExtension); _containerExtension = ContainerLocator.Current; RegisterRequiredTypes(_containerExtension); PlatformInitializer?.RegisterTypes(_containerExtension); RegisterTypes(_containerExtension); AutoRegistrationViewNameProvider.SetDefaultProvider(GetNavigationSegmentNameFromType); GetType().AutoRegisterViews(_containerExtension); _containerExtension.FinalizeExtension(); _moduleCatalog = Container.Resolve <IModuleCatalog>(); ConfigureModuleCatalog(_moduleCatalog); _containerExtension.CreateScope(); NavigationService = _containerExtension.Resolve <INavigationService>(); InitializeModules(); }
public static IContainerExtension LocateContainer(IContainerExtension container = null) { if (container != null) { ContainerLocator.SetContainerExtension(() => container); } try { var located = ContainerLocator.Current; if (located != null) { return(located); } // If somehow we have an actual null container let's be sure to refresh the Locator ContainerLocator.ResetContainer(); } catch { // suppress any errors ContainerLocator.ResetContainer(); } var entryAssembly = Assembly.GetEntryAssembly(); var containerAttributes = entryAssembly.GetCustomAttributes().OfType <ContainerExtensionAttribute>(); if (!containerAttributes.Any()) { throw new InvalidOperationException("An instance of the IContainerExtension has not been registered with the ContainerLocator, and no ContainerExtensionAttribute has been found in the entry assembly."); } else if (containerAttributes.Count() > 1) { throw new InvalidOperationException("More than one ContainerExtensionAttribute has been found on the entry assembly. Only a single ContainerExtension should be referenced."); } var containerExtensionAttribute = containerAttributes.First(); return(containerExtensionAttribute.GetContainer()); }
/// <summary> /// Runs the initialization sequence to configure the Prism application. /// </summary> protected virtual void Initialize() { ContainerLocator.SetContainerExtension(CreateContainerExtension); _containerExtension = ContainerLocator.Current; _moduleCatalog = CreateModuleCatalog(); RegisterRequiredTypes(_containerExtension); RegisterTypes(_containerExtension); _containerExtension.FinalizeExtension(); ConfigureModuleCatalog(_moduleCatalog); RegisterFrameworkExceptionTypes(); var shell = CreateShell(); if (shell != null) { MvvmHelpers.AutowireViewModel(shell); InitializeShell(shell); } InitializeModules(); }
protected virtual void Initialize() { this.Logger = this.CreateLogger(); if (this.Logger == null) { throw new InvalidOperationException("log created faild"); } ConfigureViewModelLocator(); this.Logger.Log("log created success", LogLevel.Debug, Priority.Low); this.Logger.Log("create module catelog", LogLevel.Debug, Priority.Low); this.ModuleCatalog = this.CreateModuleCatalog(); if (this.ModuleCatalog == null) { throw new InvalidOperationException("module catelog null"); } this.Logger.Log("configuring module catelog", LogLevel.Debug, Priority.Low); ConfigureModuleCatalog(ModuleCatalog); this.Logger.Log("creating careful container", LogLevel.Debug, Priority.Low); this.Container = this.CreateContainer(); if (this.Container == null) { throw new InvalidOperationException("careful container null"); } this.Logger.Log("configuring careful container", LogLevel.Debug, Priority.Low); this.ConfigureContainer(); this.Logger.Log("confguring service locator", LogLevel.Debug, Priority.Low); this.ConfigureServiceLocator(); ContainerLocator.SetContainerExtension(() => CreateContainerExtension()); ContainerExtension = ContainerLocator.Current; //To Do :region register //RegisterRequiredTypes(ContainerExtension); //RegisterTypes(ContainerExtension); //ContainerExtension.FinalizeExtension(); //this.Logger.Log("configuring region adapter", LogLevel.Debug, Priority.Low); //this.ConfigureRegionAdapterMappings(); //this.Logger.Log("configuring default region behaviors", LogLevel.Debug, Priority.Low); //this.ConfigureDefaultRegionBehaviors(); //this.Logger.Log("registering frameworkd excetion type", LogLevel.Debug, Priority.Low); this.RegisterFrameworkExceptionTypes(); this.Logger.Log("create shell", LogLevel.Debug, Priority.Low); this.Shell = this.CreateShell(); if (this.Shell != null) { MvvmHelpers.AutowireViewModel(Shell); this.Logger.Log("setting region management", LogLevel.Debug, Priority.Low); RegionManager.SetRegionManager(this.Shell, this.Container.GetInstance <IRegionManager>()); this.Logger.Log("updating region", LogLevel.Debug, Priority.Low); RegionManager.UpdateRegions(); this.Logger.Log("initialize shell", LogLevel.Debug, Priority.Low); this.OnInitialized(); } if (this.Container.IsRegistered <IModuleManager>()) { this.Logger.Log("initialize module", LogLevel.Debug, Priority.Low); this.InitializeModules(); } this.Logger.Log("bootstrapper sequence completed", LogLevel.Debug, Priority.Low); //var regionAdapterMappins = ContainerExtension.Resolve<RegionAdapterMappings>(); //ConfigureRegionAdapterMappings(regionAdapterMappins); //var defaultRegionBehaviors = ContainerExtension.Resolve<IRegionBehaviorFactory>(); //ConfigureDefaultRegionBehaviors(defaultRegionBehaviors); //RegisterFrameworkExceptionTypes(); }
/// <summary> /// Run the bootstrapper process. /// </summary> /// <param name="runWithDefaultConfiguration">If <see langword="true"/>, registers default Prism Library services in the container. This is the default behavior.</param> public override void Run(bool runWithDefaultConfiguration) { _useDefaultConfiguration = runWithDefaultConfiguration; Logger = CreateLogger(); if (Logger == null) { throw new InvalidOperationException(Resources.NullLoggerFacadeException); } Logger.Log(Resources.LoggerCreatedSuccessfully, Category.Debug, Priority.Low); Logger.Log(Resources.CreatingModuleCatalog, Category.Debug, Priority.Low); ModuleCatalog = CreateModuleCatalog(); if (ModuleCatalog == null) { throw new InvalidOperationException(Resources.NullModuleCatalogException); } Logger.Log(Resources.ConfiguringModuleCatalog, Category.Debug, Priority.Low); ConfigureModuleCatalog(); Logger.Log(Resources.CreatingContainer, Category.Debug, Priority.Low); Container = CreateContainer(); if (Container == null) { throw new InvalidOperationException(Resources.NullDryIocContainerException); } ContainerLocator.SetContainerExtension(CreateContainerExtension); ContainerExtension = ContainerLocator.Current; Logger.Log(Resources.ConfiguringContainer, Category.Debug, Priority.Low); ConfigureContainer(); Logger.Log(Resources.ConfiguringViewModelLocator, Category.Debug, Priority.Low); ConfigureViewModelLocator(); Logger.Log(Resources.ConfiguringRegionAdapters, Category.Debug, Priority.Low); ConfigureRegionAdapterMappings(); Logger.Log(Resources.ConfiguringDefaultRegionBehaviors, Category.Debug, Priority.Low); ConfigureDefaultRegionBehaviors(); Logger.Log(Resources.RegisteringFrameworkExceptionTypes, Category.Debug, Priority.Low); RegisterFrameworkExceptionTypes(); Logger.Log(Resources.CreatingShell, Category.Debug, Priority.Low); Shell = CreateShell(); if (Shell != null) { Logger.Log(Resources.SettingTheRegionManager, Category.Debug, Priority.Low); RegionManager.SetRegionManager(Shell, Container.Resolve <IRegionManager>()); Logger.Log(Resources.UpdatingRegions, Category.Debug, Priority.Low); RegionManager.UpdateRegions(); Logger.Log(Resources.InitializingShell, Category.Debug, Priority.Low); InitializeShell(); } if (Container.IsRegistered <IModuleManager>()) { Logger.Log(Resources.InitializingModules, Category.Debug, Priority.Low); InitializeModules(); } Logger.Log(Resources.BootstrapperSequenceCompleted, Category.Debug, Priority.Low); }
public ContainerExtension(IDependencyContainer container) { Instance = container ?? throw new ArgumentNullException(nameof(container)); ContainerLocator.SetContainerExtension(() => this); }
public ContainerProviderExtensionFixture() { ContainerLocator.ResetContainer(); ContainerLocator.SetContainerExtension(() => _containerExtension); }
/// <summary> /// Runs the initialization sequence to configure the Prism application. /// </summary> public virtual void Initialize() { ContainerLocator.SetContainerExtension(CreateContainerExtension); _containerExtension = ContainerLocator.Current; _moduleCatalog = CreateModuleCatalog(); RegisterRequiredTypes(_containerExtension); RegisterTypes(_containerExtension); _containerExtension.FinalizeExtension(); ConfigureModuleCatalog(_moduleCatalog); var regionAdapterMappins = _containerExtension.Resolve <RegionAdapterMappings>(); ConfigureRegionAdapterMappings(regionAdapterMappins); var defaultRegionBehaviors = _containerExtension.Resolve <IRegionBehaviorFactory>(); ConfigureDefaultRegionBehaviors(defaultRegionBehaviors); RegisterFrameworkExceptionTypes(); var shell = CreateShell(); if (shell != null) { _containerExtension.Resolve <IRegionNavigationService>().NavigationFailed += (s, e) => Console.WriteLine($"Region navigation failed {e.Error}"); InitializeShell(shell); void FinalizeInitialization() { RegionManager.SetRegionManager(shell, _containerExtension.Resolve <IRegionManager>()); RegionManager.UpdateRegions(); InitializeModules(); OnInitialized(); } if (shell is FrameworkElement fe) { void OnLoaded(object sender, object args) { FinalizeInitialization(); fe.Loaded -= OnLoaded; } #if HAS_UNO // Uno currently loads items earlier than UWP, so we can check // for the IsLoaded property. UWP got that property in SDK 17763, // meaning that the condition can be removed once the SDK is updated // in Prism.Uno. if (fe.IsLoaded) { FinalizeInitialization(); } else #endif { // We need to delay the initialization after the shell has been loaded, otherwise // the visual tree is not materialized for the RegionManager to be available. // See https://github.com/PrismLibrary/Prism/issues/2102 for more details. fe.Loaded += OnLoaded; } } else { FinalizeInitialization(); } } }
public IShinyStartup WithContainer(IContainerExtension container) { ContainerLocator.SetContainerExtension(() => container); return(this); }