/// <inheritdoc/> public override void SetValue(object newValue, ILifetimeContainer container = null) { if (newValue is IDisposable disposable) { container?.Add(disposable); } }
/// <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); }
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); } } } } }
private void RegisterObject(IBuilderContext context, string name, CacheManager createdObject) { if (context.Locator != null) { ILifetimeContainer lifetime = context.Locator.Get <ILifetimeContainer>(typeof(ILifetimeContainer), SearchMode.Local); if (lifetime != null) { context.Locator.Add(new DependencyResolutionLocatorKey(typeof(CacheManager), name), createdObject); lifetime.Add(createdObject); } } }
public object CreateObject(Microsoft.Practices.ObjectBuilder.IBuilderContext context, string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) { TerminologyServiceSettings settings = configurationSource.GetSection(TerminologyServiceSettings.SectionName) as TerminologyServiceSettings; Check.Assert(settings != null, "settings must not be null."); TerminologyServiceData data = settings.TerminologyServiceProviders.Get(name) as TerminologyServiceData; if (data == null) throw new ConfigurationErrorsException("Unable to find Terminology service provider. " + name); Dictionary<string, ICodeSetAccess> codeSetAccessDictionary = new Dictionary<string, ICodeSetAccess>(); foreach (CodeSetAccessProviderData codeSetAccessProviderData in data.codeSetAccessProviders) { codeSetAccessDictionary.Add(codeSetAccessProviderData.Name, (ICodeSetAccess)CodeSetAccessProviderCustomFactory.Instance.Create (context, codeSetAccessProviderData, configurationSource, reflectionCache)); } Dictionary<string, ITerminologyAccess> terminologyAccessDictionary = new Dictionary<string, ITerminologyAccess>(); foreach (TerminologyAccessProviderData terminologyAccessProviderData in data.TerminologyAccessProviders) { terminologyAccessDictionary.Add(terminologyAccessProviderData.Name, (ITerminologyAccess)TerminologyAccessProviderCustomFactory.Instance.Create (context, terminologyAccessProviderData, configurationSource, reflectionCache)); } ITerminologyService terminologyService = new TerminologyService(terminologyAccessDictionary, codeSetAccessDictionary); if (context.Locator != null) { ILifetimeContainer lifetime = context.Locator.Get<ILifetimeContainer>(typeof(ILifetimeContainer), SearchMode.Local); if (lifetime != null) { context.Locator.Add(new DependencyResolutionLocatorKey(typeof(ITerminologyServiceProvider), name), terminologyService); lifetime.Add(terminologyService); } } return terminologyService; }
/// <summary> /// Adds a service that will not be created until the first time it is requested. /// </summary> /// <param name="serviceType">The type of service</param> /// <param name="registerAs">The type to register the service as</param> public void AddOnDemand(Type serviceType, Type registerAs) { Guard.ArgumentNotNull(serviceType, "serviceType"); if (registerAs == null) { registerAs = serviceType; } DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(registerAs, null); if (locator.Contains(key, SearchMode.Local)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.DuplicateService, registerAs.FullName)); } DemandAddPlaceholder placeholder = new DemandAddPlaceholder(serviceType); locator.Add(key, placeholder); container.Add(placeholder); }
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); } } } } }
/// <summary> /// Implementation of <see cref="IBuilderStrategy.BuildUp"/>. /// </summary> /// <param name="context">The build context.</param> /// <param name="typeToBuild">The type of the object being built.</param> /// <param name="existing">The existing instance of the object.</param> /// <param name="idToBuild">The ID of the object being built.</param> /// <returns>The built object.</returns> public override object BuildUp(IBuilderContext context, Type typeToBuild, object existing, string idToBuild) { DependencyResolutionLocatorKey key = null; ILifetimeContainer lifeTime = GetLifetime(context); if (context.Locator != null) { key = new DependencyResolutionLocatorKey(typeToBuild, idToBuild); if (context.Locator.Contains(key, SearchMode.Local)) { return(context.Locator.Get(key)); } } existing = base.BuildUp(context, typeToBuild, existing, idToBuild); if (context.Locator != null && lifeTime != null && IsSingleton(context, key)) { context.Locator.Add(key, existing); lifeTime.Add(existing); } return(existing); }
public void AddToLifetime(object o) { lifetime.Add(o); }
public void Add(object key, object value) { container.Add(value); Decoratee.Add(key, value); }
/// <summary> /// See <see cref="ILifetimeContainer.Add"/>. /// </summary> void ILifetimeContainer.Add(object item) { lifetime.Add(item); }