|
From: <jer...@us...> - 2007-02-26 03:09:24
|
Revision: 25
http://structuremap.svn.sourceforge.net/structuremap/?rev=25&view=rev
Author: jeremydmiller
Date: 2007-02-25 19:09:23 -0800 (Sun, 25 Feb 2007)
Log Message:
-----------
Adding the "Prototype" option to the Fluent Interface configuration
Modified Paths:
--------------
trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs
Added Paths:
-----------
trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs
Modified: trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs 2007-02-25 19:29:51 UTC (rev 24)
+++ trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs 2007-02-26 03:09:23 UTC (rev 25)
@@ -14,7 +14,7 @@
private string _propertyName;
private MemoryInstanceMemento _memento;
private List<IExpression> _children = new List<IExpression>();
-
+ private PrototypeMemento _prototypeMemento;
public InstanceExpression(Type pluginType)
@@ -26,21 +26,29 @@
void IExpression.Configure(PluginGraph graph)
{
- if (_pluggedType == null && string.IsNullOrEmpty(_memento.ConcreteKey))
+ if (_prototypeMemento == null && _pluggedType == null && string.IsNullOrEmpty(_memento.ConcreteKey))
{
throw new StructureMapException(301, _memento.InstanceKey, TypePath.GetAssemblyQualifiedName(_pluginType));
}
PluginFamily family = graph.LocateOrCreateFamilyForType(_pluginType);
- Plugin plugin = _pluggedType == null
- ? family.Plugins[_memento.ConcreteKey]
- : family.Plugins.FindOrCreate(_pluggedType);
- _memento.ConcreteKey = plugin.ConcreteKey;
+ if (_prototypeMemento == null)
+ {
+ Plugin plugin = _pluggedType == null
+ ? family.Plugins[_memento.ConcreteKey]
+ : family.Plugins.FindOrCreate(_pluggedType);
-
-
- family.Source.AddExternalMemento(_memento);
+ _memento.ConcreteKey = plugin.ConcreteKey;
+ family.Source.AddExternalMemento(_memento);
+ }
+ else
+ {
+ _prototypeMemento.InstanceKey = _memento.InstanceKey;
+ family.Source.AddExternalMemento(_prototypeMemento);
+ }
+
+
}
@@ -84,11 +92,10 @@
public void UsePrototype(ICloneable cloneable)
{
-
+ _prototypeMemento = new PrototypeMemento(_memento.InstanceKey, cloneable);
}
-
public InstanceExpression UsingConcreteTypeNamed(string concreteKey)
{
_memento.ConcreteKey = concreteKey;
Added: trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs (rev 0)
+++ trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs 2007-02-26 03:09:23 UTC (rev 25)
@@ -0,0 +1,57 @@
+using System;
+
+namespace StructureMap.Configuration.DSL
+{
+ public class PrototypeMemento : InstanceMemento
+ {
+ private readonly string _instanceKey;
+ private readonly ICloneable _prototype;
+
+ public PrototypeMemento(string instanceKey, ICloneable prototype)
+ {
+ _instanceKey = instanceKey;
+ _prototype = prototype;
+ }
+
+
+ public override object Build(IInstanceCreator creator)
+ {
+ return _prototype.Clone();
+ }
+
+ protected override string innerConcreteKey
+ {
+ get { return string.Empty; }
+ }
+
+ protected override string innerInstanceKey
+ {
+ get { return _instanceKey; }
+ }
+
+ protected override string getPropertyValue(string Key)
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override InstanceMemento getChild(string Key)
+ {
+ throw new NotImplementedException();
+ }
+
+ public override InstanceMemento[] GetChildrenArray(string Key)
+ {
+ throw new NotImplementedException();
+ }
+
+ public override bool IsReference
+ {
+ get { return false; }
+ }
+
+ public override string ReferenceKey
+ {
+ get { throw new NotImplementedException(); }
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs 2007-02-25 19:29:51 UTC (rev 24)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs 2007-02-26 03:09:23 UTC (rev 25)
@@ -51,19 +51,37 @@
- // Build an instance for IWidget, then setup StructureMap to return cloned instances of the
- // "Prototype" (GoF pattern) whenever someone asks for IWidget named "Jeremy"
- registry.AddInstanceOf<IWidget>().WithName("Jeremy").UsePrototype(new CloneableWidget("Jeremy"));
-
+
// Return the specific instance when an IWidget named "Julia" is requested
registry.AddInstanceOf<IWidget>( new CloneableWidget("Julia") ).WithName("Julia");
*/
manager = registry.BuildInstanceManager();
}
+ [Test]
+ public void UseAPreBuiltObjectForAnInstanceAsAPrototype()
+ {
+ Registry registry = new Registry();
+ // Build an instance for IWidget, then setup StructureMap to return cloned instances of the
+ // "Prototype" (GoF pattern) whenever someone asks for IWidget named "Jeremy"
+ registry.AddInstanceOf<IWidget>().WithName("Jeremy").UsePrototype(new CloneableWidget("Jeremy"));
+ manager = registry.BuildInstanceManager();
+ CloneableWidget widget1 = (CloneableWidget) manager.CreateInstance<IWidget>("Jeremy");
+ CloneableWidget widget2 = (CloneableWidget) manager.CreateInstance<IWidget>("Jeremy");
+ CloneableWidget widget3 = (CloneableWidget) manager.CreateInstance<IWidget>("Jeremy");
+
+ Assert.AreEqual("Jeremy", widget1.Name);
+ Assert.AreEqual("Jeremy", widget2.Name);
+ Assert.AreEqual("Jeremy", widget3.Name);
+ Assert.AreNotSame(widget1, widget2);
+ Assert.AreNotSame(widget1, widget3);
+ Assert.AreNotSame(widget2, widget3);
+ }
+
+
[Test]
public void SpecifyANewInstanceWithADependency()
{
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|