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.ForRequestedType <ColorRule>().TheDefault.IsThis(instance);

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


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

            Assert.AreEqual(1, count);

            Assert.AreSame(result1, result2);
            Assert.AreSame(result1, result3);
            Assert.AreSame(result1, result4);
        }
 protected override object build(Type pluginType, BuildSession session)
 {
     return(new ClassWithNoArgs
     {
         TheAddress = (Address)session.CreateInstance(typeof(Address))
     });
 }
        public void Throw_200_When_trying_to_build_an_instance_that_cannot_be_found()
        {
            var graph = new PipelineGraph(new PluginGraph());

            assertActionThrowsErrorCode(200, delegate
            {
                var session = new BuildSession(graph, null);
                session.CreateInstance(typeof(IGateway), "Gateway that is not configured");
            });
        }
        protected override object build(Type pluginType, BuildSession session)
        {
            if (EnumerableInstance.IsEnumerable(pluginType))
            {
                var enumerable = new EnumerableInstance(pluginType, null);
                return enumerable.Build(pluginType, session);
            }

            return session.CreateInstance(pluginType);
        }
        public void When_calling_CreateInstance_if_no_default_can_be_found_throw_202()
        {
            var graph = new PipelineGraph(new PluginGraph());

            assertActionThrowsErrorCode(202, delegate
            {
                var session = new BuildSession(graph, null);
                session.CreateInstance(typeof(IGateway));
            });
        }
        protected override object build(Type pluginType, BuildSession session)
        {
            if (EnumerableInstance.IsEnumerable(pluginType))
            {
                var enumerable = new EnumerableInstance(pluginType, null);
                return(enumerable.Build(pluginType, session));
            }

            return(session.CreateInstance(pluginType));
        }
示例#7
0
        public void Throw_exception_When_trying_to_build_an_instance_that_cannot_be_found()
        {
            var graph = PipelineGraph.BuildEmpty();

            var ex = Exception <StructureMapConfigurationException> .ShouldBeThrownBy(() => {
                var session = new BuildSession(graph);
                session.CreateInstance(typeof(IGateway), "Gateway that is not configured");
            });

            ex.Title.ShouldEqual("Could not find an Instance named 'Gateway that is not configured' for PluginType StructureMap.Testing.Widget3.IGateway");
        }
        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);
        }
        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);
        }
        public void CanMakeAClassWithNoConstructorParametersWithoutADefinedMemento()
        {
            var registry = new Registry();
            registry.Scan(x => x.Assembly("StructureMap.Testing.Widget3"));

            registry.BuildInstancesOf<IGateway>();

            PluginGraph graph = registry.Build();
            var pipelineGraph = new PipelineGraph(graph);

            var session = new BuildSession(graph);

            var gateway =
                (DefaultGateway) session.CreateInstance(typeof (IGateway), "Default");

            Assert.IsNotNull(gateway);
        }
        public object Build(BuildSession buildSession, Type pluginType, Instance instance)
        {
            var constructorArgs = _ConcreteType
                .GetConstructors()
                .FirstOrDefault()
                .GetParameters()
                .Select(p => buildSession.CreateInstance(p.ParameterType))
                .ToArray();

            var interceptors = new List<IInterceptor>
                                   {
                                       new NotifyInterceptor()
                                   }
                                   .ToArray();

            return new ProxyGenerator().CreateClassProxy(_ConcreteType, interceptors, constructorArgs);
        }
        public void when_building_an_instance_use_the_register_the_stack_frame()
        {
            var recordingInstance       = new BuildSessionInstance1();
            ConfiguredInstance instance =
                new ConfiguredInstance(typeof(ClassWithRule)).Child("rule").Is(recordingInstance);
            var session = new BuildSession(new PluginGraph());

            session.CreateInstance(typeof(IClassWithRule), instance);

            recordingInstance.Root.ConcreteType.ShouldEqual(typeof(ClassWithRule));
            recordingInstance.Root.RequestedType.ShouldEqual(typeof(IClassWithRule));
            recordingInstance.Root.Name.ShouldEqual(instance.Name);

            recordingInstance.Current.ConcreteType.ShouldEqual(typeof(ColorRule));
            recordingInstance.Current.RequestedType.ShouldEqual(typeof(Rule));
            recordingInstance.Current.Name.ShouldEqual(recordingInstance.Name);
        }
示例#13
0
        public void CanMakeAClassWithNoConstructorParametersWithoutADefinedMemento()
        {
            var registry = new Registry();

            registry.Scan(x => x.Assembly("StructureMap.Testing.Widget3"));

            registry.BuildInstancesOf <IGateway>();

            PluginGraph graph         = registry.Build();
            var         pipelineGraph = new PipelineGraph(graph);

            var session = new BuildSession(graph);

            var gateway =
                (DefaultGateway)session.CreateInstance(typeof(IGateway), "Default");

            Assert.IsNotNull(gateway);
        }
        public void Throw_200_When_trying_to_build_an_instance_that_cannot_be_found()
        {
            var graph = new RootPipelineGraph(new PluginGraph());

            assertActionThrowsErrorCode(200, delegate
            {
                var session = new BuildSession(graph);
                session.CreateInstance(typeof (IGateway), "Gateway that is not configured");
            });
        }
        public void Throw_exception_When_trying_to_build_an_instance_that_cannot_be_found()
        {
            var graph = PipelineGraph.BuildEmpty();

            var ex = Exception<StructureMapConfigurationException>.ShouldBeThrownBy(() =>
            {
                var session = new BuildSession(graph);
                session.CreateInstance(typeof (IGateway), "Gateway that is not configured");
            });

            ex.Title.ShouldBe(
                "Could not find an Instance named 'Gateway that is not configured' for PluginType StructureMap.Testing.Widget3.IGateway");
        }
 protected override object build(Type pluginType, BuildSession session)
 {
     return new ClassWithNoArgs
     {
         TheAddress = (Address) session.CreateInstance(typeof (Address))
     };
 }
        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);
        }
        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);
        }
        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.ForRequestedType<ColorRule>().TheDefault.IsThis(instance);

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

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

            Assert.AreEqual(1, count);

            Assert.AreSame(result1, result2);
            Assert.AreSame(result1, result3);
            Assert.AreSame(result1, result4);
        }
        public void when_building_an_instance_use_the_register_the_stack_frame()
        {
            var recordingInstance = new BuildSessionInstance1();
            ConfiguredInstance instance =
                new ConfiguredInstance(typeof (ClassWithRule)).Child("rule").Is(recordingInstance);
            var session = new BuildSession(new PluginGraph());

            session.CreateInstance(typeof (IClassWithRule), instance);

            recordingInstance.Root.ConcreteType.ShouldEqual(typeof (ClassWithRule));
            recordingInstance.Root.RequestedType.ShouldEqual(typeof (IClassWithRule));
            recordingInstance.Root.Name.ShouldEqual(instance.Name);

            recordingInstance.Current.ConcreteType.ShouldEqual(typeof (ColorRule));
            recordingInstance.Current.RequestedType.ShouldEqual(typeof (Rule));
            recordingInstance.Current.Name.ShouldEqual(recordingInstance.Name);
        }
示例#21
0
 protected override object build(Type pluginType, BuildSession session)
 {
     return(session.CreateInstance(pluginType, _referenceKey));
 }
        public void When_calling_CreateInstance_if_no_default_can_be_found_throw_202()
        {
            var graph = new PipelineGraph(new PluginGraph());

            assertActionThrowsErrorCode(202, delegate
            {
                var session = new BuildSession(graph, null);
                session.CreateInstance(typeof (IGateway));
            });
        }