Exemplo n.º 1
0
 public FactoryRegistration(Func <PoorMansIoC, object> factory, Type concreteType, ComponentLifetime lifetime, Type implementedType)
 {
     ConcreteType    = concreteType;
     Factory         = factory;
     ImplementedType = implementedType;
     Lifetime        = lifetime;
 }
Exemplo n.º 2
0
 public FactoryRegistration(Func<PoorMansIoC, object> factory, Type concreteType, ComponentLifetime lifetime, Type implementedType)
 {
     ConcreteType = concreteType;
     Factory = factory;
     ImplementedType = implementedType;
     Lifetime = lifetime;
 }
Exemplo n.º 3
0
        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));
            }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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();
        }
Exemplo n.º 8
0
 public TypedRegistration(Type concreteType, Type implementedType, ComponentLifetime lifetime)
 {
     ConcreteType = concreteType;
     ImplementedType = implementedType;
     Lifetime = lifetime;
 }
Exemplo n.º 9
0
 public TypedRegistration(Type concreteType, Type implementedType, ComponentLifetime lifetime)
 {
     ConcreteType    = concreteType;
     ImplementedType = implementedType;
     Lifetime        = lifetime;
 }