/// <summary> /// Attaches to a <c>Disposed</c> event in the object being built. /// </summary> public override object BuildUp(IBuilderContext context, Type typeToBuild, object existing, string idToBuild) { object result = base.BuildUp(context, typeToBuild, existing, idToBuild); ISingletonPolicy policy = context.Policies.Get <ISingletonPolicy>(typeToBuild, idToBuild); if (result != null && policy != null && policy.IsSingleton) { IComponent component = result as IComponent; if (component != null) { component.Disposed += new DisposedHandlerClosure(context.Locator, new DependencyResolutionLocatorKey(typeToBuild, idToBuild)).OnDisposed; } else { EventInfo disposedEvent = result.GetType().GetEvent("Disposed"); if (disposedEvent != null) { try { disposedEvent.AddEventHandler(result, new EventHandler( new DisposedHandlerClosure(context.Locator, new DependencyResolutionLocatorKey(typeToBuild, idToBuild)).OnDisposed)); } catch { // Disposed event is not an EventHandler } } } } return(result); }
public override object BuildUp(IBuilderContext context, object buildKey, object existing) { if (context.Locator == null || context.Lifetime == null) { return(base.BuildUp(context, buildKey, existing)); } if (context.Locator.Contains(buildKey)) { return(context.Locator.Get(buildKey)); } existing = base.BuildUp(context, buildKey, existing); ISingletonPolicy singletonPolicy = context.Policies.Get <ISingletonPolicy>(buildKey); if (singletonPolicy != null && singletonPolicy.IsSingleton) { context.Locator.Add(buildKey, existing); context.Lifetime.Add(existing); } return(existing); }
private static void RegisterObject(IBuilderContext context, Type typeToBuild, object existing, string idToBuild) { if (context.Locator != null) { ILifetimeContainer lifetime = context.Locator.Get <ILifetimeContainer>(typeof(ILifetimeContainer), SearchMode.Local); if (lifetime != null) { ISingletonPolicy singletonPolicy = context.Policies.Get <ISingletonPolicy>(typeToBuild, idToBuild); if (singletonPolicy != null && singletonPolicy.IsSingleton) { lock (context.Locator) { context.Locator.Add(new DependencyResolutionLocatorKey(typeToBuild, idToBuild), existing); } lock (lifetime) { lifetime.Add(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> // FxCop suppression: Validation is done by Guard class public override void PostBuildUp(IBuilderContext context) { base.PostBuildUp(context); if (context.Locator != null && context.Lifetime != null) { ISingletonPolicy singletonPolicy = context.Policies.Get <ISingletonPolicy>(context.BuildKey); if (singletonPolicy != null && singletonPolicy.IsSingleton) { context.Locator.Add(context.BuildKey, context.Existing); context.Lifetime.Add(context.Existing); } } }
private void RegisterObject(IBuilderContext context, Type typeToBuild, object existing, string idToBuild) { if (context.Locator != null) { ILifetimeContainer lifetime = context.Locator.Get <ILifetimeContainer>(typeof(ILifetimeContainer), SearchMode.Local); if (lifetime != null) { ISingletonPolicy singletonPolicy = context.Policies.Get <ISingletonPolicy>(typeToBuild, idToBuild); if (singletonPolicy != null && singletonPolicy.IsSingleton) { context.Locator.Add(new DependencyResolutionLocatorKey(typeToBuild, idToBuild), existing); lifetime.Add(existing); if (TraceEnabled(context)) { TraceBuildUp(context, typeToBuild, idToBuild, Properties.Resources.SingletonRegistered); } } } } }
private static bool IsSingleton(IBuilderContext context, DependencyResolutionLocatorKey key) { ISingletonPolicy policy = context.Policies.Get <ISingletonPolicy>(key.Type, key.ID); return(policy != null && policy.IsSingleton); }