private ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context) { Type typeToBuild = context.BuildKey.Type; object openGenericBuildKey = new NamedTypeBuildKey(typeToBuild.GetGenericTypeDefinition(), context.BuildKey.Name); IPolicyList factorySource; ILifetimeFactoryPolicy factoryPolicy = context.Policies.Get <ILifetimeFactoryPolicy>(openGenericBuildKey, out factorySource); if (factoryPolicy != null) { // creating the lifetime policy can result in arbitrary code execution // in particular it will likely result in a Resolve call, which could result in locking // to avoid deadlocks the new lifetime policy is created outside the lock // multiple instances might be created, but only one instance will be used ILifetimePolicy newLifetime = factoryPolicy.CreateLifetimePolicy(); lock (this.genericLifetimeManagerLock) { // check whether the policy for closed-generic has been added since first checked ILifetimePolicy lifetime = factorySource.GetNoDefault <ILifetimePolicy>(context.BuildKey, false); if (lifetime == null) { factorySource.Set <ILifetimePolicy>(newLifetime, context.BuildKey); lifetime = newLifetime; } return(lifetime); } } return(null); }
public override void PreTearDown(IBuilderContext context) { // Assumes registration name is null var buildKey = new NamedTypeBuildKey(context.Existing.GetType()); ILifetimePolicy lifeTime = context.Policies.Get <ILifetimePolicy>(buildKey); base.PreTearDown(context); }
/// <summary> /// Called during the chain of responsibility for a build operation. The /// PostBuildUp method is called when the chain has finished the PreBuildUp /// phase and executes in reverse order from the PreBuildUp calls. /// </summary> /// <param name="context">Context of the build operation.</param> // FxCop suppression: Validation is done by Guard class public override void PostBuildUp(IBuilderContext context) { Guard.ArgumentNotNull(context, "context"); // If we got to this method, then we know the lifetime policy didn't // find the object. So we go ahead and store it. ILifetimePolicy lifetimePolicy = GetLifetimePolicy(context); lifetimePolicy.SetValue(context.Existing); }
/// <summary> /// Called during the chain of responsibility for a build operation. The /// PostBuildUp method is called when the chain has finished the PreBuildUp /// phase and executes in reverse order from the PreBuildUp calls. /// </summary> /// <param name="context">Context of the build operation.</param> public override void PostBuildUp(IBuilderContext context) { Guard.ArgumentNotNull(context, "context"); IPolicyList containingPolicyList; ILifetimePolicy lifetimePolicy = GetLifetimePolicy(context, out containingPolicyList); lifetimePolicy.SetValue(context.Existing); }
private static ILifetimePolicy GetLifetimePolicy(IBuilderContext context) { ILifetimePolicy policy = context.Policies.Get <ILifetimePolicy>(context.BuildKey); if (policy == null) { policy = new TransientLifetimePolicy(); context.PersistentPolicies.Set <ILifetimePolicy>(policy, context.BuildKey); } return(policy); }
public bool HasResolvedLifetime(Type type, string name) { NamedTypeBuildKey key = new NamedTypeBuildKey(type, name); ILifetimePolicy lifetimePolicy = Context.Policies.Get <ILifetimePolicy>(key); if (lifetimePolicy != null) { return(lifetimePolicy.GetValue() != null); } return(false); }
private void RegisterType(bool singleton, Type type) { ILifetimePolicy policy = null; if (singleton) { policy = new SingletonLifetimePolicy(type, this); } else { policy = new TransientLifetimePolicy(type, this); } _resolvers[type] = new ConcreteTypeResolver(policy); }
private static ILifetimePolicy GetLifetimePolicy(IBuilderContext context) { ILifetimePolicy policy = context.Policies.GetNoDefault <ILifetimePolicy>(context.BuildKey, false); if (policy == null && context.BuildKey.Type.IsGenericType) { policy = GetLifetimePolicyForGenericType(context); } if (policy == null) { policy = new TransientLifetimeManager(); context.PersistentPolicies.Set <ILifetimePolicy>(policy, context.BuildKey); } return(policy); }
private static ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context) { Type typeToBuild = BuildKey.GetType(context.BuildKey); object openGenericBuildKey = BuildKey.ReplaceType(context.BuildKey, typeToBuild.GetGenericTypeDefinition()); ILifetimeFactoryPolicy factoryPolicy = context.Policies.Get <ILifetimeFactoryPolicy>(openGenericBuildKey); if (factoryPolicy != null) { ILifetimePolicy lifetime = factoryPolicy.CreateLifetimePolicy(); context.PersistentPolicies.Set <ILifetimePolicy>(lifetime, context.BuildKey); return(lifetime); } return(null); }
private static ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context) { Type typeToBuild = context.BuildKey.Type; object openGenericBuildKey = new NamedTypeBuildKey(typeToBuild.GetGenericTypeDefinition(), context.BuildKey.Name); IPolicyList factorySource; ILifetimeFactoryPolicy factoryPolicy = context.Policies.Get <ILifetimeFactoryPolicy>(openGenericBuildKey, out factorySource); if (factoryPolicy != null) { ILifetimePolicy lifetime = factoryPolicy.CreateLifetimePolicy(); factorySource.Set <ILifetimePolicy>(lifetime, context.BuildKey); return(lifetime); } return(null); }
public override void PreBuildUp(IBuilderContext context) { Guard.ArgumentNotNull(context, "context"); if (context.Existing == null) { ILifetimePolicy lifetimePolicy = GetLifetimePolicy(context); IRequiresRecovery recovery = lifetimePolicy as IRequiresRecovery; if (recovery != null) { context.RecoveryStack.Add(recovery); } object existing = lifetimePolicy.GetValue(); if (existing != null) { context.Existing = existing; context.BuildComplete = true; } } }
public void LifetimeStrategyUsesFactoryToGetLifetimePolicyForGenericType() { MockBuilderContext context = CreateContext(); var openKey = new NamedTypeBuildKey(typeof(YetAnotherDummyInterfaceImplementation <>)); context.PersistentPolicies.Set <ILifetimeFactoryPolicy>( new LifetimeFactoryPolicy <RecoverableLifetime>(), openKey); context.ExecuteBuildUp(new NamedTypeBuildKey <YetAnotherDummyInterfaceImplementation <string> >(), null); context.ExecuteBuildUp(new NamedTypeBuildKey <YetAnotherDummyInterfaceImplementation <int> >(), null); ILifetimePolicy stringLifetime = context.Policies.GetNoDefault <ILifetimePolicy>(new NamedTypeBuildKey(typeof(YetAnotherDummyInterfaceImplementation <string>)), false); ILifetimePolicy intLifetime = context.Policies.GetNoDefault <ILifetimePolicy>(new NamedTypeBuildKey(typeof(YetAnotherDummyInterfaceImplementation <int>)), false); Assert.IsNotNull(stringLifetime); Assert.IsNotNull(intLifetime); AssertExtensions.IsInstanceOfType(stringLifetime, typeof(RecoverableLifetime)); AssertExtensions.IsInstanceOfType(intLifetime, typeof(RecoverableLifetime)); Assert.AreNotSame(stringLifetime, intLifetime); }
public void LifetimeStrategyUsesFactoryToGetLifetimePolicyForGenericType() { MockBuilderContext context = CreateContext(); context.PersistentPolicies.Set <ILifetimeFactoryPolicy>( new LifetimeFactoryPolicy <RecoverableLifetime>(), typeof(MyFoo <>)); context.ExecuteBuildUp(typeof(MyFoo <string>), null); MyFoo <string> stringFoo = (MyFoo <string>)context.Existing; context.ExecuteBuildUp(typeof(MyFoo <int>), null); MyFoo <int> intFoo = (MyFoo <int>)context.Existing; ILifetimePolicy stringLifetime = context.Policies.GetNoDefault <ILifetimePolicy>(typeof(MyFoo <string>), false); ILifetimePolicy intLifetime = context.Policies.GetNoDefault <ILifetimePolicy>(typeof(MyFoo <int>), false); Assert.IsNotNull(stringLifetime); Assert.IsNotNull(intLifetime); Assert.IsInstanceOfType(stringLifetime, typeof(RecoverableLifetime)); Assert.IsInstanceOfType(intLifetime, typeof(RecoverableLifetime)); Assert.AreNotSame(stringLifetime, intLifetime); }
/// <summary> /// Called during the chain of responsibility for a build operation. The /// PreBuildUp method is called when the chain is being executed in the /// forward direction. /// </summary> /// <param name="context">Context of the build operation.</param> // FxCop suppression: Validation is done by Guard class public override void PreBuildUp(IBuilderContext context) { Guard.ArgumentNotNull(context, "context"); if (context.Existing != null) { return; } IPolicyList containingPolicyList; ILifetimePolicy lifetimePolicy = GetLifetimePolicy(context, out containingPolicyList); var scope = lifetimePolicy as IScopeLifetimePolicy; if (null != scope && !ReferenceEquals(containingPolicyList, context.PersistentPolicies)) { lifetimePolicy = scope.CreateScope() as ILifetimePolicy; context.PersistentPolicies.Set(lifetimePolicy, context.BuildKey); context.Lifetime.Add(lifetimePolicy); } IRequiresRecovery recovery = lifetimePolicy as IRequiresRecovery; if (recovery != null) { context.RecoveryStack.Add(recovery); } object existing = lifetimePolicy.GetValue(); if (existing != null) { context.Existing = existing; context.BuildComplete = true; } }
public InstanceResult(object instance, ILifetimePolicy lifetimePolicy) { _instance = instance; _lifetimePolicy = lifetimePolicy; }
public override void PreBuildUp(IBuilderContext context) { ILifetimePolicy lifeTime = context.Policies.Get <ILifetimePolicy>(context.BuildKey); base.PreBuildUp(context); }
/// <summary> /// Looks for an existing mapping. if not found, attempts a mapping based on the name of the resolve request. /// </summary> /// <param name="context"> /// The current context for the buildup. /// </param> public override void PreBuildUp(IBuilderContext context) { // Note that this is the same functionality as the default BuildKeyMappingPolicy. The difference is that if we have no policy, we infer one. var policy = context.Policies.Get <IBuildKeyMappingPolicy>(context.BuildKey); if (policy != null) { context.BuildKey = policy.Map(context.BuildKey, context); } else { if (context.BuildKey.Type.IsInterface) { string interFaceName = context.BuildKey.Type.Name; string interfaceNamespace = context.BuildKey.Type.Namespace; // By convention, all of our interfaces start with I, and by convention, the concrete type that they implement is the same name without the I. if (interFaceName.StartsWith("I")) { string newName = interFaceName.Substring(1, interFaceName.Length - 1); string fullName = interfaceNamespace + "." + newName; Type concreteType = null; if (typeCache.ContainsKey(fullName)) { concreteType = typeCache[fullName]; } else { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies.Where(assembly => assembly.FullName.StartsWith("Veracity") || assembly.FullName.StartsWith("Veracity"))) { concreteType = assembly.GetType(fullName); if (concreteType != null) { lock (lockObject) { if (!typeCache.ContainsKey(fullName)) { typeCache.Add(fullName, concreteType); } } break; } } } if (concreteType != null) { NamedTypeBuildKey oldKey = context.BuildKey; NamedTypeBuildKey newBuildKey = new NamedTypeBuildKey(concreteType, null); context.BuildKey = newBuildKey; // look for persistant policies for this interface name ILifetimePolicy lifetime = context.PersistentPolicies.Get <ILifetimePolicy>(oldKey); if (lifetime != null) { context.PersistentPolicies.Set(lifetime, newBuildKey); } } } } } }
public ConcreteTypeResolver(ILifetimePolicy policy) { _policy = policy; }