public void LambdaInstance_accepts_Visitor()
        {
            var instance = new LambdaInstance<IGateway>(() => null);
            instance.As<IDependencySource>().AcceptVisitor(theVisitor);

            theVisitor.AssertWasCalled(x => x.Dependency(instance));
        }
        /// <summary>
        /// Adds an additional Instance constructed by a Lambda Expression using IContext
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public LambdaInstance<object> Add(Expression<Func<IContext, object>> func)
        {
            var instance = new LambdaInstance<object>(func);
            Add(instance);

            return instance;
        }
        /// <summary>
        /// Adds an additional Instance constructed by a Func using IContext
        /// </summary>
        /// <param name="description">User friendly description for diagnostic purposes</param>
        /// <param name="func"></param>
        /// <returns></returns>
        public LambdaInstance<object> Add(string description, Func<IContext, object> func)
        {
            var instance = new LambdaInstance<object>(description, func);
            Add(instance);

            return instance;
        }
Ejemplo n.º 4
0
            /// <summary>
            /// For this Profile, use an Instance with this Func
            /// </summary>
            /// <param name="func"></param>
            /// <returns></returns>
            public LambdaInstance <T> Use(Func <IContext, T> func)
            {
                var instance = new LambdaInstance <T>(func);

                Use(instance);

                return(instance);
            }
        /// <summary>
        /// Add an Instance to this type created by a Lambda
        /// </summary>
        public LambdaInstance <T, TPluginType> Add <T>(string description, Func <IContext, T> func) where T : TPluginType
        {
            var instance = new LambdaInstance <T, TPluginType>(description, func);

            AddInstance(instance);

            return(instance);
        }
        /// <summary>
        /// Add an Instance to this type created by a Lambda
        /// </summary>
        public LambdaInstance <T, TPluginType> Add <T>(Expression <Func <IContext, T> > func) where T : TPluginType
        {
            var instance = new LambdaInstance <T, TPluginType>(func);

            AddInstance(instance);

            return(instance);
        }
        /// <summary>
        /// Use a lambda to construct the default instance of the Plugin type
        /// </summary>
        public LambdaInstance <T, TPluginType> Use <T>(Expression <Func <T> > expression) where T : TPluginType
        {
            var instance = new LambdaInstance <T, TPluginType>(expression);

            registerDefault(instance);

            return(instance);
        }
        public LambdaInstance <object> Add(Func <IContext, object> func)
        {
            var instance = new LambdaInstance <object>(func);

            Add(instance);

            return(instance);
        }
        /// <summary>
        /// Use a lambda to construct the default instance of the Plugin type
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public LambdaInstance <TPluginType> Use(Func <TPluginType> func)
        {
            var instance = new LambdaInstance <TPluginType>(func);

            registerDefault(instance);

            return(instance);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Register an Instance constructed by a Func that uses IContex
        /// </summary>
        /// <param name="description">User friendly diagnostic description</param>
        /// <param name="func"></param>
        /// <returns></returns>
        public LambdaInstance <object> Use(string description, Func <IContext, object> func)
        {
            var instance = new LambdaInstance <object>(description, func);

            Use(instance);

            return(instance);
        }
        public void LambdaInstance_accepts_Visitor()
        {
            var instance = new LambdaInstance <IGateway>(() => null);

            instance.As <IDependencySource>().AcceptVisitor(theVisitor);

            theVisitor.AssertWasCalled(x => x.Dependency(instance));
        }
Ejemplo n.º 12
0
        public void LambdaInstance_accepts_Visitor()
        {
            var instance = new LambdaInstance <IGateway>(() => null);

            instance.As <IDependencySource>().AcceptVisitor(theVisitor);

            theVisitor.Received().Dependency(instance);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Register an Instance constructed by a Lambda Expression using IContext
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public LambdaInstance <object> Use(Expression <Func <IContext, object> > func)
        {
            var instance = new LambdaInstance <object>(func);

            Use(instance);

            return(instance);
        }
        /// <summary>
        /// Add an Instance to this type created by a Lambda
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public LambdaInstance <PLUGINTYPE> Add(Func <IContext, PLUGINTYPE> func)
        {
            var instance = new LambdaInstance <PLUGINTYPE>(func);

            Add(instance);

            return(instance);
        }
        /// <summary>
        /// Add an Instance to this type created by a Lambda
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public LambdaInstance <TPluginType> Add(Func <IContext, TPluginType> func)
        {
            var instance = new LambdaInstance <TPluginType>(func);

            Add(instance);

            return(instance);
        }
        /// <summary>
        /// Use a lambda to construct the default instance of the Plugin type
        /// Use this overload if your func is too complicated to be an expression
        /// </summary>
        /// <param name="description">Diagnostic description of the func</param>
        /// <param name="func"></param>
        public LambdaInstance <T, TPluginType> Use <T>(string description, Func <T> func) where T : TPluginType
        {
            var instance = new LambdaInstance <T, TPluginType>(description, func);

            registerDefault(instance);

            return(instance);
        }
Ejemplo n.º 17
0
            public LambdaInstance <IServiceContext, T> Add(Func <IServiceContext, T> func)
            {
                var instance = LambdaInstance.For(func);

                _parent.Add(instance);

                return(instance);
            }
Ejemplo n.º 18
0
        public void Sad_path_inner_function_throws_exception_207_with_key_and_plugin_type()
        {
            var instance = new LambdaInstance<object>("throws", () => { throw new NotImplementedException(); });

            var ex =
                Exception<StructureMapBuildException>.ShouldBeThrownBy(
                    () => { instance.Build<IWidget>(new StubBuildSession()); });

            ex.Title.ShouldContain("'Lambda: throws'");
        }
Ejemplo n.º 19
0
        public void Sad_path_inner_function_throws_exception_207_with_key_and_plugin_type()
        {
            var instance = new LambdaInstance <object>("throws", () => { throw new NotImplementedException(); });

            var ex = Exception <StructureMapBuildException> .ShouldBeThrownBy(() => {
                instance.Build <IWidget>(new StubBuildSession());
            });

            ex.Title.ShouldContain("'Lambda: throws'");
        }
Ejemplo n.º 20
0
            public LambdaInstance <IServiceContext, T> Add(Func <IServiceContext, T> func)
            {
                var instance = LambdaInstance.For(func);

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

                _parent.Add(instance);

                return(instance);
            }
Ejemplo n.º 21
0
        public void configure_and_use_missing_instance_by_generic_registration()
        {
            var instance = new LambdaInstance <ColorRule>(c => new ColorRule(c.RequestedName));

            var container = new Container(x => {
                x.For(typeof(Rule))
                .MissingNamedInstanceIs(instance);
            });

            container.GetInstance <Rule>("red").ShouldBeOfType <ColorRule>().Color.ShouldEqual("red");
            container.GetInstance <Rule>("green").ShouldBeOfType <ColorRule>().Color.ShouldEqual("green");
            container.GetInstance <Rule>("blue").ShouldBeOfType <ColorRule>().Color.ShouldEqual("blue");
        }
Ejemplo n.º 22
0
        public void configure_and_use_missing_instance_by_generic_registration()
        {
            var instance = new LambdaInstance<ColorRule>(c => new ColorRule(c.RequestedName));
            
            var container = new Container(x => {
                x.For(typeof (Rule))
                    .MissingNamedInstanceIs(instance);
            });

            container.GetInstance<Rule>("red").ShouldBeOfType<ColorRule>().Color.ShouldEqual("red");
            container.GetInstance<Rule>("green").ShouldBeOfType<ColorRule>().Color.ShouldEqual("green");
            container.GetInstance<Rule>("blue").ShouldBeOfType<ColorRule>().Color.ShouldEqual("blue");
        }
Ejemplo n.º 23
0
        public void Sad_path_inner_function_throws_exception_207_with_key_and_plugin_type()
        {
            var instance = new LambdaInstance <object>(() => { throw new NotImplementedException(); });

            try
            {
                instance.Build(typeof(IWidget), new StubBuildSession());
                Assert.Fail("Should have thrown an exception");
            }
            catch (StructureMapException ex)
            {
                Assert.AreEqual(207, ex.ErrorCode);
            }
        }
Ejemplo n.º 24
0
        public void Sad_path_inner_function_throws_exception_207_with_key_and_plugin_type()
        {
            var instance = new LambdaInstance<object>(() => { throw new NotImplementedException(); });

            try
            {
                instance.Build(typeof (IWidget), new StubBuildSession());
                Assert.Fail("Should have thrown an exception");
            }
            catch (StructureMapException ex)
            {
                Assert.AreEqual(207, ex.ErrorCode);
            }
        }
Ejemplo n.º 25
0
        public void Return_the_same_object_everytime_an_object_is_requested()
        {
            var count = 0;

            var session  = BuildSession.Empty();
            var instance = new LambdaInstance <ColorRule>("counting", () => {
                count++;
                return(new ColorRule("Red"));
            });

            var result1 = session.FindObject(typeof(ColorRule), instance);
            var result2 = session.FindObject(typeof(ColorRule), instance);
            var result3 = session.FindObject(typeof(ColorRule), instance);
            var result4 = session.FindObject(typeof(ColorRule), instance);

            Assert.AreEqual(1, count);

            Assert.AreSame(result1, result2);
            Assert.AreSame(result1, result3);
            Assert.AreSame(result1, result4);
        }
        public void Return_the_same_object_everytime_an_object_is_requested()
        {
            int count = 0;

            var session  = new BuildSession(new PluginGraph());
            var instance = new LambdaInstance <ColorRule>(() =>
            {
                count++;
                return(new ColorRule("Red"));
            });

            object result1 = session.CreateInstance(typeof(ColorRule), instance);
            object result2 = session.CreateInstance(typeof(ColorRule), instance);
            object result3 = session.CreateInstance(typeof(ColorRule), instance);
            object result4 = session.CreateInstance(typeof(ColorRule), instance);

            Assert.AreEqual(1, count);

            Assert.AreSame(result1, result2);
            Assert.AreSame(result1, result3);
            Assert.AreSame(result1, result4);
        }
Ejemplo n.º 27
0
        public void Get_a_unique_value_for_each_individual_buildsession()
        {
            var count = 0;

            var session  = BuildSession.Empty();
            var session2 = BuildSession.Empty();
            var instance = new LambdaInstance <ColorRule>("counting", () => {
                count++;
                return(new ColorRule("Red"));
            });

            var result1 = session.FindObject(typeof(ColorRule), instance);
            var result2 = session.FindObject(typeof(ColorRule), instance);
            var result3 = session2.FindObject(typeof(ColorRule), instance);
            var result4 = session2.FindObject(typeof(ColorRule), instance);

            Assert.AreEqual(2, count);

            Assert.AreSame(result1, result2);
            Assert.AreNotSame(result1, result3);
            Assert.AreSame(result3, result4);
        }
        public void Get_a_unique_value_for_each_individual_buildsession()
        {
            int count = 0;

            var session  = new BuildSession(new PluginGraph());
            var session2 = new BuildSession(new PluginGraph());
            var instance = new LambdaInstance <ColorRule>(() =>
            {
                count++;
                return(new ColorRule("Red"));
            });

            object result1 = session.CreateInstance(typeof(ColorRule), instance);
            object result2 = session.CreateInstance(typeof(ColorRule), instance);
            object result3 = session2.CreateInstance(typeof(ColorRule), instance);
            object result4 = session2.CreateInstance(typeof(ColorRule), instance);

            Assert.AreEqual(2, count);

            Assert.AreSame(result1, result2);
            Assert.AreNotSame(result1, result3);
            Assert.AreSame(result3, result4);
        }
Ejemplo n.º 29
0
        public void Get_a_unique_value_for_each_individual_buildsession()
        {
            var count = 0;

            var session  = BuildSession.Empty();
            var session2 = BuildSession.Empty();
            var instance = new LambdaInstance <ColorRule>("counting", () =>
            {
                count++;
                return(new ColorRule("Red"));
            });

            var result1 = session.FindObject(typeof(ColorRule), instance);
            var result2 = session.FindObject(typeof(ColorRule), instance);
            var result3 = session2.FindObject(typeof(ColorRule), instance);
            var result4 = session2.FindObject(typeof(ColorRule), instance);

            count.ShouldBe(2);

            result1.ShouldBeTheSameAs(result2);
            result1.ShouldNotBeTheSameAs(result3);
            result3.ShouldBeTheSameAs(result4);
        }
    public void Configure <T>(Func <T> componentFactory, DependencyLifecycle dependencyLifecycle)
    {
        var pluginType = typeof(T);

        lock (configuredInstances)
        {
            if (configuredInstances.ContainsKey(pluginType))
            {
                return;
            }
        }

        var lifecycle = GetLifecycleFrom(dependencyLifecycle);
        LambdaInstance <T, T> lambdaInstance = null;

        container.Configure(x =>
        {
            lambdaInstance = x.For <T>()
                             .LifecycleIs(lifecycle)
                             .Use("Custom constructor func", componentFactory);

            x.EnableSetterInjectionFor(pluginType);

            foreach (var implementedInterface in GetAllInterfacesImplementedBy(pluginType))
            {
                x.For(implementedInterface).Use(c => c.GetInstance <T>());

                x.EnableSetterInjectionFor(implementedInterface);
            }
        }
                            );

        lock (configuredInstances)
        {
            configuredInstances.Add(pluginType, lambdaInstance);
        }
    }
Ejemplo n.º 31
0
 public void requires_service_provider()
 {
     LambdaInstance.For(s => new Clock())
     .RequiresServiceProvider.ShouldBeTrue();
 }
Ejemplo n.º 32
0
 public void derive_the_default_name()
 {
     LambdaInstance.For(s => new Clock())
     .Name.ShouldBe(nameof(Clock));
 }
Ejemplo n.º 33
0
        public void Return_the_same_object_within_a_session_for_the_default_of_a_plugin_type()
        {
            int count = 0;

            var instance = new LambdaInstance<ColorRule>(() =>
            {
                count++;
                return new ColorRule("Red");
            });
            var registry = new Registry();
            registry.For<ColorRule>().Use(instance);

            PluginGraph graph = registry.Build();
            var session = BuildSession.ForPluginGraph(graph);

            object result1 = session.GetInstance(typeof (ColorRule));
            object result2 = session.GetInstance(typeof (ColorRule));
            object result3 = session.GetInstance(typeof (ColorRule));
            object result4 = session.GetInstance(typeof (ColorRule));

            Assert.AreEqual(1, count);

            Assert.AreSame(result1, result2);
            Assert.AreSame(result1, result3);
            Assert.AreSame(result1, result4);
        }
Ejemplo n.º 34
0
        public void Return_the_same_object_everytime_an_object_is_requested()
        {
            int count = 0;

            var session = BuildSession.Empty();
            var instance = new LambdaInstance<ColorRule>(() =>
            {
                count++;
                return new ColorRule("Red");
            });

            object result1 = session.FindObject(typeof (ColorRule), instance);
            object result2 = session.FindObject(typeof (ColorRule), instance);
            object result3 = session.FindObject(typeof (ColorRule), instance);
            object result4 = session.FindObject(typeof (ColorRule), instance);

            Assert.AreEqual(1, count);

            Assert.AreSame(result1, result2);
            Assert.AreSame(result1, result3);
            Assert.AreSame(result1, result4);
        }
Ejemplo n.º 35
0
        public void Get_a_unique_value_for_each_individual_buildsession()
        {
            int count = 0;

            var session = BuildSession.Empty();
            var session2 = BuildSession.Empty();
            var instance = new LambdaInstance<ColorRule>(() =>
            {
                count++;
                return new ColorRule("Red");
            });

            object result1 = session.FindObject(typeof (ColorRule), instance);
            object result2 = session.FindObject(typeof (ColorRule), instance);
            object result3 = session2.FindObject(typeof (ColorRule), instance);
            object result4 = session2.FindObject(typeof (ColorRule), instance);

            Assert.AreEqual(2, count);

            Assert.AreSame(result1, result2);
            Assert.AreNotSame(result1, result3);
            Assert.AreSame(result3, result4);
        }
Ejemplo n.º 36
0
        public void Return_the_same_object_within_a_session_for_the_default_of_a_plugin_type()
        {
            var count = 0;

            var instance = new LambdaInstance<ColorRule>("counting", () =>
            {
                count++;
                return new ColorRule("Red");
            });
            var registry = new Registry();
            registry.For<ColorRule>().UseInstance(instance);

            var graph = registry.Build();
            var session = BuildSession.ForPluginGraph(graph);

            var result1 = session.GetInstance(typeof (ColorRule));
            var result2 = session.GetInstance(typeof (ColorRule));
            var result3 = session.GetInstance(typeof (ColorRule));
            var result4 = session.GetInstance(typeof (ColorRule));

            count.ShouldBe(1);

            result1.ShouldBeTheSameAs(result2);
            result1.ShouldBeTheSameAs(result3);
            result1.ShouldBeTheSameAs(result4);
        }
Ejemplo n.º 37
0
        public void Return_the_same_object_everytime_an_object_is_requested()
        {
            var count = 0;

            var session = BuildSession.Empty();
            var instance = new LambdaInstance<ColorRule>("counting", () =>
            {
                count++;
                return new ColorRule("Red");
            });

            var result1 = session.FindObject(typeof (ColorRule), instance);
            var result2 = session.FindObject(typeof (ColorRule), instance);
            var result3 = session.FindObject(typeof (ColorRule), instance);
            var result4 = session.FindObject(typeof (ColorRule), instance);

            count.ShouldBe(1);

            result1.ShouldBeTheSameAs(result2);
            result1.ShouldBeTheSameAs(result3);
            result1.ShouldBeTheSameAs(result4);
        }
Ejemplo n.º 38
0
        public void Get_a_unique_value_for_each_individual_buildsession()
        {
            var count = 0;

            var session = BuildSession.Empty();
            var session2 = BuildSession.Empty();
            var instance = new LambdaInstance<ColorRule>("counting", () =>
            {
                count++;
                return new ColorRule("Red");
            });

            var result1 = session.FindObject(typeof (ColorRule), instance);
            var result2 = session.FindObject(typeof (ColorRule), instance);
            var result3 = session2.FindObject(typeof (ColorRule), instance);
            var result4 = session2.FindObject(typeof (ColorRule), instance);

            count.ShouldBe(2);

            result1.ShouldBeTheSameAs(result2);
            result1.ShouldNotBeTheSameAs(result3);
            result3.ShouldBeTheSameAs(result4);
        }
        public void LambdaInstance_accepts_Visitor()
        {
            var instance = new LambdaInstance<IGateway>(() => null);
            instance.As<IDependencySource>().AcceptVisitor(theVisitor);

            theVisitor.Received().Dependency(instance);
        }
Ejemplo n.º 40
0
        public LambdaInstance<object> Use(Func<IContext, object> func)
        {
            var instance = new LambdaInstance<object>(func);
            Use(instance);

            return instance;
        }