public void if_an_enumerable_type_and_there_is_no_exact_match_by_type_try_ienumerable_of_the_element_type()
        {
            var collection = new DependencyCollection();
            var instance = new EnumerableInstance(new Instance[0]);

            collection.Add(typeof (IEnumerable<IGateway>), instance);

            collection.FindByTypeOrName(typeof (IList<IGateway>), null).ShouldBeTheSameAs(instance);
            collection.FindByTypeOrName(typeof (List<IGateway>), null).ShouldBeTheSameAs(instance);
            collection.FindByTypeOrName(typeof (IGateway[]), null).ShouldBeTheSameAs(instance);
        }
        public void if_an_enumerable_type_and_there_is_no_exact_match_by_type_try_ienumerable_of_the_element_type()
        {
            var collection = new DependencyCollection();
            var instance   = new EnumerableInstance(new Instance[0]);

            collection.Add(typeof(IEnumerable <IGateway>), instance);

            collection.FindByTypeOrName(typeof(IList <IGateway>), null).ShouldBeTheSameAs(instance);
            collection.FindByTypeOrName(typeof(List <IGateway>), null).ShouldBeTheSameAs(instance);
            collection.FindByTypeOrName(typeof(IGateway[]), null).ShouldBeTheSameAs(instance);
        }
        public void add_and_retrieve_by_type_only()
        {
            var collection = new DependencyCollection();
            collection.Add(typeof (IService), service1);
            collection.Add(typeof (IGateway), gateway3);

            collection.FindByTypeOrName(typeof (IGateway), "foo")
                .ShouldBe(gateway3);

            collection.FindByTypeOrName(typeof (IService), "anything")
                .ShouldBe(service1);
        }
        public void add_and_retrieve_by_name_only()
        {
            var collection = new DependencyCollection();
            collection.Add("foo", gateway1);
            collection.Add("bar", gateway2);

            collection.FindByTypeOrName(typeof (IGateway), "foo")
                .ShouldBeTheSameAs(gateway1);

            collection.FindByTypeOrName(typeof (IGateway), "bar")
                .ShouldBeTheSameAs(gateway2);
        }
        public void add_and_retrieve_by_type_only()
        {
            var collection = new DependencyCollection();

            collection.Add(typeof(IService), service1);
            collection.Add(typeof(IGateway), gateway3);

            collection.FindByTypeOrName(typeof(IGateway), "foo")
            .ShouldBe(gateway3);

            collection.FindByTypeOrName(typeof(IService), "anything")
            .ShouldBe(service1);
        }
        public void add_and_retrieve_by_name_only()
        {
            var collection = new DependencyCollection();

            collection.Add("foo", gateway1);
            collection.Add("bar", gateway2);

            collection.FindByTypeOrName(typeof(IGateway), "foo")
            .ShouldBeTheSameAs(gateway1);

            collection.FindByTypeOrName(typeof(IGateway), "bar")
            .ShouldBeTheSameAs(gateway2);
        }
        public void get_by_name_with_nothing_returns_null()
        {
            var collection = new DependencyCollection();

            collection.FindByTypeOrName(typeof(IGateway), "foo")
            .ShouldBeNull();
        }
        public static ConstructorStep BuildConstructorStep(Type pluggedType, ConstructorInfo constructor,
                                                           DependencyCollection dependencies, Policies policies)
        {
            var ctor = constructor ?? policies.SelectConstructor(pluggedType, dependencies);

            if (ctor == null)
            {
                throw new StructureMapConfigurationException(
                          "No public constructor could be selected for concrete type " + pluggedType.GetFullName());
            }

            var ctorStep  = new ConstructorStep(ctor);
            var multiples = findTypesWithMultipleParametersRequired(ctor);

            var ctorDependencies = ctor
                                   .GetParameters()
                                   .Select(x => {
                var dependency = multiples.Contains(x.ParameterType)
                        ? dependencies.FindByTypeAndName(x.ParameterType, x.Name)
                        : dependencies.FindByTypeOrName(x.ParameterType, x.Name);

                if (dependency == null && ((x.DefaultValue != null && x.DefaultValue.GetType().Name != "DBNull")))
                {
                    dependency = x.DefaultValue;
                }

                return(SourceFor(ConstructorArgument, x.Name, x.ParameterType, dependency));
            });

            ctorStep.Add(ctorDependencies);

            return(ctorStep);
        }
        private static void determineSetterSource(DependencyCollection dependencies, Policies policies,
                                                  PropertyInfo setter,
                                                  IHasSetters plan)
        {
            var isMandatory = policies.IsMandatorySetter(setter);

            object dependency = null;

            if (setter.PropertyType.IsSimple() && !isMandatory)
            {
                dependency = dependencies.FindByTypeAndName(setter.PropertyType, setter.Name);
            }
            else
            {
                dependency = dependencies.FindByTypeOrName(setter.PropertyType, setter.Name);
            }

            if (dependency == null && !isMandatory)
            {
                return;
            }

            var source = SourceFor(SetterProperty, setter.Name, setter.PropertyType, dependency);

            plan.Add(setter.PropertyType, setter, source);
        }
 public static bool HasMissingPrimitives(ConstructorInfo ctor, DependencyCollection dependencies)
 {
     return ctor
         .GetParameters()
         .Where(x => x.ParameterType.IsSimple())
         .Any(param => dependencies.FindByTypeOrName(param.ParameterType, param.Name) == null);
 }
        public static ConstructorStep BuildConstructorStep(Type pluggedType, ConstructorInfo constructor,
            DependencyCollection dependencies, Policies policies)
        {
            var ctor = constructor ?? policies.SelectConstructor(pluggedType);
            if (ctor == null)
            {
                throw new StructureMapConfigurationException(
                    "No public constructor could be selected for concrete type " + pluggedType.GetFullName());
            }

            var ctorStep = new ConstructorStep(ctor);
            var multiples = findTypesWithMultipleParametersRequired(ctor);

            var ctorDependencies = ctor
                .GetParameters()
                .Select(x => {
                    var dependency = multiples.Contains(x.ParameterType)
                        ? dependencies.FindByTypeAndName(x.ParameterType, x.Name)
                        : dependencies.FindByTypeOrName(x.ParameterType, x.Name);

                    if (dependency == null && ( (x.DefaultValue != null && x.DefaultValue.GetType().Name != "DBNull")))
                    {
                        dependency = x.DefaultValue;
                    }

                    return SourceFor(ConstructorArgument, x.Name, x.ParameterType, dependency);
                });

            ctorStep.Add(ctorDependencies);

            return ctorStep;
        }
        public void use_most_specific_criteria_if_possible()
        {
            var collection = new DependencyCollection();
            collection.Add("foo", typeof (IGateway), gateway1);
            collection.Add("bar", typeof (IGateway), gateway2);
            collection.Add("bar", typeof (IService), service1);

            collection.FindByTypeOrName(typeof (IGateway), "foo")
                .ShouldBeTheSameAs(gateway1);

            collection.FindByTypeOrName(typeof (IGateway), "bar")
                .ShouldBeTheSameAs(gateway2);

            collection.FindByTypeOrName(typeof (IService), "bar")
                .ShouldBeTheSameAs(service1);
        }
        public void use_most_specific_criteria_if_possible()
        {
            var collection = new DependencyCollection();

            collection.Add("foo", typeof(IGateway), gateway1);
            collection.Add("bar", typeof(IGateway), gateway2);
            collection.Add("bar", typeof(IService), service1);

            collection.FindByTypeOrName(typeof(IGateway), "foo")
            .ShouldBeTheSameAs(gateway1);

            collection.FindByTypeOrName(typeof(IGateway), "bar")
            .ShouldBeTheSameAs(gateway2);

            collection.FindByTypeOrName(typeof(IService), "bar")
            .ShouldBeTheSameAs(service1);
        }
        public void can_override_and_last_one_in_wins()
        {
            var collection = new DependencyCollection();
            collection.Add("foo", typeof(IGateway), gateway1);
            collection.Add("foo", typeof(IGateway), gateway2);

            collection.FindByTypeOrName(typeof(IGateway), "foo")
                .ShouldBeTheSameAs(gateway2);
        }
        public void can_override_and_last_one_in_wins()
        {
            var collection = new DependencyCollection();

            collection.Add("foo", typeof(IGateway), gateway1);
            collection.Add("foo", typeof(IGateway), gateway2);

            collection.FindByTypeOrName(typeof(IGateway), "foo")
            .ShouldBeTheSameAs(gateway2);
        }
Exemple #16
0
        private static void determineSetterSource(DependencyCollection dependencies, Policies policies,
            PropertyInfo setter,
            IHasSetters plan)
        {
            var isMandatory = policies.IsMandatorySetter(setter);

            object dependency = null;
            if (setter.PropertyType.IsSimple() && !isMandatory)
            {
                dependency = dependencies.FindByTypeAndName(setter.PropertyType, setter.Name);
            }
            else
            {
                dependency = dependencies.FindByTypeOrName(setter.PropertyType, setter.Name);
            }

            if (dependency == null && !isMandatory) return;

            var source = SourceFor(SetterProperty, setter.Name, setter.PropertyType, dependency);
            plan.Add(setter.PropertyType, setter, source);
        }
 public void get_by_name_with_nothing_returns_null()
 {
     var collection = new DependencyCollection();
     collection.FindByTypeOrName(typeof (IGateway), "foo")
         .ShouldBeNull();
 }