/// <summary> /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code. /// Builds a <see cref="LogWriter"/> described by the <see cref="LoggingSettings"/> configuration section. /// </summary> /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param> /// <param name="name">The name of the instance to build. It is part of the <see cref="ICustomFactory.CreateObject(IBuilderContext, string, IConfigurationSource, ConfigurationReflectionCache)"/> method, but it is not used in this implementation.</param> /// <param name="configurationSource">The source for configuration objects.</param> /// <param name="reflectionCache">The cache to use retrieving reflection information.</param> /// <returns>A fully initialized instance of <see cref="LogWriter"/>.</returns> public object CreateObject(IBuilderContext context, string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) { LogWriterStructureHolder structureHolder = (LogWriterStructureHolder)LogWriterStructureHolderCustomFactory.Instance.CreateObject(context, name, configurationSource, reflectionCache); LogWriterStructureHolderUpdater structureHolderUpdater = new LogWriterStructureHolderUpdater(configurationSource); LogWriter createdObject = new LogWriter(structureHolder, structureHolderUpdater); structureHolderUpdater.SetLogWriter(createdObject); // add the writer to the locator, if necessary. if (context.Locator != null) { ILifetimeContainer lifetime = context.Locator.Get <ILifetimeContainer>(typeof(ILifetimeContainer), SearchMode.Local); if (lifetime != null) { context.Locator.Add(new DependencyResolutionLocatorKey(typeof(LogWriter), name), createdObject); lifetime.Add(createdObject); } } return(createdObject); }
/// <summary> /// Returns a default instance of type <typeparamref name="T"/> based on configuration information /// from <paramref name="configurationSource"/>. /// </summary> /// <typeparam name="T">The type to build.</typeparam> /// <param name="locator">The locator to be used for this build operation.</param> /// <param name="lifetimeContainer">The lifetime container to be used for this build operation.</param> /// <param name="configurationSource">The source for configuration information.</param> /// <returns>A new instance of <typeparamref name="T"/> or any of it subtypes, or an existing instance /// if type <typeparamref name="T"/> is a singleton that is already present in the <paramref name="locator"/>. /// </returns> public static T BuildUp <T>(IReadWriteLocator locator, ILifetimeContainer lifetimeContainer, IConfigurationSource configurationSource) { if (configurationSource == null) { throw new ArgumentNullException("configurationSource"); } try { return(GetObjectBuilder() .BuildUp <T>(locator, lifetimeContainer, GetPolicies(configurationSource), strategyChain, NamedTypeBuildKey.Make <T>(), null)); } catch (BuildFailedException e) { // look for the wrapped ConfigurationErrorsException, if any, and throw it ConfigurationErrorsException cee = GetConfigurationErrorsException(e); if (cee != null) { throw cee; } // unknown exception, bubble it up throw; } }
public virtual void SetupTest() { TestManager = GetManager(); LifetimeContainer = new FakeLifetimeContainer(); OtherContainer = new FakeLifetimeContainer(); TestObject = new object(); }
/// <summary> /// Initializes an instance of the <see cref="ServiceCollection"/> class. /// </summary> /// <param name="container">The lifetime container the collection will use</param> /// <param name="locator">The locator the collection will use</param> /// <param name="builder">The builder the collection will use</param> /// <param name="parent">The parent collection</param> public ServiceCollection(ILifetimeContainer container, IReadWriteLocator locator, IBuilder <BuilderStage> builder, ServiceCollection parent) { this.builder = builder; this.container = container; this.locator = locator; this.parent = parent; }
private TestableServiceCollection(ILifetimeContainer container, IReadWriteLocator locator, IBuilder <BuilderStage> builder, TestableServiceCollection parent) : base(container, locator, builder, parent) { this.builder = builder; this.container = container; this.locator = locator; }
/// <inheritdoc/> public override void SetValue(object newValue, ILifetimeContainer container = null) { Set(newValue, container); Set = (o, c) => throw new InvalidOperationException("ContainerControlledLifetimeManager can only be set once"); Get = SynchronizedGetValue; TryGet = SynchronizedGetValue; }
/// <inheritdoc/> public override void SetValue(object newValue, ILifetimeContainer container = null) { if (newValue is IDisposable disposable) { container?.Add(disposable); } }
private BuilderContainer(BuilderContainer parent) { builder = new BuilderBase <BuilderStage>(parent.configurator); lifetime = new LifetimeContainer(); locator = new Locator(parent.locator); locator.Add(typeof(ILifetimeContainer), lifetime); }
/// <summary> /// Initializes an instance of the <see cref="ManagedObjectCollection{TItem}"/> class. /// </summary> public ManagedObjectCollection( ILifetimeContainer container, IReadWriteLocator locator, IBuilder <WCSFBuilderStage> builder, SearchMode searchMode, IndexerCreationDelegate indexerCreationDelegate, Predicate <TItem> filter, ManagedObjectCollection <TItem> parentCollection) { _lifetimeContainer = container; _locator = locator; _builder = builder; _searchMode = searchMode; _indexerCreationDelegate = indexerCreationDelegate; _filter = filter; _parentCollection = parentCollection; _container = locator.Get <CompositionContainer>(new DependencyResolutionLocatorKey(typeof(CompositionContainer), null)); if (_container != null) { _container.ObjectAdded += new EventHandler <DataEventArgs <object> >(Container_ItemAdded); _container.ObjectRemoved += new EventHandler <DataEventArgs <object> >(Container_ItemRemoved); } }
private Locator CreateLocator(ILifetimeContainer lifetimeContainer) { Locator locator = new Locator(); locator.Add(typeof(ILifetimeContainer), lifetimeContainer); return(locator); }
public object Resolve(Type type, string name, ImplicitRegistration registration) { unsafe { var thisContext = this; var context = new BuilderContext { Lifetime = Lifetime, Registration = registration, RegistrationType = type, Name = name, Type = registration is ExplicitRegistration containerRegistration ? containerRegistration.Type : type, ExecutePlan = ExecutePlan, ResolvePlan = ResolvePlan, List = List, Overrides = Overrides, DeclaringType = Type, #if !NET40 Parent = new IntPtr(Unsafe.AsPointer(ref thisContext)) #endif }; return(ExecutePlan(registration.BuildChain, ref context)); } }
internal static void Register(this IUnityContainer container, ServiceDescriptor serviceDescriptor, ILifetimeContainer lifetime) { if (serviceDescriptor.ImplementationType != null) { var name = serviceDescriptor.ServiceType.IsGenericTypeDefinition ? UnityContainer.All : null; container.RegisterType(serviceDescriptor.ServiceType, serviceDescriptor.ImplementationType, name, (ITypeLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); } else if (serviceDescriptor.ImplementationFactory != null) { container.RegisterFactory(serviceDescriptor.ServiceType, null, scope => { var serviceProvider = scope.Resolve <IServiceProvider>(); var instance = serviceDescriptor.ImplementationFactory(serviceProvider); return(instance); }, (IFactoryLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); } else if (serviceDescriptor.ImplementationInstance != null) { container.RegisterInstance(serviceDescriptor.ServiceType, null, serviceDescriptor.ImplementationInstance, (IInstanceLifetimeManager)serviceDescriptor.GetLifetime(lifetime)); } else { throw new InvalidOperationException("Unsupported registration type"); } }
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); } } } } }
public override void RemoveValue(ILifetimeContainer container = null) { var disposable = GetValue() as IDisposable; disposable?.Dispose(); HttpContext.Current.Items.Remove(_key); }
/// <summary> /// Initializes an instance of the <see cref="ServiceCollection"/> class. /// </summary> /// <param name="container">The lifetime container the collection will use</param> /// <param name="locator">The locator the collection will use</param> /// <param name="builder">The builder the collection will use</param> /// <param name="parent">The parent collection</param> public ServiceCollection(ILifetimeContainer container, IReadWriteLocator locator, IBuilder <WCSFBuilderStage> builder, IServiceCollection parent) { _builder = builder; _container = container; _locator = locator; _parent = parent; }
public override void SetValue(object newValue, ILifetimeContainer container = null) { if (HttpContext.Current == null) { return; } HttpContext.Current.Items[_key] = newValue; }
protected override void SynchronizedSetValue(object newValue, ILifetimeContainer container) { _value = newValue; if (_value is IDisposable) { _container.Add(new DisposableAction(() => RemoveValue(_container))); } }
/// <summary> /// Initializes a new instance of the <see cref="LocatorNameTypeFactoryBase{T}"/> class with a configuration source /// and a locator. /// </summary> /// <param name="configurationSource">The configuration source to use.</param> protected LocatorNameTypeFactoryBase(IConfigurationSource configurationSource) { this.configurationSource = configurationSource; locator = new Locator(); lifetimeContainer = new LifetimeContainer(); locator.Add(typeof(ILifetimeContainer), lifetimeContainer); }
/// <summary> /// Removes the given object from the backing store. /// </summary> public override void RemoveValue(ILifetimeContainer container = null) { var disposable = GetValue() as IDisposable; disposable?.Dispose(); UnityPerRequestHttpModule.SetValue(_lifetimeKey, null); }
public override object GetValue(ILifetimeContainer container = null) { if (HttpContext.Current == null) { return(null); //effectively makes it TransientLifetimeManager } return(HttpContext.Current.Items[_key]); }
/// <summary> /// Retrieve a value from the backing store associated with this Lifetime policy for the /// current thread. /// </summary> /// <param name="container">Instance of container requesting the value</param> /// <returns>the object desired, or <see langword="null"/> if no such object is currently /// stored for the current thread.</returns> public override object GetValue(ILifetimeContainer container = null) { EnsureValues(); _values.TryGetValue(_key, out var result); return(result); }
/// <summary> /// Stores the given value into backing store for retrieval later. /// </summary> /// <param name="newValue">The object being stored.</param> /// <param name="container">The container this value belongs to.</param> /// <remarks>Setting a value will attempt to release the lock acquired by /// <see cref="SynchronizedLifetimeManager.GetValue"/>.</remarks> public override void SetValue(object newValue, ILifetimeContainer container = null) { _value = newValue; if (_value is IDisposable) { _container.Add(new DisposableAction(() => RemoveValue(_container))); } TryExit(); }
/// <inheritdoc/> public override object GetValue(ILifetimeContainer container = null) { if (null == _values) { return(NoValue); } return(_values.TryGetValue(_key, out var result) ? result : NoValue); }
/// <summary> /// Sobreescrita do método RemoveValue. Remove o item do contexto caso contexto não seja nulo. /// </summary> /// <param name="container"></param> public override void RemoveValue(ILifetimeContainer container = null) { var httpContext = _httpContextAccessor.HttpContext; if (httpContext != null) { httpContext.Items.Remove(_key); } }
/// <summary> /// Sobreescrita do método SetValue. Atribui valor a um item do contexto caso o contexto não seja nulo. /// </summary> /// <param name="newValue"></param> /// <param name="container"></param> public override void SetValue(object newValue, ILifetimeContainer container = null) { var httpContext = _httpContextAccessor.HttpContext; if (httpContext != null) { httpContext.Items[_key] = newValue; } }
public override object GetValue(ILifetimeContainer container = null) { if (HttpContext.Current == null) { throw new InvalidOperationException("HttpContext not available"); } return(HttpContext.Current.Items[_key]); }
public TypedObjectsCollection Add(Type type, ILifetimeContainer container) { if (!this.ContainsKey(type)) { this.Add(type, new TypedObject()); } this[type].Add(container); return(this); }
public TTypeToBuild BuildUp <TTypeToBuild>(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, object buildKey, object existing) { return((TTypeToBuild)BuildUp(locator, lifetime, policies, strategies, buildKey, existing)); }
/// <summary> /// Retrieve a value from the backing store associated with this Lifetime policy. /// </summary> /// <returns>the object desired, or null if no such object is currently stored.</returns> /// <remarks>Calls to this method acquire a lock which is released only if a non-null value /// has been set for the lifetime manager.</remarks> public override object GetValue(ILifetimeContainer container = null) { Monitor.Enter(_lockObj); if (_value != null) { Monitor.Exit(_lockObj); } return(_value); }
public override object GetValue(ILifetimeContainer container = null) { object value; if (ObjectDictionary.TryGetValue(objectKey, out value)) { return(value); } return(null); }
/// <summary> /// Create a new <see cref="BuilderContext"/> using the explicitly provided /// values. /// </summary> /// <param name="chain">The <see cref="IStrategyChain"/> to use for this context.</param> /// <param name="lifetime">The <see cref="ILifetimeContainer"/> to use for this context.</param> /// <param name="persistentPolicies">The set of persistent policies to use for this context.</param> /// <param name="transientPolicies">The set of transient policies to use for this context. It is /// the caller's responsibility to ensure that the transient and persistent policies are properly /// combined.</param> /// <param name="buildKey">Build key for this context.</param> /// <param name="existing">Existing object to build up.</param> public BuilderContext(IStrategyChain chain, ILifetimeContainer lifetime, IPolicyList persistentPolicies, IPolicyList transientPolicies, NamedTypeBuildKey buildKey, object existing) { this.chain = chain; this.lifetime = lifetime; this.persistentPolicies = persistentPolicies; this.policies = transientPolicies; this.originalBuildKey = buildKey; this.BuildKey = buildKey; this.Existing = existing; }
/// <summary> /// Create a new <see cref="BuilderContext"/> using the explicitly provided /// values. /// </summary> /// <param name="chain">The <see cref="IStrategyChain"/> to use for this context.</param> /// <param name="locator">The <see cref="IReadWriteLocator"/> to use for this context.</param> /// <param name="lifetime">The <see cref="ILifetimeContainer"/> to use for this context.</param> /// <param name="persistentPolicies">The set of persistent policies to use for this context.</param> /// <param name="transientPolicies">The set of transient policies to use for this context. It is /// the caller's responsibility to ensure that the transient and persistent policies are properly /// combined.</param> /// <param name="buildKey">Build key for this context.</param> /// <param name="existing">Existing object to build up.</param> public BuilderContext(IStrategyChain chain, IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList persistentPolicies, IPolicyList transientPolicies, object buildKey, object existing) { this.chain = chain; this.lifetime = lifetime; this.locator = locator; this.persistentPolicies = persistentPolicies; this.policies = transientPolicies; this.originalBuildKey = buildKey; this.buildKey = buildKey; this.existing = existing; }
/// <summary> /// Create a new <see cref="BuilderContext"/> using the explicitly provided /// values. /// </summary> /// <param name="chain">The <see cref="IStrategyChain"/> to use for this context.</param> /// <param name="lifetime">The <see cref="ILifetimeContainer"/> to use for this context.</param> /// <param name="persistentPolicies">The set of persistent policies to use for this context.</param> /// <param name="transientPolicies">The set of transient policies to use for this context. It is /// the caller's responsibility to ensure that the transient and persistent policies are properly /// combined.</param> /// <param name="buildKey">Build key for this context.</param> /// <param name="existing">Existing object to build up.</param> public BuilderContext(IStrategyChain chain, ILifetimeContainer lifetime, IPolicyList persistentPolicies, IPolicyList transientPolicies, NamedTypeBuildKey buildKey, object existing) { this.chain = chain; this.lifetime = lifetime; this.persistentPolicies = persistentPolicies; this.policies = transientPolicies; this.originalBuildKey = buildKey; this.BuildKey = buildKey; this.Existing = existing; this.resolverOverrides = new CompositeResolverOverride(); this.ownsOverrides = true; }
/// <summary> /// Initialize a new instance of the <see cref = "BuilderContext" /> class with a <see cref = "IStrategyChain" />, /// <see cref = "ILifetimeContainer" />, <see cref = "IPolicyList" /> and the /// build key used to start this build operation. /// </summary> /// <param name = "chain">The <see cref = "IStrategyChain" /> to use for this context.</param> /// <param name = "lifetime">The <see cref = "ILifetimeContainer" /> to use for this context.</param> /// <param name = "policies">The <see cref = "IPolicyList" /> to use for this context.</param> /// <param name = "originalBuildKey">Build key to start building.</param> /// <param name = "existing">The existing object to build up.</param> public BuilderContext(IStrategyChain chain, ILifetimeContainer lifetime, IPolicyList policies, NamedTypeBuildKey originalBuildKey, object existing) { this.chain = chain; this.lifetime = lifetime; this.originalBuildKey = originalBuildKey; BuildKey = originalBuildKey; persistentPolicies = policies; this.policies = new PolicyList(persistentPolicies); Existing = existing; }
private TestableServiceCollection(ILifetimeContainer container, IReadWriteLocator locator, IBuilder<WCSFBuilderStage> builder, TestableServiceCollection parent) : base(container, locator, builder, parent) { this.builder = builder; this.container = container; this.locator = locator; }
public TestableServiceCollection(ILifetimeContainer container, IReadWriteLocator locator, IBuilder<WCSFBuilderStage> builder) : this(container, locator, builder, null) { }
private Locator CreateLocator(ILifetimeContainer lifetimeContainer) { Locator locator = new Locator(); locator.Add(typeof (ILifetimeContainer), lifetimeContainer); return locator; }
public ParentMarkerPolicy(ILifetimeContainer lifetime) { this.lifetime = lifetime; }
/// <summary> /// Create a new <see cref="BuilderContext"/> using the explicitly provided /// values. /// </summary> /// <param name="chain">The <see cref="IStrategyChain"/> to use for this context.</param> /// <param name="lifetime">The <see cref="ILifetimeContainer"/> to use for this context.</param> /// <param name="persistentPolicies">The set of persistent policies to use for this context.</param> /// <param name="transientPolicies">The set of transient policies to use for this context. It is /// the caller's responsibility to ensure that the transient and persistent policies are properly /// combined.</param> /// <param name="buildKey">Build key for this context.</param> /// <param name="resolverOverrides">The resolver overrides.</param> protected BuilderContext(IStrategyChain chain, ILifetimeContainer lifetime, IPolicyList persistentPolicies, IPolicyList transientPolicies, NamedTypeBuildKey buildKey, CompositeResolverOverride resolverOverrides) { this.chain = chain; this.lifetime = lifetime; this.persistentPolicies = persistentPolicies; this.policies = transientPolicies; this.originalBuildKey = buildKey; this.BuildKey = buildKey; this.Existing = null; this.resolverOverrides = resolverOverrides; this.ownsOverrides = false; }