public FactoryRegistration(Func <PoorMansIoC, object> factory, Type concreteType, ComponentLifetime lifetime, Type implementedType) { ConcreteType = concreteType; Factory = factory; ImplementedType = implementedType; Lifetime = lifetime; }
public FactoryRegistration(Func<PoorMansIoC, object> factory, Type concreteType, ComponentLifetime lifetime, Type implementedType) { ConcreteType = concreteType; Factory = factory; ImplementedType = implementedType; Lifetime = lifetime; }
private void RegisterFactoryDelegate <T>(Func <PoorMansIoC, T> factory, ComponentLifetime componentLifetime, Type[] implementedTypes) { var providedTypes = implementedTypes.None() ? new[] { typeof(T) } : implementedTypes; foreach (var type in providedTypes) { _registrations.Add(new FactoryRegistration(c => factory(c), typeof(T), componentLifetime, type)); } }
public void RegisterType <T>(ComponentLifetime lifetime, params Type[] implementedTypes) { foreach (var t in implementedTypes) { if (!t.IsAssignableFrom(typeof(T))) { throw new ArgumentException("Concrete type {0} is not assignable to {1}".FormatWith(typeof(T).FullName, t.FullName)); } } RegisterType(typeof(T), lifetime, implementedTypes); }
public void Register <T>(Func <PoorMansIoC, T> factory, ComponentLifetime componentLifetime, params Type[] implementedTypes) { foreach (var t in implementedTypes) { if (!t.IsAssignableFrom(typeof(T))) { throw new ArgumentException("Factory return type {0} is not assignable to {1}".FormatWith(typeof(T).FullName, t.FullName)); } } RegisterFactoryDelegate(factory, componentLifetime, implementedTypes); }
private object ConstructObject(Type type, ComponentLifetime componentLifetime, params object[] overrides) { var registration = _registrations .Where(r => r.ImplementedType == type) .First(); object instance = null; var factoryRegistration = registration as FactoryRegistration; if (factoryRegistration != null) { instance = factoryRegistration.Factory(this); } var typedRegistration = registration as TypedRegistration; if (typedRegistration != null) { var constructorInfo = typedRegistration.ConcreteType .GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .Single(); var args = constructorInfo .GetParameters() .Select(p => Resolve(p.ParameterType, overrides)) .ToArray(); instance = constructorInfo.Invoke(args); } if (instance == null) { throw new DependencyResolutionException("No registration was found for type {0}".FormatWith(type.FullName)); } if (componentLifetime == ComponentLifetime.SingleInstance) { _singleInstanceComponents.Add(instance); var disposable = instance as IDisposable; if (disposable != null) { _garbageMan.Add(disposable); } } return(instance); }
public void RegisterType(Type concreteType, ComponentLifetime lifetime, params Type[] implementedTypes) { if (concreteType.IsInterface) { throw new ArgumentException("An interface was supplied where there should have been a concrete type"); } if (implementedTypes.Any(it => !it.IsAssignableFrom(concreteType))) { throw new ArgumentException("One or more of the implemented types is not actually implemented by this concrete type."); } var advertisedTypes = implementedTypes.None() ? new[] { concreteType } : implementedTypes; advertisedTypes.Select(t => new TypedRegistration(concreteType, t, lifetime)) .Do(r => _registrations.Add(r)) .Done(); }
public TypedRegistration(Type concreteType, Type implementedType, ComponentLifetime lifetime) { ConcreteType = concreteType; ImplementedType = implementedType; Lifetime = lifetime; }