Exemplo n.º 1
0
        public Scope(IServiceCollection services, PerfTimer timer = null)
        {
            if (timer == null)
            {
                Bootstrapping = new PerfTimer();

                Bootstrapping.Start("Bootstrapping Container");
            }
            else
            {
                Bootstrapping = timer;
                Bootstrapping.MarkStart("Lamar Scope Creation");
            }



            Root = this;

            Bootstrapping.MarkStart("Build ServiceGraph");
            ServiceGraph = new ServiceGraph(services, this);
            Bootstrapping.MarkFinished("Build ServiceGraph");

            ServiceGraph.Initialize(Bootstrapping);

            if (timer == null)
            {
                Bootstrapping.Stop();
            }
            else
            {
                Bootstrapping.MarkFinished("Lamar Scope Creation");
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public void finds_the_single_default()
        {
            theServices.AddTransient <IWidget, AWidget>();
            var theGraph = new ServiceGraph(theServices, Scope.Empty());

            theGraph.Initialize();

            theGraph.FindDefault(typeof(IWidget))
            .ShouldBeOfType <ConstructorInstance>()
            .ImplementationType.ShouldBe(typeof(AWidget));
        }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
0
        public void finds_all()
        {
            theServices.AddTransient <IWidget, AWidget>();
            theServices.AddSingleton(this);
            theServices.AddTransient <IThing, Thing>();
            theServices.AddTransient <IWidget, MoneyWidget>();

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

            theGraph.Initialize();

            theGraph.FindAll(typeof(IWidget))
            .OfType <ConstructorInstance>()
            .Select(x => x.ImplementationType)
            .ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(MoneyWidget));
        }
Exemplo n.º 8
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();
        }
Exemplo n.º 9
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));
        }
Exemplo n.º 10
0
        public void requires_service_provider_with_dependencies_positive()
        {
            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.CreatePlan(theGraph);

            instance.RequiresServiceProvider.ShouldBeTrue();
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
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));
        }