public void Configure <T>(Func <T> componentFactory, DependencyLifecycle dependencyLifecycle) { var componentType = typeof(T); if (HasComponent(componentType)) { return; } var serviceTypes = GetAllServiceTypesFor(componentType); SingletonInstanceStore instanceStore = null; foreach (var t in serviceTypes) { if (defaultInstances.Contains(t)) { container.RegisterType(t, Guid.NewGuid().ToString(), GetLifetimeManager(dependencyLifecycle, ref instanceStore), new InjectionFactory(unityContainer => componentFactory())); } else { container.RegisterType(t, GetLifetimeManager(dependencyLifecycle, ref instanceStore), new InjectionFactory(unityContainer => componentFactory())); defaultInstances.Add(t); } } }
public void Configure(Type concreteComponent, DependencyLifecycle dependencyLifecycle) { if (HasComponent(concreteComponent)) { return; } SingletonInstanceStore instanceStore = null; RegisterDefaultInstances(concreteComponent, () => GetLifetimeManager(dependencyLifecycle, ref instanceStore)); }
static LifetimeManager GetLifetimeManager(DependencyLifecycle dependencyLifecycle, ref SingletonInstanceStore instanceStore) { switch (dependencyLifecycle) { case DependencyLifecycle.InstancePerCall: return(new TransientLifetimeManager()); case DependencyLifecycle.SingleInstance: if (instanceStore == null) { instanceStore = new SingletonInstanceStore(); } return(new SingletonLifetimeManager(instanceStore)); case DependencyLifecycle.InstancePerUnitOfWork: return(new HierarchicalLifetimeManager()); } throw new ArgumentException("Unhandled lifecycle - " + dependencyLifecycle); }
public SingletonLifetimeManager(SingletonInstanceStore instanceStore) { this.instanceStore = instanceStore; }