예제 #1
0
        public void add_type_adds_a_plugin_for_type_once_and_only_once()
        {
            var graph = new PluginGraph();

            graph.AddType(typeof (IThingy), typeof (BigThingy));

            PluginFamily family = graph.FindFamily(typeof (IThingy));
            family.InstanceCount.ShouldEqual(1);
            graph.AddType(typeof (IThingy), typeof (BigThingy));

            family.InstanceCount.ShouldEqual(1);
        }
        public void EnumSetter()
        {
            var graph = new PluginGraph();
            PluginFamily family = graph.FindFamily(typeof (IGridColumn));
            family.AddPlugin(typeof (EnumGridColumn));

            family.AddInstance(_source.GetMemento("Enum"));

            var manager = new Container(graph);

            var column = (EnumGridColumn) manager.GetInstance<IGridColumn>("Enum");

            Assert.AreEqual(FontStyleEnum.BodyText, column.FontStyle);
        }
        public void StringSetter()
        {
            var graph = new PluginGraph();
            PluginFamily family = graph.FindFamily(typeof (IGridColumn));
            family.AddPlugin(typeof (StringGridColumn));

            InstanceMemento memento = _source.GetMemento("String");
            family.AddInstance(memento);

            var manager = new Container(graph);
            var column = (StringGridColumn) manager.GetInstance<IGridColumn>("String");

            Assert.AreEqual(memento.GetProperty("Name"), column.Name);
        }
        public void PrimitiveNonStringSetter()
        {
            var graph = new PluginGraph();
            PluginFamily family = graph.FindFamily(typeof (IGridColumn));
            family.AddPlugin(typeof (LongGridColumn));

            InstanceMemento memento = _source.GetMemento("Long");
            long count = long.Parse(memento.GetProperty("Count"));
            family.AddInstance(memento);

            var manager = new Container(graph);

            var column = (LongGridColumn) manager.GetInstance<IGridColumn>("Long");
            Assert.AreEqual(count, column.Count);
        }
예제 #5
0
        public void FindMasterInstances(PluginGraph graph)
        {
            var master = new Dictionary<Type, Instance>();

            foreach (var pair in _instances)
            {
                PluginFamily family = graph.FindFamily(pair.Key);
                Instance masterInstance = ((IDiagnosticInstance) pair.Value)
                    .FindInstanceForProfile(family, _name, graph.Log);

                master.Add(pair.Key, masterInstance);
            }

            _instances = master;
        }
예제 #6
0
        public void FindPluginFamilies()
        {
            var graph = new PluginGraph();
            graph.Scan(x => { x.Assembly("StructureMap.Testing.Widget"); });

            graph.FindFamily(typeof (IWidget)).DefaultInstanceKey = "Blue";
            graph.CreateFamily(typeof (WidgetMaker));

            graph.Seal();

            foreach (PluginFamily family in graph.PluginFamilies)
            {
                Console.WriteLine(family.PluginType.AssemblyQualifiedName);
            }

            Assert.AreEqual(5, graph.FamilyCount);
        }
        public void BuildClassWithEnumeration()
        {
            var graph = new PluginGraph();

            PluginFamily family = graph.FindFamily(typeof (Cow));
            family.AddPlugin(typeof (Cow), "Default");

            var manager = new Container(graph);

            manager.Configure(r => r.InstanceOf<Cow>().Is.OfConcreteType<Cow>()
                                       .WithName("Angus")
                                       .WithProperty("Name").EqualTo("Bessie")
                                       .WithProperty("Breed").EqualTo("Angus")
                                       .WithProperty("Weight").EqualTo("1200"));

            var angus = manager.GetInstance<Cow>("Angus");

            Assert.IsNotNull(angus);
            Assert.AreEqual("Bessie", angus.Name, "Name");
            Assert.AreEqual(BreedEnum.Angus, angus.Breed, "Breed");
            Assert.AreEqual(1200, angus.Weight, "Weight");
        }
        public void ReadChildArrayProperty()
        {
            var graph = new PluginGraph();

            graph.FindFamily(typeof (Rule)).AddPlugin(typeof (ComplexRule));

            MemoryInstanceMemento memento = ComplexRule.GetMemento();
            memento.SetProperty(XmlConstants.PLUGGED_TYPE, typeof (ComplexRule).AssemblyQualifiedName);
            memento.AddChildArray("cars", new InstanceMemento[]
            {
                MemoryInstanceMemento.CreateReferencedInstanceMemento("Ford"),
                MemoryInstanceMemento.CreateReferencedInstanceMemento("Chevy"),
                MemoryInstanceMemento.CreateReferencedInstanceMemento("Dodge"),
            });

            var instance = (IStructuredInstance) memento.ReadInstance(graph, typeof (Rule));
            Instance[] instances = instance.GetChildArray("cars");
            Assert.AreEqual(3, instances.Length);

            assertIsReference(instances[0], "Ford");
            assertIsReference(instances[1], "Chevy");
            assertIsReference(instances[2], "Dodge");
        }
        public void FindRegistriesWithinPluginGraphSeal()
        {
            var graph = new PluginGraph();

            var scanner = new AssemblyScanner();
            scanner.AssemblyContainingType(typeof (RedGreenRegistry));
            scanner.LookForRegistries();
            scanner.ScanForAll(graph);

            graph.Seal();

            var colors = new List<string>();
            PluginFamily family = graph.FindFamily(typeof (IWidget));

            family.Instances.Each(instance => colors.Add(instance.Name));

            Assert.Contains("Red", colors);
            Assert.Contains("Green", colors);
            Assert.Contains("Yellow", colors);
            Assert.Contains("Blue", colors);
            Assert.Contains("Brown", colors);
            Assert.Contains("Black", colors);
        }
예제 #10
0
        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.FindFamily(typeof (IService));
            family.AddInstance(new ObjectInstance(red).WithName("red"));
            family.AddInstance(new ObjectInstance(green).WithName("green"));

            var session = new BuildSession(graph);
            session.TryGetInstance<IService>("red").ShouldBeTheSameAs(red);
            session.TryGetInstance<IService>("green").ShouldBeTheSameAs(green);
        }
예제 #11
0
        public void FindPlugins()
        {
            var graph = new PluginGraph();
            graph.Scan(x =>
            {
                x.Assembly("StructureMap.Testing.Widget");
                x.Assembly("StructureMap.Testing.Widget2");
            });

            graph.FindFamily(typeof (Rule));

            graph.Seal();

            PluginFamily family = graph.FindFamily(typeof (Rule));
            Assert.IsNotNull(family);
            Assert.AreEqual(5, family.PluginCount, "There are 5 Rule classes in the two assemblies");
        }
예제 #12
0
        public void PutsRightNumberOfPluginsIntoAFamily()
        {
            var graph = new PluginGraph();

            graph.Scan(x => { x.Assembly("StructureMap.Testing.Widget"); });

            graph.FindFamily(typeof (IWidget)).DefaultInstanceKey = "Blue";
            graph.Seal();

            PluginFamily family = graph.FindFamily(typeof (IWidget));
            Assert.IsNotNull(family);

            Assert.AreEqual("Blue", family.DefaultInstanceKey);

            Assert.AreEqual(4, family.PluginCount, "3 different IWidget classes are marked as Pluggable");
        }
예제 #13
0
        public void PicksUpManuallyAddedPlugin()
        {
            var graph = new PluginGraph();

            graph.Scan(x => { x.Assembly("StructureMap.Testing.Widget"); });

            graph.FindFamily(typeof (IWidget)).DefaultInstanceKey = "Blue";

            PluginFamily family = graph.FindFamily(typeof (IWidget));
            family.AddPlugin(typeof (NotPluggableWidget), "NotPluggable");

            graph.Seal();

            Assert.IsNotNull(family);

            Assert.AreEqual(
                5,
                family.PluginCount,
                "5 different IWidget classes are marked as Pluggable, + the manual add");
        }
        public void Process_to_PluginGraph()
        {
            var graph = new PluginGraph();
            var scanner = new DefaultConventionScanner();

            var registry = new Registry();

            scanner.Process(typeof (Convention), registry);

            registry.ConfigureWithoutScanning(graph);

            Assert.IsFalse(graph.ContainsFamily(typeof (IServer)));
            Assert.IsTrue(graph.ContainsFamily(typeof(IConvention)));

            PluginFamily family = graph.FindFamily(typeof (IConvention));
            family.Seal();
            Assert.AreEqual(1, family.InstanceCount);
        }
 public ConfiguredInstance(InstanceMemento memento, PluginGraph graph, Type pluginType)
     : base(memento.FindPlugin(graph.FindFamily(pluginType)))
 {
     read(memento, graph, pluginType);
 }
        public void SetUp()
        {
            _graph = new PluginGraph();
            PluginFamily family = _graph.FindFamily(typeof (IService));
            family.AddPlugin(typeof (ColorService), "Color");

            _graph.FindFamily(typeof (Rule));
        }
        public void ReadPrimitivePropertiesHappyPath()
        {
            var graph = new PluginGraph();

            graph.FindFamily(typeof (Rule)).AddPlugin(typeof (ComplexRule));

            MemoryInstanceMemento memento = ComplexRule.GetMemento();
            memento.SetProperty(XmlConstants.PLUGGED_TYPE, typeof (ComplexRule).AssemblyQualifiedName);

            var instance = (IConfiguredInstance) memento.ReadInstance(graph, typeof (Rule));

            Assert.AreEqual(memento.GetProperty("String"), instance.GetProperty("String"));
            Assert.AreEqual(memento.GetProperty("Breed"), instance.GetProperty("Breed"));
            Assert.AreEqual(memento.GetProperty("Int"), instance.GetProperty("Int"));
            Assert.AreEqual(memento.GetProperty("Long"), instance.GetProperty("Long"));
            Assert.AreEqual(memento.GetProperty("Byte"), instance.GetProperty("Byte"));
            Assert.AreEqual(memento.GetProperty("Double"), instance.GetProperty("Double"));
            Assert.AreEqual(memento.GetProperty("Bool"), instance.GetProperty("Bool"));
        }
        public void ReadChildProperty_child_property_is_defined_build_child()
        {
            var graph = new PluginGraph();

            graph.FindFamily(typeof (Rule)).AddPlugin(typeof (ComplexRule));

            MemoryInstanceMemento memento = ComplexRule.GetMemento();
            memento.SetProperty(XmlConstants.PLUGGED_TYPE, typeof (ComplexRule).AssemblyQualifiedName);
            MemoryInstanceMemento carMemento = MemoryInstanceMemento.CreateReferencedInstanceMemento("GrandPrix");
            memento.AddChild("car", carMemento);

            var instance = (IStructuredInstance) memento.ReadInstance(graph, typeof (Rule));
            var child = (ReferencedInstance) instance.GetChild("car");

            Assert.AreEqual("GrandPrix", child.ReferenceKey);
        }
예제 #19
0
        public void SetUp()
        {
            var graph = new PluginGraph();

            graph.Scan(x => x.Assembly("StructureMap.Testing.Widget"));

            DataMother.WriteDocument("IntegratedTest.XML");
            MementoSource source1 =
                new XmlFileMementoSource("IntegratedTest.XML", "GrandChildren", "GrandChild");

            MementoSource source2 = new XmlFileMementoSource("IntegratedTest.XML", "Children", "Child");
            MementoSource source3 = new XmlFileMementoSource("IntegratedTest.XML", "Parents", "Parent");

            graph.FindFamily(typeof (GrandChild)).AddMementoSource(source1);
            graph.FindFamily(typeof (Child)).AddMementoSource(source2);
            graph.FindFamily(typeof (Parent)).AddMementoSource(source3);

            container = new Container(graph);
        }