|
From: <jer...@us...> - 2008-05-29 03:12:01
|
Revision: 110
http://structuremap.svn.sourceforge.net/structuremap/?rev=110&view=rev
Author: jeremydmiller
Date: 2008-05-28 20:11:55 -0700 (Wed, 28 May 2008)
Log Message:
-----------
Starting the InstanceFactory.Merge functionality
Modified Paths:
--------------
trunk/Source/StructureMap/InstanceFactory.cs
trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs
trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
Added Paths:
-----------
trunk/Source/StructureMap.Testing/Container/ContainerTester.cs
Removed Paths:
-------------
trunk/Source/StructureMap.Testing/Container/InstanceManagerTester.cs
Modified: trunk/Source/StructureMap/InstanceFactory.cs
===================================================================
--- trunk/Source/StructureMap/InstanceFactory.cs 2008-05-28 15:50:14 UTC (rev 109)
+++ trunk/Source/StructureMap/InstanceFactory.cs 2008-05-29 03:11:55 UTC (rev 110)
@@ -157,6 +157,11 @@
_instanceBuilders.Add(family.Plugins);
foreach (Instance instance in family.GetAllInstances())
{
+ if (_instances.ContainsKey(instance.Name))
+ {
+ continue;
+ }
+
AddInstance(instance);
}
}
Added: trunk/Source/StructureMap.Testing/Container/ContainerTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Container/ContainerTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Container/ContainerTester.cs 2008-05-29 03:11:55 UTC (rev 110)
@@ -0,0 +1,233 @@
+using System;
+using NUnit.Framework;
+using StructureMap.Configuration.DSL;
+using StructureMap.Exceptions;
+using StructureMap.Graph;
+using StructureMap.Interceptors;
+using StructureMap.Pipeline;
+using StructureMap.Source;
+using StructureMap.Testing.GenericWidgets;
+using StructureMap.Testing.Widget;
+using StructureMap.Testing.Widget3;
+
+namespace StructureMap.Testing.Container
+{
+ [TestFixture]
+ public class ContainerTester : Registry
+ {
+ #region Setup/Teardown
+
+ [SetUp]
+ public void SetUp()
+ {
+ _manager = new StructureMap.Container(delegate(Registry registry)
+ {
+ registry.ScanAssemblies().IncludeAssembly("StructureMap.Testing.Widget");
+ registry.BuildInstancesOf<Rule>();
+ registry.BuildInstancesOf<IWidget>();
+ registry.BuildInstancesOf<WidgetMaker>();
+ });
+ }
+
+ #endregion
+
+ private IContainer _manager;
+
+ private void addColorMemento(string Color)
+ {
+ ConfiguredInstance instance = new ConfiguredInstance(Color).WithConcreteKey("Color").SetProperty("Color", Color);
+
+ _manager.AddInstance<Rule>(instance);
+ _manager.AddInstance<IWidget>(instance);
+ _manager.AddInstance<WidgetMaker>(instance);
+ }
+
+ public interface IProvider
+ {
+ }
+
+ public class Provider : IProvider
+ {
+ }
+
+ public class ClassThatUsesProvider
+ {
+ private readonly IProvider _provider;
+
+ public ClassThatUsesProvider(IProvider provider)
+ {
+ _provider = provider;
+ }
+
+
+ public IProvider Provider
+ {
+ get { return _provider; }
+ }
+ }
+
+ public class DifferentProvider : IProvider
+ {
+ }
+
+ [Test]
+ public void CanBuildConcreteTypesThatAreNotPreviouslyRegistered()
+ {
+ IContainer manager = new StructureMap.Container(delegate(Registry registry)
+ {
+ // Create a new Container that has a default instance configured for only the
+ // IProvider interface. Container is the real "container" behind ObjectFactory
+ registry.ForRequestedType<IProvider>().TheDefaultIsConcreteType<Provider>();
+ });
+
+ // Now, have that same Container create a ClassThatUsesProvider. StructureMap will
+ // see that ClassThatUsesProvider is concrete, determine its constructor args, and build one
+ // for you with the default IProvider. No other configuration necessary.
+ ClassThatUsesProvider classThatUsesProvider = manager.GetInstance<ClassThatUsesProvider>();
+ Assert.IsInstanceOfType(typeof (Provider), classThatUsesProvider.Provider);
+ }
+
+ [Test]
+ public void CanBuildConcreteTypesThatAreNotPreviouslyRegisteredWithArgumentsProvided()
+ {
+ IContainer manager = new StructureMap.Container(delegate(Registry registry)
+ {
+ registry.ForRequestedType<IProvider>().TheDefaultIsConcreteType<Provider>();
+ });
+
+ DifferentProvider differentProvider = new DifferentProvider();
+ ExplicitArguments args = new ExplicitArguments();
+ args.Set<IProvider>(differentProvider);
+
+ ClassThatUsesProvider classThatUsesProvider = manager.GetInstance<ClassThatUsesProvider>(args);
+ Assert.AreSame(differentProvider, classThatUsesProvider.Provider);
+ }
+
+
+
+
+ [Test]
+ public void FindAPluginFamilyForAGenericTypeFromPluginTypeName()
+ {
+ Type serviceType = typeof (IService<string>);
+ PluginGraph pluginGraph = PluginGraph.BuildGraphFromAssembly(serviceType.Assembly);
+ PipelineGraph pipelineGraph = new PipelineGraph(pluginGraph);
+
+ Type stringService = typeof (IService<string>);
+
+ IInstanceFactory factory = pipelineGraph.ForType(stringService);
+ Assert.AreEqual(stringService, factory.PluginType);
+ }
+
+ [Test]
+ public void GetDefaultInstance()
+ {
+ addColorMemento("Red");
+ addColorMemento("Orange");
+ addColorMemento("Blue");
+
+ _manager.SetDefault(typeof (Rule), "Blue");
+ ColorRule rule = _manager.GetInstance(typeof (Rule)) as ColorRule;
+
+ Assert.IsNotNull(rule);
+ Assert.AreEqual("Blue", rule.Color);
+ }
+
+ [Test]
+ public void GetInstanceOf3Types()
+ {
+ addColorMemento("Red");
+ addColorMemento("Orange");
+ addColorMemento("Blue");
+
+ ColorRule rule = _manager.GetInstance(typeof (Rule), "Blue") as ColorRule;
+ Assert.IsNotNull(rule);
+ Assert.AreEqual("Blue", rule.Color);
+
+ ColorWidget widget = _manager.GetInstance(typeof (IWidget), "Red") as ColorWidget;
+ Assert.IsNotNull(widget);
+ Assert.AreEqual("Red", widget.Color);
+
+ ColorWidgetMaker maker = _manager.GetInstance(typeof (WidgetMaker), "Orange") as ColorWidgetMaker;
+ Assert.IsNotNull(maker);
+ Assert.AreEqual("Orange", maker.Color);
+ }
+
+ [Test, ExpectedException(typeof (StructureMapException))]
+ public void GetMissingType()
+ {
+ object o = _manager.GetInstance(typeof (string));
+ }
+
+
+ private void assertColorIs(IContainer manager, string color)
+ {
+ ColorService rule = (ColorService) manager.GetInstance<IService>();
+ Assert.AreEqual(color, rule.Color);
+ }
+
+ [Test]
+ public void SetDefaultInstanceByString()
+ {
+ IContainer manager = new StructureMap.Container(delegate(Registry registry)
+ {
+ registry.ForRequestedType<IService>()
+ .AddInstance(Instance<ColorService>().WithName("Red").WithProperty("color").EqualTo("Red"))
+ .AddInstance(Instance<ColorService>().WithName("Blue").WithProperty("color").EqualTo("Blue"))
+ .AddInstance(Instance<ColorService>().WithName("Green").WithProperty("color").EqualTo("Green"));
+ });
+
+ manager.SetDefault(typeof(IService), "Red");
+ assertColorIs(manager, "Red");
+
+ manager.SetDefault(typeof(IService), "Green");
+ assertColorIs(manager, "Green");
+
+ manager.SetDefault(typeof(IService), "Blue");
+ assertColorIs(manager, "Blue");
+ }
+
+ [Test]
+ public void Can_set_profile_name_and_reset_defaults()
+ {
+ IContainer manager = new StructureMap.Container(delegate(Registry registry)
+ {
+ registry.ForRequestedType<IService>()
+ .TheDefaultIs(Instance<ColorService>().WithName("Orange").WithProperty("color").EqualTo("Orange"))
+ .AddInstance(Instance<ColorService>().WithName("Red").WithProperty("color").EqualTo("Red"))
+ .AddInstance(Instance<ColorService>().WithName("Blue").WithProperty("color").EqualTo("Blue"))
+ .AddInstance(Instance<ColorService>().WithName("Green").WithProperty("color").EqualTo("Green"));
+
+ registry.CreateProfile("Red").For<IService>().UseNamedInstance("Red");
+ registry.CreateProfile("Blue").For<IService>().UseNamedInstance("Blue");
+ });
+
+ assertColorIs(manager, "Orange");
+
+ manager.SetDefaultsToProfile("Red");
+ assertColorIs(manager, "Red");
+
+ manager.SetDefaultsToProfile("Blue");
+ assertColorIs(manager, "Blue");
+
+ manager.SetDefaultsToProfile(string.Empty);
+ assertColorIs(manager, "Orange");
+ }
+
+ [Test, ExpectedException(typeof(StructureMapException))]
+ public void TryToGetDefaultInstanceWithNoInstance()
+ {
+ StructureMap.Container manager = new StructureMap.Container(new PluginGraph());
+ manager.GetInstance<IService>();
+ }
+
+ [Test, ExpectedException(typeof(StructureMapConfigurationException))]
+ public void CTOR_throws_StructureMapConfigurationException_if_there_is_an_error()
+ {
+ PluginGraph graph = new PluginGraph();
+ graph.Log.RegisterError(400, new ApplicationException("Bad!"));
+
+ new StructureMap.Container(graph);
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs 2008-05-28 15:50:14 UTC (rev 109)
+++ trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs 2008-05-29 03:11:55 UTC (rev 110)
@@ -94,5 +94,21 @@
Assert.IsNotNull(factory.FindInstance("New2"));
Assert.IsNotNull(factory.FindInstance("New3"));
}
+
+ [Test]
+ public void Merge_from_PluginFamily_will_not_replace_an_existing_instance()
+ {
+ InstanceFactory factory = new InstanceFactory(typeof(IWidget));
+ LiteralInstance instance1 = new LiteralInstance(new AWidget()).WithName("New");
+ factory.AddInstance(instance1);
+
+ PluginFamily family = new PluginFamily(typeof(IWidget));
+ family.AddInstance(new LiteralInstance(new AWidget()).WithName("New"));
+
+ factory.Merge(family);
+
+ Assert.AreSame(instance1, factory.FindInstance("New"));
+
+ }
}
}
\ No newline at end of file
Deleted: trunk/Source/StructureMap.Testing/Container/InstanceManagerTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Container/InstanceManagerTester.cs 2008-05-28 15:50:14 UTC (rev 109)
+++ trunk/Source/StructureMap.Testing/Container/InstanceManagerTester.cs 2008-05-29 03:11:55 UTC (rev 110)
@@ -1,233 +0,0 @@
-using System;
-using NUnit.Framework;
-using StructureMap.Configuration.DSL;
-using StructureMap.Exceptions;
-using StructureMap.Graph;
-using StructureMap.Interceptors;
-using StructureMap.Pipeline;
-using StructureMap.Source;
-using StructureMap.Testing.GenericWidgets;
-using StructureMap.Testing.Widget;
-using StructureMap.Testing.Widget3;
-
-namespace StructureMap.Testing.Container
-{
- [TestFixture]
- public class InstanceManagerTester : Registry
- {
- #region Setup/Teardown
-
- [SetUp]
- public void SetUp()
- {
- _manager = new StructureMap.Container(delegate(Registry registry)
- {
- registry.ScanAssemblies().IncludeAssembly("StructureMap.Testing.Widget");
- registry.BuildInstancesOf<Rule>();
- registry.BuildInstancesOf<IWidget>();
- registry.BuildInstancesOf<WidgetMaker>();
- });
- }
-
- #endregion
-
- private IContainer _manager;
-
- private void addColorMemento(string Color)
- {
- ConfiguredInstance instance = new ConfiguredInstance(Color).WithConcreteKey("Color").SetProperty("Color", Color);
-
- _manager.AddInstance<Rule>(instance);
- _manager.AddInstance<IWidget>(instance);
- _manager.AddInstance<WidgetMaker>(instance);
- }
-
- public interface IProvider
- {
- }
-
- public class Provider : IProvider
- {
- }
-
- public class ClassThatUsesProvider
- {
- private readonly IProvider _provider;
-
- public ClassThatUsesProvider(IProvider provider)
- {
- _provider = provider;
- }
-
-
- public IProvider Provider
- {
- get { return _provider; }
- }
- }
-
- public class DifferentProvider : IProvider
- {
- }
-
- [Test]
- public void CanBuildConcreteTypesThatAreNotPreviouslyRegistered()
- {
- IContainer manager = new StructureMap.Container(delegate(Registry registry)
- {
- // Create a new Container that has a default instance configured for only the
- // IProvider interface. Container is the real "container" behind ObjectFactory
- registry.ForRequestedType<IProvider>().TheDefaultIsConcreteType<Provider>();
- });
-
- // Now, have that same Container create a ClassThatUsesProvider. StructureMap will
- // see that ClassThatUsesProvider is concrete, determine its constructor args, and build one
- // for you with the default IProvider. No other configuration necessary.
- ClassThatUsesProvider classThatUsesProvider = manager.GetInstance<ClassThatUsesProvider>();
- Assert.IsInstanceOfType(typeof (Provider), classThatUsesProvider.Provider);
- }
-
- [Test]
- public void CanBuildConcreteTypesThatAreNotPreviouslyRegisteredWithArgumentsProvided()
- {
- IContainer manager = new StructureMap.Container(delegate(Registry registry)
- {
- registry.ForRequestedType<IProvider>().TheDefaultIsConcreteType<Provider>();
- });
-
- DifferentProvider differentProvider = new DifferentProvider();
- ExplicitArguments args = new ExplicitArguments();
- args.Set<IProvider>(differentProvider);
-
- ClassThatUsesProvider classThatUsesProvider = manager.GetInstance<ClassThatUsesProvider>(args);
- Assert.AreSame(differentProvider, classThatUsesProvider.Provider);
- }
-
-
-
-
- [Test]
- public void FindAPluginFamilyForAGenericTypeFromPluginTypeName()
- {
- Type serviceType = typeof (IService<string>);
- PluginGraph pluginGraph = PluginGraph.BuildGraphFromAssembly(serviceType.Assembly);
- PipelineGraph pipelineGraph = new PipelineGraph(pluginGraph);
-
- Type stringService = typeof (IService<string>);
-
- IInstanceFactory factory = pipelineGraph.ForType(stringService);
- Assert.AreEqual(stringService, factory.PluginType);
- }
-
- [Test]
- public void GetDefaultInstance()
- {
- addColorMemento("Red");
- addColorMemento("Orange");
- addColorMemento("Blue");
-
- _manager.SetDefault(typeof (Rule), "Blue");
- ColorRule rule = _manager.GetInstance(typeof (Rule)) as ColorRule;
-
- Assert.IsNotNull(rule);
- Assert.AreEqual("Blue", rule.Color);
- }
-
- [Test]
- public void GetInstanceOf3Types()
- {
- addColorMemento("Red");
- addColorMemento("Orange");
- addColorMemento("Blue");
-
- ColorRule rule = _manager.GetInstance(typeof (Rule), "Blue") as ColorRule;
- Assert.IsNotNull(rule);
- Assert.AreEqual("Blue", rule.Color);
-
- ColorWidget widget = _manager.GetInstance(typeof (IWidget), "Red") as ColorWidget;
- Assert.IsNotNull(widget);
- Assert.AreEqual("Red", widget.Color);
-
- ColorWidgetMaker maker = _manager.GetInstance(typeof (WidgetMaker), "Orange") as ColorWidgetMaker;
- Assert.IsNotNull(maker);
- Assert.AreEqual("Orange", maker.Color);
- }
-
- [Test, ExpectedException(typeof (StructureMapException))]
- public void GetMissingType()
- {
- object o = _manager.GetInstance(typeof (string));
- }
-
-
- private void assertColorIs(IContainer manager, string color)
- {
- ColorService rule = (ColorService) manager.GetInstance<IService>();
- Assert.AreEqual(color, rule.Color);
- }
-
- [Test]
- public void SetDefaultInstanceByString()
- {
- IContainer manager = new StructureMap.Container(delegate(Registry registry)
- {
- registry.ForRequestedType<IService>()
- .AddInstance(Instance<ColorService>().WithName("Red").WithProperty("color").EqualTo("Red"))
- .AddInstance(Instance<ColorService>().WithName("Blue").WithProperty("color").EqualTo("Blue"))
- .AddInstance(Instance<ColorService>().WithName("Green").WithProperty("color").EqualTo("Green"));
- });
-
- manager.SetDefault(typeof(IService), "Red");
- assertColorIs(manager, "Red");
-
- manager.SetDefault(typeof(IService), "Green");
- assertColorIs(manager, "Green");
-
- manager.SetDefault(typeof(IService), "Blue");
- assertColorIs(manager, "Blue");
- }
-
- [Test]
- public void Can_set_profile_name_and_reset_defaults()
- {
- IContainer manager = new StructureMap.Container(delegate(Registry registry)
- {
- registry.ForRequestedType<IService>()
- .TheDefaultIs(Instance<ColorService>().WithName("Orange").WithProperty("color").EqualTo("Orange"))
- .AddInstance(Instance<ColorService>().WithName("Red").WithProperty("color").EqualTo("Red"))
- .AddInstance(Instance<ColorService>().WithName("Blue").WithProperty("color").EqualTo("Blue"))
- .AddInstance(Instance<ColorService>().WithName("Green").WithProperty("color").EqualTo("Green"));
-
- registry.CreateProfile("Red").For<IService>().UseNamedInstance("Red");
- registry.CreateProfile("Blue").For<IService>().UseNamedInstance("Blue");
- });
-
- assertColorIs(manager, "Orange");
-
- manager.SetDefaultsToProfile("Red");
- assertColorIs(manager, "Red");
-
- manager.SetDefaultsToProfile("Blue");
- assertColorIs(manager, "Blue");
-
- manager.SetDefaultsToProfile(string.Empty);
- assertColorIs(manager, "Orange");
- }
-
- [Test, ExpectedException(typeof(StructureMapException))]
- public void TryToGetDefaultInstanceWithNoInstance()
- {
- StructureMap.Container manager = new StructureMap.Container(new PluginGraph());
- manager.GetInstance<IService>();
- }
-
- [Test, ExpectedException(typeof(StructureMapConfigurationException))]
- public void CTOR_throws_StructureMapConfigurationException_if_there_is_an_error()
- {
- PluginGraph graph = new PluginGraph();
- graph.Log.RegisterError(400, new ApplicationException("Bad!"));
-
- new StructureMap.Container(graph);
- }
- }
-}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
===================================================================
--- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-05-28 15:50:14 UTC (rev 109)
+++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-05-29 03:11:55 UTC (rev 110)
@@ -214,7 +214,7 @@
<Compile Include="Container\InstanceFactoryTester.cs">
<SubType>Code</SubType>
</Compile>
- <Compile Include="Container\InstanceManagerTester.cs">
+ <Compile Include="Container\ContainerTester.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Container\IntegratedTester.cs">
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|