public DependencyHolder(IDIContainer container, Type dependencyType) { this.Container = container; this.DependencyType = dependencyType; this.InstanceType = InstanceType.Static; this.LifetimeManager = InstanceLifetimeManager.GetInstanceLifetimeManager(this.Container, DependencyType, this.InstanceType); }
public static void RegisterInstance(this IContainer container, Type type, object value, string key = null) { if (container == null) { throw new ArgumentNullException(nameof(container)); } var lifetimeManager = new InstanceLifetimeManager(value, container); container.Register(lifetimeManager, type, key); }
public void InstanceLifetimeManager_GetInstanceLifetimeManager_Test() { //arrage Mock <IDIContainer> mockContainer = new Mock <IDIContainer>(); InstanceLifetimeManager localInstanceLifetimeManager, staticInstanceLifetimeManager, invalidLifetimeManager; //act localInstanceLifetimeManager = InstanceLifetimeManager.GetInstanceLifetimeManager(mockContainer.Object, typeof(foo), InstanceType.Local); staticInstanceLifetimeManager = InstanceLifetimeManager.GetInstanceLifetimeManager(mockContainer.Object, typeof(foo), InstanceType.Static); invalidLifetimeManager = InstanceLifetimeManager.GetInstanceLifetimeManager(mockContainer.Object, typeof(foo), InstanceType.Invalid); //assert Assert.IsInstanceOfType(localInstanceLifetimeManager, typeof(LocalInstanceLifetimeManager)); Assert.IsInstanceOfType(staticInstanceLifetimeManager, typeof(StaticInstanceLifetimeManager)); Assert.IsNull(invalidLifetimeManager); }
/// <inheritdoc /> IUnityContainer IUnityContainer.RegisterInstance(Type type, string name, object instance, IInstanceLifetimeManager lifetimeManager) { var mappedToType = instance?.GetType(); var typeFrom = type ?? mappedToType; LifetimeManager manager = (null != lifetimeManager) ? (LifetimeManager)lifetimeManager : InstanceLifetimeManager.CreateLifetimePolicy(); try { // Validate input if (null == typeFrom) { throw new InvalidOperationException($"At least one of Type arguments '{nameof(type)}' or '{nameof(instance)}' must be not 'null'"); } if (manager.InUse) { throw new InvalidOperationException(LifetimeManagerInUse); } manager.SetValue(instance, LifetimeContainer); // Create registration and add to appropriate storage var container = manager is SingletonLifetimeManager ? _root : this; var registration = new ContainerRegistration(null, mappedToType, manager); if (manager is ContainerControlledLifetimeManager lifeteime) { lifeteime.Scope = container; } // Add or replace existing var previous = container.Register(typeFrom, name, registration); if (previous is ContainerRegistration old && old.LifetimeManager is IDisposable disposable) { // Dispose replaced lifetime manager container.LifetimeContainer.Remove(disposable); disposable.Dispose(); } // If Disposable add to container's lifetime if (manager is IDisposable disposableManager) { container.LifetimeContainer.Add(disposableManager); } // Check what strategies to run registration.BuildChain = _strategiesChain.ToArray() .Where(strategy => strategy.RequiredToResolveInstance(this, registration)) .ToArray(); // Raise event container.RegisteringInstance?.Invoke(this, new RegisterInstanceEventArgs(typeFrom, instance, name, manager)); } catch (Exception ex) { var parts = new List <string>(); if (null != name) { parts.Add($" '{name}'"); } if (null != lifetimeManager && !(lifetimeManager is TransientLifetimeManager)) { parts.Add(lifetimeManager.ToString()); } var message = $"Error in RegisterInstance<{typeFrom?.Name}>({string.Join(", ", parts)})"; throw new InvalidOperationException(message, ex); } return(this); }