示例#1
0
        void IDependencyVisitor.List(ListDependency dependency)
        {
            var elements = dependency.Items.Select(instanceFor).ToArray();
            var instance = new EnumerableInstance(elements);

            Dependencies.Add(dependency.DependencyType, instance);
        }
示例#2
0
 public void is_enumerable()
 {
     EnumerableInstance.IsEnumerable(typeof(IWidget[])).ShouldBeTrue();
     EnumerableInstance.IsEnumerable(typeof(IList <IWidget>)).ShouldBeTrue();
     EnumerableInstance.IsEnumerable(typeof(IEnumerable <IWidget>)).ShouldBeTrue();
     EnumerableInstance.IsEnumerable(typeof(List <IWidget>)).ShouldBeTrue();
     EnumerableInstance.IsEnumerable(typeof(IWidget)).ShouldBeFalse();
 }
示例#3
0
        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 PluginFamily Build(Type type)
        {
            if (EnumerableInstance.IsEnumerable(type))
            {
                var family = new PluginFamily(type);
                family.SetDefault(new AllPossibleInstance(type));

                return(family);
            }

            return(null);
        }
示例#5
0
        public void to_dependency_source_as_list_with_explicit_values()
        {
            var i1             = new FakeInstance();
            var i2             = new FakeInstance();
            var i3             = new FakeInstance();
            var enumerableType = typeof(List <IGateway>);
            var source         = new EnumerableInstance(new Instance[] { i1, i2, i3 })
                                 .ToDependencySource(enumerableType)
                                 .ShouldBeOfType <ListDependencySource>();

            source.ItemType.ShouldBe(typeof(IGateway));
            source.Items.ShouldHaveTheSameElementsAs(i1.DependencySource, i2.DependencySource, i3.DependencySource);
        }
示例#6
0
        public void build_children_to_a_list()
        {
            var children = new Instance[]
            {
                new SmartInstance <ColorWidget>().Ctor <string>("color").Is("red"),
                new SmartInstance <ColorWidget>().Ctor <string>("color").Is("green"),
                new ObjectInstance(new AWidget())
            };

            var theInstance = new EnumerableInstance(children);
            var list        =
                theInstance.Build <IList <IWidget> >(new StubBuildSession()).As <IList <IWidget> >();

            list.Count.ShouldBe(3);

            list[0].ShouldBeOfType <ColorWidget>().Color.ShouldBe("red");
            list[2].ShouldBeOfType <AWidget>();
        }
示例#7
0
        public void build_children_to_an_array()
        {
            var children = new Instance[]
            {
                new SmartInstance <ColorWidget>().Ctor <string>("color").Is("red"),
                new SmartInstance <ColorWidget>().Ctor <string>("color").Is("green"),
                new ObjectInstance(new AWidget())
            };

            var theInstance = new EnumerableInstance(children);

            var list = theInstance.Build <IWidget[]>(new StubBuildSession()).ShouldBeOfType <IWidget[]>();

            list.Length.ShouldEqual(3);

            list[0].ShouldBeOfType <ColorWidget>().Color.ShouldEqual("red");
            list[2].ShouldBeOfType <AWidget>();
        }
示例#8
0
        public void build_children_to_a_list()
        {
            var children = new Instance[]
            {
                new SmartInstance <ColorWidget>().WithCtorArg("color").EqualTo("red"),
                new SmartInstance <ColorWidget>().WithCtorArg("color").EqualTo("green"),
                new ObjectInstance(new AWidget())
            };

            var theInstance = new EnumerableInstance(typeof(IList <IWidget>), children);

            var list =
                theInstance.Build(typeof(IList <IWidget>), new StubBuildSession()).ShouldBeOfType <List <IWidget> >();

            list.Count.ShouldEqual(3);

            list[0].ShouldBeOfType <ColorWidget>().Color.ShouldEqual("red");
            list[2].ShouldBeOfType <AWidget>();
        }
示例#9
0
        public static IDependencySource SourceFor(string ctorOrSetter, string name, Type dependencyType, object value)
        {
            if (value == null)
            {
                if (dependencyType.IsSimple())
                {
                    return(new DependencyProblem
                    {
                        Message = MissingPrimitiveWarning,
                        Type = ctorOrSetter,
                        Name = name,
                        ReturnedType = dependencyType
                    });
                }

                if (EnumerableInstance.IsEnumerable(dependencyType))
                {
                    return(new AllPossibleValuesDependencySource(dependencyType));
                }

                return(new DefaultDependencySource(dependencyType));
            }

            if (value is IDependencySource)
            {
                return(value as IDependencySource);
            }

            if (value is Instance)
            {
                return(value.As <Instance>().ToDependencySource(dependencyType));
            }

            if (value.GetType().CanBeCastTo(dependencyType))
            {
                return(new Constant(dependencyType, value));
            }

            if (dependencyType.IsSimple())
            {
                try
                {
                    return(new Constant(dependencyType, ConvertType(value, dependencyType)));
                }
                catch (Exception)
                {
                    return(new DependencyProblem
                    {
                        Type = ctorOrSetter,
                        Name = name,
                        ReturnedType = dependencyType,
                        Message =
                            CastingError.ToFormat(value, value.GetType().GetFullName(), dependencyType.GetFullName())
                    });
                }
            }

            if (EnumerableInstance.IsEnumerable(dependencyType))
            {
                var coercion     = EnumerableInstance.DetermineCoercion(dependencyType);
                var coercedValue = coercion.Convert(value.As <IEnumerable <object> >());

                return(new Constant(dependencyType, coercedValue));
            }


            return(new DependencyProblem
            {
                Type = ctorOrSetter,
                Name = name,
                ReturnedType = dependencyType,
                Message = UnableToDetermineDependency.ToFormat(dependencyType.GetFullName(), value)
            });
        }
示例#10
0
 public AllPossibleValuesDependencySource(Type enumerationType)
 {
     _enumerationType = enumerationType;
     _elementType     = EnumerableInstance.DetermineElementType(enumerationType);
 }
示例#11
0
 public void build_coercion_strategy_for_an_array()
 {
     EnumerableInstance.DetermineCoercion(typeof(IWidget[])).ShouldBeOfType <ArrayCoercion <IWidget> >();
 }
示例#12
0
 public void build_coercion_strategy_for_a_plain_enumerable()
 {
     EnumerableInstance.DetermineCoercion(typeof(IEnumerable <IWidget>)).ShouldBeOfType <ListCoercion <IWidget> >();
 }