private void Application_Startup(object sender, StartupEventArgs e) { this.RegisterErrorHandler(); Jupiter.Properties.Settings.Default.Upgrade(); ViewModelLocationProvider.SetDefaultViewModelFactory(x => this.Container.Resolve(x)); var lifetimeManager = new ContainerControlledLifetimeManager(); var lifetimeManager2 = new ContainerControlledLifetimeManager(); var lifetimeManager3 = new ContainerControlledLifetimeManager(); var lifetimeManager4 = new ContainerControlledLifetimeManager(); var ea = new Prism.Events.EventAggregator(); this.Container.RegisterInstance <Prism.Events.IEventAggregator>(ea); this.Container.RegisterType <Interfaces.INodeTreeModel, Models.NodeTreeModel>(lifetimeManager); this.Container.RegisterType <Interfaces.INodeInfoDataGrid, Models.NodeInfoDataGridModel>(lifetimeManager2); this.Container.RegisterType <Interfaces.ISubscriptionModel, Models.SubscriptionModel>(lifetimeManager3); this.Container.RegisterType <Interfaces.IOneTimeAccessModel, Models.OneTimeAccessModel>(lifetimeManager4); var c = new Client(ea); this.Container.RegisterInstance <Interfaces.IConnection>(c); this.Container.RegisterInstance <Interfaces.IReferenceFetchable>(c); this.Container.RegisterInstance <Interfaces.INodeInfoGetter>(c); this.Container.RegisterInstance <Interfaces.ISubscriptionOperatable>(c); this.Container.RegisterInstance <Interfaces.IOneTimeAccessOperator>(c); var references = new OPCUAReference(c, null, ea); this.Container.RegisterInstance <Interfaces.IReference>(references); this.Container.Resolve <Views.MainWindow>().Show(); }
public object Resolve(IBuilderContext context) { var lifetimeManager = new ContainerControlledLifetimeManager(); lifetimeManager.SetValue(_logSourceType); var loggerNameKey = new NamedTypeBuildKey(typeof(Type), "LogSourceType"); //Create the build context for the logger var newKey = new NamedTypeBuildKey(_type, _name); //Register the item as a transient policy context.Policies.Set <IBuildKeyMappingPolicy>(new BuildKeyMappingPolicy(loggerNameKey), loggerNameKey); context.Policies.Set <ILifetimePolicy>(lifetimeManager, loggerNameKey); context.Lifetime.Add(lifetimeManager); try { return(context.NewBuildUp(newKey)); } finally { context.Lifetime.Remove(lifetimeManager); context.Policies.Clear <IBuildKeyMappingPolicy>(loggerNameKey); context.Policies.Clear <ILifetimePolicy>(loggerNameKey); } }
public void ComplicatedRegistrationsWithChildContainerLifetimes2() { var container = new QuickInjectContainer(); container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor()); var child = container.CreateChildContainer(); var correctInstanceForIFooResolutionFromChild = new Foo(); var correctInstanceForFooResolutionFromChild = new SuperFoo(); var preSetFooOnLifetime = new Foo(); SuperFoo fooResolvedFromMainContainer = new SuperFoo(); var lifetime = new ContainerControlledLifetimeManager(); lifetime.SetValue(fooResolvedFromMainContainer); container.RegisterType <IFoo, Foo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => new Foo())); container.RegisterType <IBar, Foo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => correctInstanceForIFooResolutionFromChild)); container.RegisterType <Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => fooResolvedFromMainContainer)); container.RegisterType <SuperFoo>(lifetime); child.RegisterType <Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => correctInstanceForFooResolutionFromChild)); var f = container.Resolve <Foo>(); var g = container.Resolve <Foo>(); Assert.AreSame(child.Resolve <IBar>(), correctInstanceForIFooResolutionFromChild); Assert.AreSame(child.Resolve <IFoo>(), correctInstanceForIFooResolutionFromChild); Assert.AreSame(child.Resolve <Foo>(), correctInstanceForFooResolutionFromChild); Assert.AreSame(container.Resolve <Foo>(), fooResolvedFromMainContainer); Assert.AreSame(container.Resolve <SuperFoo>(), fooResolvedFromMainContainer); }
/// <summary> /// Register the type mapping with the container, with the given <see cref="LifetimeType" />. /// </summary> /// <param name="serviceType"><see cref="System.Type" /> of the service that will be requested.</param> /// <param name="implementationType"><see cref="System.Type" /> of the implementation that will be returned.</param> /// <param name="lifetime">The <see cref="LifetimeType" /> of the service.</param> /// <returns>The <see cref="IContainerProvider" /> object that this method was called on.</returns> /// <exception cref="ArgumentException">Thrown when the implementation type does not inherit from IPerRequestTask and lifetime is PerRequest.</exception> public override IContainerProvider RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime) { LifetimeManager lifetimeManager; switch (lifetime) { case LifetimeType.Singleton: lifetimeManager = new ContainerControlledLifetimeManager(); break; default: lifetimeManager = new TransientLifetimeManager(); break; } if (container.Registrations.Any(registration => registration.RegisteredType == serviceType)) { container.RegisterType(serviceType, implementationType, implementationType.FullName, lifetimeManager); } else { container.RegisterType(serviceType, implementationType, lifetimeManager); } return(this); }
/// <summary> /// 子类覆写此方法时, 应当实现将 <paramref name="toType"/> 类型注册为 /// <paramref name="fromType"/> 类型的实现类. /// </summary> /// <param name="fromType">接口类型</param> /// <param name="toType">实现类型</param> /// <param name="key">注册命名对象的名称, 可以为 null 以表示注册为默认实现类</param> /// <param name="lifetime">指定根据本条映射关系创建的对象的生命期</param> protected override void DoRegisterType(Type fromType, Type toType, string key, Lifetime lifetime) { LifetimeManager lm; switch (lifetime) { case Lifetime.New: _container.RegisterType(fromType, toType, key); break; case Lifetime.PerThread: lm = new PerThreadLifetimeManager(); _container.RegisterType(fromType, toType, key, lm); break; case Lifetime.Singleton: lm = new ContainerControlledLifetimeManager(); _container.RegisterType(fromType, toType, key, lm); break; default: throw new ArgumentOutOfRangeException("lifetime"); } }
/// <summary> /// Registers the type. /// </summary> /// <param name="serviceType">Type of the service.</param> /// <param name="implementationType">Type of the implementation.</param> /// <param name="lifetime">The lifetime of the service.</param> /// <returns></returns> public override IServiceRegistrar RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime) { Invariant.IsNotNull(serviceType, "serviceType"); Invariant.IsNotNull(implementationType, "implementationType"); LifetimeManager lifeTimeManager; switch (lifetime) { case LifetimeType.PerRequest: lifeTimeManager = new PerRequestLifetimeManager(); break; case LifetimeType.Singleton: lifeTimeManager = new ContainerControlledLifetimeManager(); break; default: lifeTimeManager = new TransientLifetimeManager(); break; } if (Container.Registrations.Any(registration => registration.RegisteredType.Equals(serviceType))) { Container.RegisterType(serviceType, implementationType, implementationType.FullName, lifeTimeManager); } else { Container.RegisterType(serviceType, implementationType, lifeTimeManager); } return(this); }
private void SetSingleton(IBuilderContext context, Type t, object value) { var policy = new ContainerControlledLifetimeManager(); policy.SetValue(value); context.PersistentPolicies.Set <ILifetimePolicy>(policy, new NamedTypeBuildKey(t)); }
protected void RegisterSingletonType <TInterface, TType>() where TType : TInterface { var lifeTimeManager = new ContainerControlledLifetimeManager(); uc.RegisterType <TInterface, TType>(lifeTimeManager); }
private static IUnityContainer BuildUnityContainer() { UnityContainer container = new UnityContainer(); #if DEBUG LifetimeManager lifetimeManager = new ContainerControlledLifetimeManager(); container.RegisterType <ISteamRepository, MockSteamRepository>(lifetimeManager); container.RegisterType <IFacebookContextSettings, MockFacebookContextSettings>(); #else container.RegisterType <ISteamRepository, SteamRepository>(); container.RegisterType <IFacebookContextSettings, FacebookContextSettings>(); #endif container.RegisterType <ISteamCommunityManager, SteamCommunityManager>(); container.RegisterType <IAchievementXmlParser, AchievementXmlParser>(); container.RegisterType <IGameXmlParser, GameXmlParser>(); container.RegisterType <ISteamProfileXmlParser, SteamProfileXmlParser>(); container.RegisterType <IWebClientWrapper, WebClientWrapper>(); container.RegisterType <IAchievementManager, AchievementManager>(); container.RegisterType <IAchievementService, AchievementService>(); container.RegisterType <IUserService, UserService>(); container.RegisterControllers(); return(container); }
/// <summary> /// Convert the lifetime enum to a lifetime manager in Unity. /// </summary> /// <param name="type"> Type to register </param> /// <param name="lifetime"> Lifetime to convert </param> /// <returns> A lifetime manager </returns> protected virtual LifetimeManager GetLifetimeManager(Type type, Lifetime lifetime) { if (type == null) { throw new ArgumentNullException("type"); } LifetimeManager lifetimeManager; switch (lifetime) { case Lifetime.Transient: lifetimeManager = new TransientLifetimeManager(); break; case Lifetime.Singleton: lifetimeManager = new ContainerControlledLifetimeManager(); break; case Lifetime.Scoped: lifetimeManager = ScopedLifetimeFactory.Current.Create(); break; default: throw new InvalidOperationException( string.Format( "Either the [Component] attribute on {0} or the ComponentRegistrar.DefaultLifetime must have been specified.", type.FullName)); } return(lifetimeManager); }
LifetimeManager GetLifetimeManager(DependencyLifeTime lifeTime) { LifetimeManager lifetimeManager = null; switch (lifeTime) { case DependencyLifeTime.PerDependency: { lifetimeManager = new PerResolveLifetimeManager(); break; } case DependencyLifeTime.PerRequest: { lifetimeManager = new PerRequestLifetimeManager(); break; } case DependencyLifeTime.SingleInstance: { lifetimeManager = new ContainerControlledLifetimeManager(); break; } default: { lifetimeManager = new PerResolveLifetimeManager(); break; } } return(lifetimeManager); }
/// <summary> /// Registers the type mapping. /// </summary> /// <param name="serviceType"><see cref="Type"/> that will be requested.</param> /// <param name="implementationType"><see cref="Type"/> that will actually be returned.</param> /// <param name="serviceName">Name to use for registration, null if a default registration.</param> /// <param name="lifetime">The lifetime strategy of the resolved instances.</param> protected override void DoRegister(Type serviceType, Type implementationType, string serviceName, ServiceLifetime lifetime) { if (_container == null) { throw new ObjectDisposedException("container"); } var args = new UnityServiceRegisterEventArgs(serviceType, implementationType, serviceName, lifetime); args.InjectionMembers.AddRange(_injectionMembers); OnRegistering(args); LifetimeManager lifetimeManager; switch (lifetime) { case ServiceLifetime.Transient: lifetimeManager = new TransientLifetimeManager(); break; case ServiceLifetime.PerThread: lifetimeManager = new PerThreadLifetimeManager(); break; case ServiceLifetime.PerRequest: lifetimeManager = new PerRequestLifetimeManager(); break; default: lifetimeManager = new ContainerControlledLifetimeManager(); break; } _container.RegisterType(serviceType, implementationType, serviceName, lifetimeManager, args.InjectionMembers.ToArray()); }
public void RegisteringTheSameLifetimeManagerTwiceThrows() { LifetimeManager singleton = new ContainerControlledLifetimeManager(); new UnityContainer() .RegisterType <ILogger, MockLogger>(singleton) .RegisterType <ILogger, SpecialLogger>("special", singleton); }
public void RegisterType(Type interfaceType, Type realType, object[] parms, ContainerControlledLifetimeManager containerControlledLifetimeManager) { var instance = Activator.CreateInstance(realType, parms); containerControlledLifetimeManager.SetValue(instance); objects.Add(interfaceType, containerControlledLifetimeManager); }
public Dependency(int order = -1) { Order = order; Name = string.Empty; Lifetime = new ContainerControlledLifetimeManager(); Register = true; AllowRoles = new List <NodeRole>(); }
/// <summary> /// Checks if the instance being resolved has a lifetime manager. If it does not, an instance of /// <see cref="ContainerControlledLifetimeManager"/> is registered, which will then be used by /// <see cref="LifetimeStrategy"/>. /// </summary> /// <param name="context">Context of the build operation.</param> public override void PreBuildUp(IBuilderContext context) { if (context.Existing == null && !HasLifetimeManager(context)) { ILifetimePolicy lifetimeManager = new ContainerControlledLifetimeManager(); context.PersistentPolicies.Set(lifetimeManager, context.BuildKey); } }
//Create a Singleton public static LifetimeManager CreateSingleton <TFrom, TTo>(IUnityContainer container) where TTo : TFrom { https: //docs.microsoft.com/en-us/previous-versions/msp-n-p/ff647854(v=pandp.10) var singletonLifeTimeManager = new ContainerControlledLifetimeManager(); container.RegisterType <TFrom, TTo>(singletonLifeTimeManager); return(singletonLifeTimeManager); }
public void RegisteringTheSameLifetimeManagerTwiceThrows() { LifetimeManager singleton = new ContainerControlledLifetimeManager(); AssertExtensions.AssertException <InvalidOperationException>(() => { GetContainer() .RegisterType <IService, Service>(singleton) .RegisterType <IService, OtherService>("special", singleton); }); }
public void RegisteringTheSameLifetimeManagerTwiceThrows() { LifetimeManager singleton = new ContainerControlledLifetimeManager(); AssertExtensions.AssertException <InvalidOperationException>(() => { new UnityContainer() .RegisterType <ILogger, MockLogger>(singleton) .RegisterType <ILogger, SpecialLogger>("special", singleton); }); }
public void RegisterType <TKeyType, TValueType>(bool isReuseRootComponent = true) where TValueType : TKeyType { ContainerControlledLifetimeManager lifeTimeManager = null; if (isReuseRootComponent) { lifeTimeManager = new ContainerControlledLifetimeManager(); } this._lazyUnityContianer.Value.RegisterType <TKeyType, TValueType>(lifeTimeManager); }
private void AddExplicitBehaviorPolicies(Type implementationType, string name, IPolicyList policies) { var lifetimeManager = new ContainerControlledLifetimeManager(); lifetimeManager.SetValue(explicitBehavior); var behaviorName = Guid.NewGuid().ToString(); var newBehaviorKey = new NamedTypeBuildKey(explicitBehavior.GetType(), behaviorName); policies.Set<ILifetimePolicy>(lifetimeManager, newBehaviorKey); InterceptionBehaviorsPolicy behaviorsPolicy = GetBehaviorsPolicy(policies, implementationType, name); behaviorsPolicy.AddBehaviorKey(newBehaviorKey); }
public override void RegisterInstance(Type type, object instance, string name) { var lifetime = new ContainerControlledLifetimeManager(); if (string.IsNullOrEmpty(name)) { _container.RegisterInstance(type, instance, lifetime); } else { _container.RegisterInstance(type, name, instance, lifetime); } }
private void AddExplicitBehaviorPolicies(Type implementationType, string name, IPolicyList policies) { var lifetimeManager = new ContainerControlledLifetimeManager(); lifetimeManager.SetValue(this.explicitBehavior); var behaviorName = Guid.NewGuid().ToString(); var newBehaviorKey = new NamedTypeBuildKey(this.explicitBehavior.GetType(), behaviorName); policies.Set <ILifetimePolicy>(lifetimeManager, newBehaviorKey); InterceptionBehaviorsPolicy behaviorsPolicy = this.GetBehaviorsPolicy(policies, implementationType, name); behaviorsPolicy.AddBehaviorKey(newBehaviorKey); }
private void RegisterIocTypes() { ContainerControlledLifetimeManager visionServiceLifetimeManager = new ContainerControlledLifetimeManager(); ObjectFactory.Container.RegisterType <IVisionClient, VisionClient>(visionServiceLifetimeManager); this.containerControlledLifetimeManagers.Add(visionServiceLifetimeManager); ContainerControlledLifetimeManager imageHandlerLifetimeManager = new ContainerControlledLifetimeManager(); ObjectFactory.Container.RegisterType <ImageHandler>(imageHandlerLifetimeManager); this.containerControlledLifetimeManagers.Add(imageHandlerLifetimeManager); ObjectFactory.Container.RegisterType <ICognitiveImageProcessor, CognitiveImageProcessor>(ModuleName); }
public void PerformRegistrations_CreatesAllParts_RegistersItIntoUnity() { var lifetimeManager = new ContainerControlledLifetimeManager(); var injectionMembers = new InjectionMember[] { new InterceptionBehavior <PolicyInjectionBehavior>() }; StubCreateInjectionMembers(injectionMembers); registrationNameFactory.Setup(f => f.GetRegistrationName(It.IsAny <TypeMapping>())).Returns("TEST"); configLifetimeManagerFactory.Setup(l => l.CreateLifetimeManager(It.IsAny <TypeMapping>())).Returns(lifetimeManager); // Act handler.PerformRegistrations(target.Object, new[] { new TypeMapping(typeof(String), typeof(Boolean)) }); // Assert target.Verify(t => t.RegisterType(typeof(String), typeof(Boolean), "TEST", lifetimeManager, injectionMembers)); }
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); Container = new UnityContainer(); //Container.RegisterInstance<Models.Imodel>("Model", new Models.Model(), new ContainerControlledLifetimeManager()); var lifetimeManager = new ContainerControlledLifetimeManager(); Container.RegisterType <Models.Imodel, Models.Model>("Model", lifetimeManager); var bootstrapper = new Bootstrapper(); bootstrapper.Run(); }
/// <summary> /// Adds class to the container using Static container approach. /// </summary> /// <param name="name">alias name to use in type registration</param> /// <typeparam name="TTYpe">Marker interface for class isntance.</typeparam> /// <typeparam name="TInstance">Class implenting the <see cref="TTYpe"/>.</typeparam> public static void Add <TTYpe, TInstance>(string name, bool createNewEveryTime = false) where TInstance : class, TTYpe { LifetimeManager lifetimeManager; if (createNewEveryTime) { lifetimeManager = new TransientLifetimeManager(); } else { lifetimeManager = new ContainerControlledLifetimeManager(); } Current.RegisterType <TTYpe, TInstance>(name, lifetimeManager); }
static void TestContainerControlledLifetime() { Console.WriteLine("Test ContainerControlledLifetimeManager"); LifetimeTest.ResetCounter(); using (var container = new UnityContainer()) { var lifeManager = new ContainerControlledLifetimeManager(); container.RegisterType <ILifetimeTest, LifetimeTest>(lifeManager); var obj1 = container.Resolve <ILifetimeTest>(); var obj2 = container.Resolve <ILifetimeTest>(); LifetimeTest.PrintCounter(); } LifetimeTest.PrintCounter(); }
public void RegisterInstance_T_Name_Manager() { // Arrange var manager = new ContainerControlledLifetimeManager(); // Act Container.RegisterInstance <IService>(Name, Instance, manager); // Validate var registration = Container.Registrations.First(r => typeof(IService) == r.RegisteredType); Assert.AreEqual(typeof(IService), registration.RegisteredType); Assert.AreEqual(Name, registration.Name); Assert.AreSame(manager, registration.LifetimeManager); }
private LifetimeManager GetLifetimeManager(DependencyLifecycle lifecycle) { LifetimeManager manager = new ContainerControlledLifetimeManager(); switch (lifecycle) { case DependencyLifecycle.Transient: manager = new TransientLifetimeManager(); break; default: break; } return(manager); }
public override void RegisterInstance(Type type, object instance, string name) { CheckInstanceIsAssignableFromType(type, instance); LifetimeManager lifetime = new ContainerControlledLifetimeManager(); if (string.IsNullOrWhiteSpace(name)) { container.RegisterInstance(type, instance, lifetime); } else { container.RegisterInstance(type, name, instance, lifetime); } }