public InterceptorTesting()
        {
            _lastService = null;
            recorder = new ContextRecorder();

            // SAMPLE: interceptors-by-instance
            _container = new Container(r =>
            {
                r.For<ContextRecorder>().Use(recorder);

                r.For<IService>().AddInstances(x =>
                {
                    x.Type<ColorService>()
                        // Registers an activation action on this Instance
                        .OnCreation("last service", s => _lastService = s)
                        .Named("Intercepted")
                        .Ctor<string>("color").Is("Red");

                    x.Type<ColorService>()
                        // Activation using IContext
                        .OnCreation("last touched", (c, s) => c.GetInstance<ContextRecorder>().WasTouched = true)
                        .Named("InterceptedWithContext")
                        .Ctor<string>("color").Is("Red");

                    x.Type<ColorService>()
                        .Named("NotIntercepted")
                        .Ctor<string>("color").Is("Blue");

                    x.Object(new ColorService("Yellow"))
                        .Named("Yellow")
                        .OnCreation("set the last service", s => _lastService = s);

                    x.ConstructedBy(() => new ColorService("Purple")).Named("Purple")
                        // Applies a decorator to this instance. Not sure *why*
                        // you'd want to do it this way
                        .DecorateWith(s => new DecoratorService(s));

                    x.ConstructedBy(() => new ColorService("Purple")).Named("DecoratedWithContext")
                        // Fancier decorator
                        .DecorateWith("decorated with context", (c, s) =>
                        {
                            c.GetInstance<ContextRecorder>().WasTouched = true;
                            return new DecoratorService(s);
                        });

                    x.Type<ColorService>().Named("Decorated").DecorateWith(
                        s => new DecoratorService(s))
                        .Ctor<string>("color").Is("Orange");

                    x.Object(new ColorService("Yellow")).Named("Bad")
                        .OnCreation("throw exception", obj => { throw new ApplicationException("Bad!"); });
                });
                // ENDSAMPLE
            });
        }
        public void specify_setter_policy_by_a_predicate_on_property_type()
        {
            var theService = new ColorService("red");

            var container = new Container(x => {
                x.For<IService>().Use(theService);
                x.For<IGateway>().Use<DefaultGateway>();

                x.Policies.SetAllProperties(policy => { policy.TypeMatches(type => type == typeof (IService)); });
            });

            var target = container.GetInstance<ClassWithNamedProperties>();
            target.Service.ShouldBeTheSameAs(theService);
            target.Gateway.ShouldBeNull();
        }
        public void specify_setter_policy_and_construct_an_object()
        {
            var theService = new ColorService("red");

            var container = new Container(x => {
                x.For<IService>().Use(theService);
                x.For<IGateway>().Use<DefaultGateway>();

                x.Policies.SetAllProperties(policy => { policy.WithAnyTypeFromNamespace("StructureMap.Testing.Widget3"); });
            });

            var target = container.GetInstance<ClassWithNamedProperties>();
            target.Service.ShouldBeTheSameAs(theService);
            target.Gateway.ShouldBeOfType<DefaultGateway>();
        }
        public void specify_setter_policy_and_construct_an_object()
        {
            var theService = new ColorService("red");

            var container = new Container(x => {
                x.For<IService>().Use(theService);
                x.For<IGateway>().Use<DefaultGateway>();

                x.ForConcreteType<ClassWithNamedProperties>().Configure.Setter<int>().Is(5);

                x.Policies.SetAllProperties(policy => { policy.WithAnyTypeFromNamespace("StructureMap.Testing.Widget3"); });
            });

            var description = container.Model.For<ClassWithNamedProperties>().Default.DescribeBuildPlan();
            Debug.WriteLine(description);

            var target = container.GetInstance<ClassWithNamedProperties>();
            target.Service.ShouldBeTheSameAs(theService);
            target.Gateway.ShouldBeOfType<DefaultGateway>();
        }
        public void when_retrieving_with_try_get_instance_with_nongeneric_type_that_does_exist()
        {
            var theService = new ColorService("red");
            var registry = new Registry();
            registry.For<IService>().Use(theService);
            var session = BuildSession.ForPluginGraph(registry.Build());

            session.TryGetInstance(typeof(IService)).ShouldBeTheSameAs(theService);
        }
        public void when_retrieving_by_try_get_named_instance_with_nongeneric_type_that_does_exist()
        {
            var red = new ColorService("red");
            var green = new ColorService("green");

            var registry = new Registry();
            registry.For<IService>().Add(red).Named("red");
            registry.For<IService>().Add(green).Named("green");
            var graph = registry.Build();

            var session = BuildSession.ForPluginGraph(graph);
            session.TryGetInstance(typeof(IService), "red").ShouldBeTheSameAs(red);
        }
        public void when_retrieving_by_try_get_named_instance_that_does_exist()
        {
            var red = new ColorService("red");
            var green = new ColorService("green");

            var graph = new PluginGraph();
            PluginFamily family = graph.Families[typeof (IService)];
            family.AddInstance(new ObjectInstance(red).Named("red"));
            family.AddInstance(new ObjectInstance(green).Named("green"));

            var session = BuildSession.ForPluginGraph(graph);
            session.TryGetInstance<IService>("red").ShouldBeTheSameAs(red);
            session.TryGetInstance<IService>("green").ShouldBeTheSameAs(green);
        }
        public void when_retrieving_by_try_get_instance_for_instance_that_does_exist()
        {
            var theService = new ColorService("red");
            var session = BuildSession.Empty(new ExplicitArguments().Set<IService>(theService));

            session.TryGetInstance<IService>().ShouldBeTheSameAs(theService);
        }
        public void when_retrieving_an_object_by_nongeneric_type_and_name()
        {
            var red = new ColorService("red");
            var green = new ColorService("green");

            var registry = new Registry();
            registry.For<IService>().Add(red).Named("red");
            registry.For<IService>().Add(green).Named("green");
            var graph = registry.Build();

            var session = BuildSession.ForPluginGraph(graph);
            session.GetInstance(typeof(IService), "red").ShouldBeTheSameAs(red);
        }
        public void when_retrieving_an_object_by_name()
        {
            var red = new ColorService("red");
            var green = new ColorService("green");

            var graph = new PluginGraph();
            var family = graph.Families[typeof (IService)];
            family.AddInstance(new ObjectInstance(red).Named("red"));
            family.AddInstance(new ObjectInstance(green).Named("green"));

            var session = BuildSession.ForPluginGraph(graph);
            session.GetInstance<IService>("red").ShouldBeTheSameAs(red);
        }
示例#11
0
        public void set_optional_property_for_a_child_object()
        {
            var theService = new ColorService("red");

            theDependencies.Add<IService>(theService);
            TheTarget.Service.ShouldBeTheSameAs(theService);
        }
示例#12
0
        public void set_optional_property_for_a_child_object()
        {
            var theService = new ColorService("red");
            instance.Setter(x => x.Service).Is(theService);

            TheTarget.Service.ShouldBeTheSameAs(theService);
        }
        public void when_retrieving_by_try_get_instance_for_instance_that_does_exist()
        {
            var session = new BuildSession();
            var theService = new ColorService("red");
            session.RegisterDefault(typeof (IService), theService);

            session.TryGetInstance<IService>().ShouldBeTheSameAs(theService);
        }
示例#14
0
        public void SetUp()
        {
            _lastService = null;
            recorder = new ContextRecorder();

            _container = new Container(r =>
            {
                r.ForRequestedType<ContextRecorder>().TheDefault.IsThis(recorder);

                r.ForRequestedType<IService>().AddInstances(x =>
                {
                    x.OfConcreteType<ColorService>()
                        .OnCreation(s => _lastService = s)
                        .WithName("Intercepted")
                        .WithCtorArg("color").EqualTo("Red");

                    x.OfConcreteType<ColorService>()
                        .OnCreation((c, s) => c.GetInstance<ContextRecorder>().WasTouched = true)
                        .WithName("InterceptedWithContext")
                        .WithCtorArg("color").EqualTo("Red");

                    x.OfConcreteType<ColorService>()
                        .WithName("NotIntercepted")
                        .WithCtorArg("color").EqualTo("Blue");

                    x.Object(new ColorService("Yellow"))
                        .WithName("Yellow")
                        .OnCreation<ColorService>(s => _lastService = s);

                    x.ConstructedBy(() => new ColorService("Purple")).WithName("Purple")
                        .EnrichWith<IService>(s => new DecoratorService(s));

                    x.ConstructedBy(() => new ColorService("Purple")).WithName("DecoratedWithContext")
                        .EnrichWith<IService>((c, s) =>
                        {
                            c.GetInstance<ContextRecorder>().WasTouched = true;
                            return new DecoratorService(s);
                        });

                    x.OfConcreteType<ColorService>().WithName("Decorated").EnrichWith<IService>(
                        s => new DecoratorService(s))
                        .WithCtorArg("color").EqualTo("Orange");

                    x.Object(new ColorService("Yellow")).WithName("Bad")
                        .OnCreation<ColorService>(obj => { throw new ApplicationException("Bad!"); });
                });
            });
        }
        public void SetUp()
        {
            _lastService = null;
            recorder = new ContextRecorder();

            _container = new Container(r =>
            {
                r.For<ContextRecorder>().Use(recorder);

                r.For<IService>().AddInstances(x =>
                {
                    x.Type<ColorService>()
                        .OnCreation("last service", s => _lastService = s)
                        .Named("Intercepted")
                        .Ctor<string>("color").Is("Red");

                    x.Type<ColorService>()
                        .OnCreation("last touched", (c, s) => c.GetInstance<ContextRecorder>().WasTouched = true)
                        .Named("InterceptedWithContext")
                        .Ctor<string>("color").Is("Red");

                    x.Type<ColorService>()
                        .Named("NotIntercepted")
                        .Ctor<string>("color").Is("Blue");

                    x.Object(new ColorService("Yellow"))
                        .Named("Yellow")
                        .OnCreation("set the last service", s => _lastService = s);

                    x.ConstructedBy(() => new ColorService("Purple")).Named("Purple")
                        .DecorateWith(s => new DecoratorService(s));

                    x.ConstructedBy(() => new ColorService("Purple")).Named("DecoratedWithContext")
                        .DecorateWith("decorated with context", (c, s) =>
                        {
                            c.GetInstance<ContextRecorder>().WasTouched = true;
                            return new DecoratorService(s);
                        });

                    x.Type<ColorService>().Named("Decorated").DecorateWith(
                        s => new DecoratorService(s))
                        .Ctor<string>("color").Is("Orange");

                    x.Object(new ColorService("Yellow")).Named("Bad")
                        .OnCreation("throw exception", obj => { throw new ApplicationException("Bad!"); });
                });
            });
        }