コード例 #1
0
        public void find_dependencies_deep()
        {
            var theServices = new ServiceRegistry();

            theServices.AddTransient <IWidget, AWidget>();
            theServices.AddTransient <Rule, BlueRule>();
            theServices.AddTransient <OtherGuy>();
            theServices.AddTransient <GuyWithWidgetAndRule>();

            var theGraph = new ServiceGraph(theServices, Scope.Empty());

            theGraph.Initialize();

            var instance = ConstructorInstance.For <GuyWithGuys>();

            instance.CreatePlan(theGraph);

            var expected = new[]
            {
                typeof(AWidget),
                typeof(BlueRule),
                typeof(OtherGuy),
                typeof(GuyWithWidgetAndRule)
            };


            instance.Dependencies.OfType <ConstructorInstance>()
            .Select(x => x.ImplementationType)
            .ShouldBe(expected, true);
        }
コード例 #2
0
        public void name_for_closed_generic_type_that_is_only_one()
        {
            var instance = ConstructorInstance.For <Service <IWidget> >();

            instance.IsOnlyOneOfServiceType = true;

            instance.DefaultArgName().ShouldBe("service_of_IWidget");
        }
コード例 #3
0
        public void get_and_set_for_a_date_time()
        {
            ConstructorInstance instance = ConstructorInstance.For <ClassWithDate>();

            instance.SetValue("date", "12/23/2009");

            instance.Get("date", typeof(DateTime), new StubBuildSession()).ShouldEqual(new DateTime(2009, 12, 23));
        }
コード例 #4
0
        public void has_simple_argument()
        {
            var graph = ServiceGraph.For(x => x.AddTransient <IWidget, AWidget>());

            var ctor = ConstructorInstance.For <WithSimples>().DetermineConstructor(graph, out var message);

            message.ShouldContain("* int number is a 'simple' type that cannot be auto-filled");
        }
コード例 #5
0
        public void name_when_unique()
        {
            var instance = ConstructorInstance.For <AWidget>();

            instance.IsOnlyOneOfServiceType = true;

            instance.DefaultArgName().ShouldBe("aWidget");
        }
コード例 #6
0
        public void set_and_get_an_integer()
        {
            ConstructorInstance instance = ConstructorInstance.For <ClassWithInt>();

            instance.SetValue("age", "45");

            instance.Get("age", typeof(int), new StubBuildSession()).ShouldEqual(45);
        }
コード例 #7
0
        public void to_dependency_source()
        {
            var instance = ConstructorInstance.For <StubbedGateway>();
            var source   = instance.ToDependencySource(typeof(IGateway))
                           .ShouldBeOfType <LifecycleDependencySource>();

            source.Instance.ShouldBe(instance);
            source.PluginType.ShouldBe(typeof(IGateway));
        }
コード例 #8
0
ファイル: ServiceFamilyTester.cs プロジェクト: yuzd/lamar
        public void the_last_instance_is_the_default()
        {
            var family = new ServiceFamily(typeof(IWidget), new IDecoratorPolicy[0], new Instance[] {
                ConstructorInstance.For <IWidget, AWidget>(),
                ConstructorInstance.For <IWidget, ColorWidget>(),
            });

            family.Default.As <ConstructorInstance>().ImplementationType.ShouldBe(typeof(ColorWidget));
        }
コード例 #9
0
ファイル: try_get_instance.cs プロジェクト: yuzd/lamar
        public ServiceFamily Build(Type type, ServiceGraph serviceGraph)
        {
            if (type != typeof(IFancy))
            {
                return(null);
            }

            return(new ServiceFamily(type, new IDecoratorPolicy[0], ConstructorInstance.For <IFancy, Very>()));
        }
コード例 #10
0
        public void get_and_set_for_a_guid()
        {
            ConstructorInstance instance = ConstructorInstance.For <ClassWithGuid>();
            Guid guid = Guid.NewGuid();

            instance.SetValue("guid", guid.ToString());

            instance.Get("guid", typeof(Guid), new StubBuildSession()).ShouldEqual(guid);
        }
コード例 #11
0
        public void set_and_get_a_string()
        {
            ConstructorInstance instance = ConstructorInstance.For <ColorWidget>();

            instance.SetValue("color", "Red");


            instance.Get("color", typeof(string), new StubBuildSession()).ShouldEqual("Red");
        }
コード例 #12
0
            public ConstructorInstance <TConcrete> Use <TConcrete>() where TConcrete : class, T
            {
                var instance = ConstructorInstance.For <T, TConcrete>();

                instance.Lifetime = _lifetime;

                _parent.Add(instance);

                return(instance);
            }
コード例 #13
0
        public void has_unknown_dependency()
        {
            var graph = ServiceGraph.For(x => x.AddTransient <IWidget, AWidget>());

            var ctor = ConstructorInstance.For <WithHitsAndMisses>().DetermineConstructor(graph, out var message);

            message.ShouldContain("* int number is a 'simple' type that cannot be auto-filled");
            message.ShouldContain(
                "* Rule is not registered within this container and cannot be auto discovered by any missing family policy");
        }
コード例 #14
0
        public void get_and_set_a_non_primitive_type_that_is_not_enumerable()
        {
            ConstructorInstance instance = ConstructorInstance.For <ClassWithNonPrimitive>();

            var widget = new AWidget();

            instance.SetValue("widget", widget);

            instance.Get("widget", typeof(IWidget), new StubBuildSession()).ShouldEqual(widget);
        }
コード例 #15
0
        public void set_and_get_a_nullable_integer()
        {
            ConstructorInstance instance = ConstructorInstance.For <ClassWithNullableInt>();

            instance.As <IConfiguredInstance>().SetValue("age", "45");

            instance.Get("age", typeof(int?), new StubBuildSession()).ShouldEqual(45);

            instance.As <IConfiguredInstance>().SetValue("age", null);
            instance.Get("age", typeof(int?), new StubBuildSession()).ShouldBeNull();
        }
コード例 #16
0
        public void get_and_set_for_a_nullable_date_time()
        {
            ConstructorInstance instance = ConstructorInstance.For <ClassWithNullableDate>();

            instance.As <IConfiguredInstance>().SetValue("date", "12/23/2009");

            instance.Get("date", typeof(DateTime?), new StubBuildSession()).ShouldEqual(new DateTime(2009, 12, 23));

            instance.As <IConfiguredInstance>().SetValue("date", null);

            instance.Get("date", typeof(DateTime?), new StubBuildSession()).ShouldBeNull();
        }
コード例 #17
0
ファイル: ServiceFamilyTester.cs プロジェクト: yuzd/lamar
        public void stores_all_in_order()
        {
            var allInstances = new Instance[] {
                ConstructorInstance.For <IWidget, AWidget>(),
                ConstructorInstance.For <IWidget, AWidget>(),
                ConstructorInstance.For <IWidget, ColorWidget>(),
                ConstructorInstance.For <IWidget, MoneyWidget>(),
            };
            var family = new ServiceFamily(typeof(IWidget), new IDecoratorPolicy[0], allInstances);

            family.All.ShouldBe(allInstances);
        }
コード例 #18
0
ファイル: ServiceRegistry.cs プロジェクト: JasperFx/lamar
            public ConstructorInstance <TConcrete, T> Use <TConcrete>() where TConcrete : class, T
            {
                var instance = ConstructorInstance.For <T, TConcrete>();

                if (_lifetime != null)
                {
                    instance.Lifetime = _lifetime.Value;
                }

                _parent.Add(instance);

                return(instance);
            }
コード例 #19
0
        public void happy_path_can_find_ctor_no_error_messages()
        {
            var theServices = new ServiceRegistry();
            var theGraph    = new ServiceGraph(theServices, Scope.Empty());

            theGraph.Initialize();

            var instance = ConstructorInstance.For <NoArgGuy>();

            instance.CreatePlan(theGraph);

            instance.ErrorMessages.Any().ShouldBeFalse();
        }
コード例 #20
0
        public void add_error_message_if_no_public_constructors()
        {
            var theServices = new ServiceRegistry();
            var theGraph    = new ServiceGraph(theServices, Scope.Empty());

            theGraph.Initialize();

            var instance = ConstructorInstance.For <GuyWithNoPublicConstructors>();

            instance.CreatePlan(theGraph);

            instance.Constructor.ShouldBeNull();
            instance.ErrorMessages.ShouldContain(ConstructorInstance.NoPublicConstructors);
        }
コード例 #21
0
        public void will_choose_a_no_arg_ctor_if_that_is_all_there_is()
        {
            var theServices = new ServiceRegistry();
            var theGraph    = new ServiceGraph(theServices, Scope.Empty());

            theGraph.Initialize();

            var instance = ConstructorInstance.For <NoArgGuy>();

            instance.CreatePlan(theGraph);

            instance.Constructor.ShouldNotBeNull();
            instance.Constructor.GetParameters().Any().ShouldBeFalse();
        }
コード例 #22
0
        public void requires_service_provider_with_dependencies_negative()
        {
            var theServices = new ServiceRegistry();

            theServices.AddSingleton <IWidget, AWidget>();
            theServices.AddTransient <Rule, BlueRule>();

            var theGraph = new ServiceGraph(theServices, Scope.Empty());
            var instance = ConstructorInstance.For <GuyWithWidgetAndRule>();

            instance.CreatePlan(theGraph);

            instance.RequiresServiceProvider.ShouldBeFalse();
        }
コード例 #23
0
ファイル: ServiceFamilyTester.cs プロジェクト: yuzd/lamar
        public void setting_the_is_default_property_on_instance()
        {
            var family = new ServiceFamily(typeof(IWidget), new IDecoratorPolicy[0], new Instance[] {
                ConstructorInstance.For <IWidget, AWidget>(),
                ConstructorInstance.For <IWidget, AWidget>(),
                ConstructorInstance.For <IWidget, AWidget>(),
                ConstructorInstance.For <IWidget, ColorWidget>(),
                ConstructorInstance.For <IWidget, ColorWidget>(),
                ConstructorInstance.For <IWidget, MoneyWidget>(),
            });

            family.Instances["moneyWidget"].IsDefault.ShouldBeTrue();
            family.Instances.Values.Where(x => x.Name != "moneyWidget").Each(x => x.IsDefault.ShouldBeFalse());
        }
コード例 #24
0
        public void add_error_message_if_no_public_ctor_can_be_filled()
        {
            var theServices = new ServiceRegistry();

            var theGraph = new ServiceGraph(theServices, Scope.Empty());

            theGraph.Initialize();

            var instance = ConstructorInstance.For <GuyThatUsesIWidget>();

            instance.CreatePlan(theGraph);

            instance.Constructor.ShouldBeNull();
            instance.ErrorMessages.Any(x => x.Contains(ConstructorInstance.NoPublicConstructorCanBeFilled))
            .ShouldBeTrue();
        }
コード例 #25
0
        public void use_default_constructor_attribute_selection()
        {
            var theServices = new ServiceRegistry();

            theServices.AddSingleton <IWidget, AWidget>();
            theServices.AddTransient <Rule, BlueRule>();

            var theGraph = new ServiceGraph(theServices, Scope.Empty());

            theGraph.Initialize();

            var instance = ConstructorInstance.For <GuyWithMultipleConstructors>();

            instance.DetermineConstructor(theGraph, out string message)
            .GetParameters().Single().ParameterType.ShouldBe(typeof(IWidget));
        }
コード例 #26
0
        public void specify_dependency_by_constructor_parameter()
        {
            var instance = ConstructorInstance
                           .For <GuyWithDatabaseConnection>();

            var parameter = instance.Constructor.GetParameters().Single();

            parameter.Name.ShouldBe("connectionString");

            var connString =
                "I haven't used sql server in years and I don't remember what connection strings look like";

            instance.Dependencies.AddForConstructorParameter(parameter, connString);

            var guy = new Container().GetInstance <GuyWithDatabaseConnection>(instance);

            guy.ConnectionString.ShouldBe(connString);
        }
コード例 #27
0
        public void should_be_an_injected_field(ServiceLifetime lifetime, BuildMode mode, bool isInjected)
        {
            var instance = ConstructorInstance.For <AWidget>(lifetime);

            var variable = instance.CreateVariable(mode, null, false);

            if (isInjected)
            {
                var argType = variable.ShouldBeOfType <InjectedServiceField>()
                              .ArgType;

                argType.ShouldBe(typeof(AWidget));
            }
            else
            {
                variable.ShouldNotBeOfType <InjectedServiceField>();
            }
        }
コード例 #28
0
ファイル: ServiceFamilyTester.cs プロジェクト: yuzd/lamar
        public void make_all_the_names_unique()
        {
            var family = new ServiceFamily(typeof(IWidget), new IDecoratorPolicy[0], new Instance[] {
                ConstructorInstance.For <IWidget, AWidget>(),
                ConstructorInstance.For <IWidget, AWidget>(),
                ConstructorInstance.For <IWidget, AWidget>(),
                ConstructorInstance.For <IWidget, ColorWidget>(),
                ConstructorInstance.For <IWidget, ColorWidget>(),
                ConstructorInstance.For <IWidget, MoneyWidget>(),
            });

            family.Instances.ContainsKey("aWidget1").ShouldBeTrue();
            family.Instances.ContainsKey("aWidget2").ShouldBeTrue();
            family.Instances.ContainsKey("aWidget3").ShouldBeTrue();
            family.Instances.ContainsKey("colorWidget1").ShouldBeTrue();
            family.Instances.ContainsKey("colorWidget2").ShouldBeTrue();
            family.Instances.ContainsKey("moneyWidget").ShouldBeTrue();
        }
コード例 #29
0
        public void choose_base_type_of_resolver(ServiceLifetime lifetime, Type baseType)
        {
            var theServices = new ServiceRegistry();

            theServices.AddSingleton <IWidget, AWidget>();
            theServices.AddTransient <Rule>(x => new BlueRule());

            var theGraph = new ServiceGraph(theServices, Scope.Empty());

            theGraph.Initialize();

            var instance = ConstructorInstance.For <GuyWithWidgetAndRule>();

            instance.Lifetime = lifetime;

            instance.CreatePlan(theGraph);

            instance.ResolverBaseType.ShouldBe(baseType);
        }
コード例 #30
0
        public void select_greediest_constructor_that_can_be_filled()
        {
            var theServices = new ServiceRegistry();

            theServices.AddTransient <IWidget, AWidget>();
            theServices.AddSingleton(this);
            theServices.AddTransient <IWidget, MoneyWidget>();
            theServices.AddTransient <IThing, Thing>();

            var theGraph = new ServiceGraph(theServices, Scope.Empty());

            theGraph.Initialize();

            var instance = ConstructorInstance.For <DeepConstructorGuy>();

            instance.CreatePlan(theGraph);


            instance.Constructor.GetParameters().Select(x => x.ParameterType)
            .ShouldHaveTheSameElementsAs(typeof(IWidget), typeof(IThing));
        }