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); }
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"); }
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)); }
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"); }
public void name_when_unique() { var instance = ConstructorInstance.For <AWidget>(); instance.IsOnlyOneOfServiceType = true; instance.DefaultArgName().ShouldBe("aWidget"); }
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); }
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)); }
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)); }
public ServiceFamily Build(Type type, ServiceGraph serviceGraph) { if (type != typeof(IFancy)) { return(null); } return(new ServiceFamily(type, new IDecoratorPolicy[0], ConstructorInstance.For <IFancy, Very>())); }
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); }
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"); }
public ConstructorInstance <TConcrete> Use <TConcrete>() where TConcrete : class, T { var instance = ConstructorInstance.For <T, TConcrete>(); instance.Lifetime = _lifetime; _parent.Add(instance); return(instance); }
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"); }
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); }
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(); }
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(); }
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); }
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); }
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(); }
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); }
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(); }
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(); }
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()); }
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(); }
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)); }
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); }
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>(); } }
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(); }
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); }
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)); }