From: <jer...@us...> - 2007-03-08 11:51:51
|
Revision: 26 http://structuremap.svn.sourceforge.net/structuremap/?rev=26&view=rev Author: jeremydmiller Date: 2007-03-08 03:51:48 -0800 (Thu, 08 Mar 2007) Log Message: ----------- More work on the DSL for configuring instances Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/ChildInstanceExpression.cs trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/DefaultExpression.cs trunk/Source/StructureMap/Configuration/DSL/IExpression.cs trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs trunk/Source/StructureMap/Configuration/DSL/ProfileExpression.cs trunk/Source/StructureMap/Configuration/DSL/PropertyExpression.cs trunk/Source/StructureMap/Configuration/DSL/PrototypeExpression.cs trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/Configuration/DSL/ScanAssembliesExpression.cs trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs trunk/Source/StructureMap/Graph/PluginGraph.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/Configuration/DSL/IMementoBuilder.cs trunk/Source/StructureMap/Configuration/DSL/LiteralExpression.cs trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs trunk/Source/StructureMap/Configuration/DSL/MementoBuilder.cs trunk/Source/StructureMap.Testing/Configuration/DSL/LiteralExpressionTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/ChildInstanceExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ChildInstanceExpression.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/ChildInstanceExpression.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Text; using StructureMap.Graph; namespace StructureMap.Configuration.DSL @@ -10,9 +9,9 @@ private readonly InstanceExpression _instance; private readonly MemoryInstanceMemento _memento; private readonly string _propertyName; - private Plugin _plugin; private Type _childType; private List<IExpression> _children = new List<IExpression>(); + private IMementoBuilder _builder; public ChildInstanceExpression(InstanceExpression instance, MemoryInstanceMemento memento, string propertyName) @@ -23,7 +22,6 @@ } - public InstanceExpression IsNamedInstance(string instanceKey) { MemoryInstanceMemento child = MemoryInstanceMemento.CreateReferencedInstanceMemento(instanceKey); @@ -35,29 +33,34 @@ // TODO -- negative case if the concrete type cannot be an implicit instance public InstanceExpression IsConcreteType<T>() { - _plugin = Plugin.CreateImplicitPlugin(typeof(T)); - MemoryInstanceMemento child = MemoryInstanceMemento.CreateReferencedInstanceMemento(_plugin.ConcreteKey); - _memento.AddChild(_propertyName, child); + InstanceExpression child = new InstanceExpression(_childType); + child.UsingConcreteType<T>(); + _children.Add(child); + _builder = child; + return _instance; } void IExpression.Configure(PluginGraph graph) { - if (_plugin == null || _childType == null) + if (_childType == null) { return; } PluginFamily family = graph.LocateOrCreateFamilyForType(_childType); - family.Plugins.FindOrCreate(_plugin.PluggedType); - } + if (_builder != null) + { + InstanceMemento childMemento = _builder.BuildMemento(family); + _memento.AddChild(_propertyName, childMemento); + } - - IExpression[] IExpression.ChildExpressions - { - get { return _children.ToArray(); } + foreach (IExpression child in _children) + { + child.Configure(graph); + } } internal Type ChildType @@ -68,10 +71,11 @@ public InstanceExpression Is(InstanceExpression child) { _children.Add(child); - MemoryInstanceMemento childMemento = MemoryInstanceMemento.CreateReferencedInstanceMemento(child.InstanceKey); + MemoryInstanceMemento childMemento = + MemoryInstanceMemento.CreateReferencedInstanceMemento(child.InstanceKey); _memento.AddChild(_propertyName, childMemento); return _instance; } } -} +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -12,7 +12,6 @@ { private Type _pluginType; private List<AlterPluginFamilyDelegate> _alterations = new List<AlterPluginFamilyDelegate>(); - private Plugin _lastPlugin; private InstanceScope _scope = InstanceScope.PerRequest; public CreatePluginFamilyExpression(Type pluginType) @@ -20,11 +19,9 @@ _pluginType = pluginType; } - - public void Configure(PluginGraph graph) { - PluginFamily family = PluginFamilyAttribute.CreatePluginFamily(_pluginType); + PluginFamily family = graph.LocateOrCreateFamilyForType(_pluginType); InterceptorChainBuilder builder = new InterceptorChainBuilder(); family.InterceptionChain = builder.Build(_scope); @@ -32,26 +29,18 @@ { alteration(family); } - + graph.PluginFamilies.Add(family); AssemblyGraph assembly = new AssemblyGraph(_pluginType.Assembly); graph.Assemblies.Add(assembly); } - IExpression[] IExpression.ChildExpressions - { - get { return new IExpression[0]; } - } - public CreatePluginFamilyExpression WithDefaultConcreteType<T>() { Plugin plugin = addPlugin<T>(); - _alterations.Add(delegate (PluginFamily family) - { - family.DefaultInstanceKey = plugin.ConcreteKey; - }); + _alterations.Add(delegate(PluginFamily family) { family.DefaultInstanceKey = plugin.ConcreteKey; }); return this; } @@ -60,42 +49,50 @@ { Plugin plugin = Plugin.CreateImplicitPlugin(typeof (T)); - _alterations.Add(delegate (PluginFamily family) - { - family.Plugins.Add(plugin); - }); + _alterations.Add(delegate(PluginFamily family) { family.Plugins.Add(plugin); }); return plugin; } - public CreatePluginFamilyExpression PluginConcreteType<T>() + public CreatePluginFamilyExpression TheDefaultIs(IMementoBuilder builder) { - _lastPlugin = addPlugin<T>(); + _alterations.Add(delegate(PluginFamily family) + { + InstanceMemento memento = builder.BuildMemento(family); + family.Source.AddExternalMemento(memento); + family.DefaultInstanceKey = memento.InstanceKey; + }); return this; } - public CreatePluginFamilyExpression AliasedAs(string concreteKey) + public CreatePluginFamilyExpression TheDefaultIsConcreteType<T>() { - _lastPlugin.ConcreteKey = concreteKey; + _alterations.Add(delegate(PluginFamily family) + { + Plugin plugin = family.Plugins.FindOrCreate(typeof (T)); + family.DefaultInstanceKey = plugin.ConcreteKey; + }); + return this; } - public CreatePluginFamilyExpression AsASingleton() + public CreatePluginFamilyExpression CacheBy(InstanceScope scope) { - _scope = InstanceScope.Singleton; + _alterations.Add(delegate(PluginFamily family) + { + InterceptorChainBuilder builder = new InterceptorChainBuilder(); + family.InterceptionChain = builder.Build(scope); + }); + return this; } - public CreatePluginFamilyExpression CacheInstanceAtScope(InstanceScope scope) + public CreatePluginFamilyExpression AsSingletons() { - _scope = scope; + _alterations.Add( + delegate(PluginFamily family) { family.InterceptionChain.AddInterceptor(new SingletonInterceptor()); }); return this; } - - public void AndTheDefaultIs(InstanceExpression expression) - { - throw new NotImplementedException(); - } } -} +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/DefaultExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/DefaultExpression.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/DefaultExpression.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -17,10 +17,5 @@ { _configure(graph); } - - IExpression[] IExpression.ChildExpressions - { - get { return new IExpression[0]; } - } } -} +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/IExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/IExpression.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/IExpression.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -1,6 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Text; using StructureMap.Graph; namespace StructureMap.Configuration.DSL @@ -8,6 +5,5 @@ public interface IExpression { void Configure(PluginGraph graph); - IExpression[] ChildExpressions { get;} } -} +} \ No newline at end of file Added: trunk/Source/StructureMap/Configuration/DSL/IMementoBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/IMementoBuilder.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DSL/IMementoBuilder.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -0,0 +1,9 @@ +using StructureMap.Graph; + +namespace StructureMap.Configuration.DSL +{ + public interface IMementoBuilder + { + InstanceMemento BuildMemento(PluginFamily family); + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -1,81 +1,52 @@ using System; -using System.Collections.Generic; -using System.Collections.Specialized; -using System.Configuration; -using System.Reflection; using StructureMap.Graph; namespace StructureMap.Configuration.DSL { - public class InstanceExpression : IExpression + public class InstanceExpression : MementoBuilder<InstanceExpression> { - private readonly Type _pluginType; private Type _pluggedType; - private string _propertyName; private MemoryInstanceMemento _memento; - private List<IExpression> _children = new List<IExpression>(); - private PrototypeMemento _prototypeMemento; - - public InstanceExpression(Type pluginType) + public InstanceExpression(Type pluginType) : base(pluginType) { - _pluginType = pluginType; - _memento = new MemoryInstanceMemento(); - _memento.InstanceKey = Guid.NewGuid().ToString(); } - void IExpression.Configure(PluginGraph graph) + protected override void buildMemento() { - if (_prototypeMemento == null && _pluggedType == null && string.IsNullOrEmpty(_memento.ConcreteKey)) - { - throw new StructureMapException(301, _memento.InstanceKey, TypePath.GetAssemblyQualifiedName(_pluginType)); - } - - PluginFamily family = graph.LocateOrCreateFamilyForType(_pluginType); - - if (_prototypeMemento == null) - { - Plugin plugin = _pluggedType == null - ? family.Plugins[_memento.ConcreteKey] - : family.Plugins.FindOrCreate(_pluggedType); - - _memento.ConcreteKey = plugin.ConcreteKey; - family.Source.AddExternalMemento(_memento); - } - else - { - _prototypeMemento.InstanceKey = _memento.InstanceKey; - family.Source.AddExternalMemento(_prototypeMemento); - } - - + _memento = new MemoryInstanceMemento(); } - public IExpression[] ChildExpressions + protected override InstanceMemento memento { - get { return _children.ToArray(); } + get { return _memento; } } - - public InstanceExpression WithName(string instanceKey) + protected override InstanceExpression thisInstance { - _memento.InstanceKey = instanceKey; - return this; + get { return this; } } - - public string InstanceKey + protected override void configureMemento(PluginFamily family) { - get { return _memento.InstanceKey; } - set { _memento.InstanceKey = value; } + Plugin plugin = _pluggedType == null + ? family.Plugins[_memento.ConcreteKey] + : family.Plugins.FindOrCreate(_pluggedType); + + _memento.ConcreteKey = plugin.ConcreteKey; } - internal Type PluginType + protected override void validate() { - get { return _pluginType; } + if (_pluggedType == null && string.IsNullOrEmpty(_memento.ConcreteKey)) + { + throw new StructureMapException(301, _memento.InstanceKey, + TypePath.GetAssemblyQualifiedName(_pluginType)); + } } + public InstanceExpression UsingConcreteType<T>() { _pluggedType = typeof (T); @@ -87,25 +58,17 @@ return new PropertyExpression(this, _memento, propertyName); } - - - - public void UsePrototype(ICloneable cloneable) - { - _prototypeMemento = new PrototypeMemento(_memento.InstanceKey, cloneable); - } - - public InstanceExpression UsingConcreteTypeNamed(string concreteKey) { _memento.ConcreteKey = concreteKey; return this; } - public ChildInstanceExpression Child(string propertyName) + public ChildInstanceExpression Child<T>(string propertyName) { - ChildInstanceExpression child = new ChildInstanceExpression(this,_memento, propertyName); - _children.Add(child); + ChildInstanceExpression child = new ChildInstanceExpression(this, _memento, propertyName); + addChildExpression(child); + child.ChildType = typeof (T); return child; } @@ -114,7 +77,8 @@ { // TODO -- what if the property can't be found string propertyName = findPropertyName<T>(); - ChildInstanceExpression child = Child(propertyName); + ChildInstanceExpression child = new ChildInstanceExpression(this, _memento, propertyName); + addChildExpression(child); child.ChildType = typeof (T); return child; } @@ -124,8 +88,5 @@ Plugin plugin = Plugin.CreateImplicitPlugin(_pluggedType); return plugin.FindFirstConstructorArgumentOfType<T>(); } - - - } -} +} \ No newline at end of file Added: trunk/Source/StructureMap/Configuration/DSL/LiteralExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/LiteralExpression.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DSL/LiteralExpression.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -0,0 +1,40 @@ +using StructureMap.Graph; + +namespace StructureMap.Configuration.DSL +{ + public class LiteralExpression<T> : MementoBuilder<LiteralExpression<T>> + { + private readonly T _target; + private LiteralMemento _memento; + + public LiteralExpression(T target) : base(typeof (T)) + { + _target = target; + } + + + protected override InstanceMemento memento + { + get { return _memento; } + } + + protected override LiteralExpression<T> thisInstance + { + get { return this; } + } + + protected override void configureMemento(PluginFamily family) + { + _memento.Instance = _target; + } + + protected override void validate() + { + } + + protected override void buildMemento() + { + _memento = new LiteralMemento(null); + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -0,0 +1,61 @@ +using System; + +namespace StructureMap.Configuration.DSL +{ + public class LiteralMemento : InstanceMemento + { + private object _instance; + + public LiteralMemento(object instance) + { + _instance = instance; + } + + + public object Instance + { + get { return _instance; } + set { _instance = value; } + } + + protected override string innerConcreteKey + { + get { throw new NotImplementedException(); } + } + + protected override string innerInstanceKey + { + get { throw new NotImplementedException(); } + } + + 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(); } + } + + public override object Build(IInstanceCreator creator) + { + return _instance; + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Configuration/DSL/MementoBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/MementoBuilder.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DSL/MementoBuilder.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using StructureMap.Graph; + +namespace StructureMap.Configuration.DSL +{ + // TODO -- T must be constrained to be MementoBuilder + public abstract class MementoBuilder<T> : IExpression, IMementoBuilder + { + protected readonly Type _pluginType; + protected List<IExpression> _children = new List<IExpression>(); + + public MementoBuilder(Type pluginType) + { + _pluginType = pluginType; + buildMemento(); + memento.InstanceKey = Guid.NewGuid().ToString(); + } + + void IExpression.Configure(PluginGraph graph) + { + validate(); + PluginFamily family = graph.LocateOrCreateFamilyForType((Type) _pluginType); + configureMemento(family); + family.Source.AddExternalMemento(memento); + + foreach (IExpression child in _children) + { + child.Configure(graph); + } + } + + protected abstract InstanceMemento memento { get; } + + protected abstract T thisInstance { get; } + + protected abstract void configureMemento(PluginFamily family); + + protected abstract void validate(); + + public T WithName(string instanceKey) + { + memento.InstanceKey = instanceKey; + return thisInstance; + } + + public string InstanceKey + { + get { return memento.InstanceKey; } + set { memento.InstanceKey = value; } + } + + internal Type PluginType + { + get { return _pluginType; } + } + + protected abstract void buildMemento(); + + InstanceMemento IMementoBuilder.BuildMemento(PluginFamily family) + { + validate(); + configureMemento(family); + return memento; + } + + protected void addChildExpression(IExpression expression) + { + _children.Add(expression); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/ProfileExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ProfileExpression.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/ProfileExpression.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -9,10 +9,5 @@ { throw new NotImplementedException(); } - - public IExpression[] ChildExpressions - { - get { throw new NotImplementedException(); } - } } -} +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/PropertyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/PropertyExpression.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/PropertyExpression.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; using System.Configuration; -using System.Text; namespace StructureMap.Configuration.DSL { @@ -31,4 +28,4 @@ return _instance; } } -} +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/PrototypeExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/PrototypeExpression.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/PrototypeExpression.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -1,25 +1,41 @@ using System; -using System.Collections.Generic; -using System.Text; using StructureMap.Graph; namespace StructureMap.Configuration.DSL { - public class PrototypeExpression : IExpression + public class PrototypeExpression<T> : MementoBuilder<PrototypeExpression<T>> { - public void Configure(PluginGraph graph) + private readonly T _prototype; + private PrototypeMemento _memento; + + public PrototypeExpression(T prototype) : base(typeof (T)) { - throw new NotImplementedException(); + _prototype = prototype; } - public IExpression[] ChildExpressions + protected override InstanceMemento memento { - get { return new IExpression[0]; } + get { return _memento; } } - public void WithName(string instanceKey) + protected override PrototypeExpression<T> thisInstance { - + get { return this; } } + + protected override void configureMemento(PluginFamily family) + { + _memento.Prototype = (ICloneable) _prototype; + } + + protected override void validate() + { + // TODO + } + + protected override void buildMemento() + { + _memento = new PrototypeMemento(string.Empty, (ICloneable) _prototype); + } } -} +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -5,7 +5,7 @@ public class PrototypeMemento : InstanceMemento { private readonly string _instanceKey; - private readonly ICloneable _prototype; + private ICloneable _prototype; public PrototypeMemento(string instanceKey, ICloneable prototype) { @@ -14,6 +14,12 @@ } + public ICloneable Prototype + { + get { return _prototype; } + set { _prototype = value; } + } + public override object Build(IInstanceCreator creator) { return _prototype.Clone(); Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -20,7 +20,6 @@ } - /// <summary> /// Implement this method to /// </summary> @@ -38,18 +37,10 @@ { foreach (IExpression expression in _expressions) { - configureExpression(expression, graph); + expression.Configure(graph); } } - private static void configureExpression(IExpression expression, PluginGraph graph) - { - expression.Configure(graph); - foreach (IExpression childExpression in expression.ChildExpressions) - { - configureExpression(childExpression, graph); - } - } public void Dispose() { @@ -64,9 +55,9 @@ return expression; } - public CreatePluginFamilyExpression BuildInstancesOfType<T>() + public CreatePluginFamilyExpression BuildInstancesOf<T>() { - CreatePluginFamilyExpression expression = new CreatePluginFamilyExpression(typeof(T)); + CreatePluginFamilyExpression expression = new CreatePluginFamilyExpression(typeof (T)); addExpression(expression); return expression; @@ -81,19 +72,30 @@ public InstanceExpression AddInstanceOf<T>() { - InstanceExpression expression = new InstanceExpression(typeof(T)); + InstanceExpression expression = new InstanceExpression(typeof (T)); addExpression(expression); return expression; } public static InstanceExpression Instance<T>() { - return new InstanceExpression(typeof(T)); + return new InstanceExpression(typeof (T)); } - public PrototypeExpression AddInstanceOf<T>(T prototype) + public LiteralExpression<T> AddInstanceOf<T>(T target) { - return new PrototypeExpression(); + LiteralExpression<T> literal = new LiteralExpression<T>(target); + addExpression(literal); + + return literal; } + + public PrototypeExpression<T> AddPrototypeInstanceOf<T>(T prototype) + { + PrototypeExpression<T> expression = new PrototypeExpression<T>(prototype); + addExpression(expression); + + return expression; + } } -} +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/ScanAssembliesExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ScanAssembliesExpression.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Configuration/DSL/ScanAssembliesExpression.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -1,8 +1,6 @@ -using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; -using System.Text; using System.Threading; using StructureMap.Graph; @@ -63,4 +61,4 @@ return this; } } -} +} \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -158,5 +158,14 @@ { return Contains(typeof (T)); } + + public PluginFamily Add(Type pluginType) + { + PluginFamilyAttribute att = PluginFamilyAttribute.GetAttribute(pluginType); + PluginFamily family = att == null ? new PluginFamily(pluginType) : att.BuildPluginFamily(pluginType); + _pluginFamilies.Add(pluginType, family); + + return family; + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/PluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginGraph.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/Graph/PluginGraph.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -152,7 +152,7 @@ { if (!_pluginFamilies.Contains(pluginType)) { - PluginFamily family = _pluginFamilies.Add(pluginType, string.Empty); + PluginFamily family = _pluginFamilies.Add(pluginType); attachImplicitPlugins(family); } } Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap/StructureMap.csproj 2007-03-08 11:51:48 UTC (rev 26) @@ -217,10 +217,15 @@ <Compile Include="Configuration\DSL\CreatePluginFamilyExpression.cs" /> <Compile Include="Configuration\DSL\DefaultExpression.cs" /> <Compile Include="Configuration\DSL\IExpression.cs" /> + <Compile Include="Configuration\DSL\IMementoBuilder.cs" /> <Compile Include="Configuration\DSL\InstanceExpression.cs" /> + <Compile Include="Configuration\DSL\LiteralExpression.cs" /> + <Compile Include="Configuration\DSL\LiteralMemento.cs" /> + <Compile Include="Configuration\DSL\MementoBuilder.cs" /> <Compile Include="Configuration\DSL\ProfileExpression.cs" /> <Compile Include="Configuration\DSL\PropertyExpression.cs" /> <Compile Include="Configuration\DSL\PrototypeExpression.cs" /> + <Compile Include="Configuration\DSL\PrototypeMemento.cs" /> <Compile Include="Configuration\DSL\Registry.cs" /> <Compile Include="Configuration\DSL\ScanAssembliesExpression.cs" /> <Compile Include="Configuration\FamilyParser.cs"> Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -4,7 +4,6 @@ using StructureMap.Configuration.DSL; using StructureMap.Graph; using StructureMap.Testing.Widget; -using IList=System.Collections.IList; namespace StructureMap.Testing.Configuration.DSL { @@ -43,18 +42,6 @@ registry.AddInstanceOf<IWidget>().UsingConcreteType<AWidget>(); - - - - - /* - - - - - // Return the specific instance when an IWidget named "Julia" is requested - registry.AddInstanceOf<IWidget>( new CloneableWidget("Julia") ).WithName("Julia"); - */ manager = registry.BuildInstanceManager(); } @@ -64,7 +51,8 @@ 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")); + CloneableWidget theWidget = new CloneableWidget("Jeremy"); + registry.AddPrototypeInstanceOf<IWidget>(theWidget).WithName("Jeremy"); manager = registry.BuildInstanceManager(); @@ -83,6 +71,26 @@ [Test] + public void UseAPreBuiltObjectWithAName() + { + Registry registry = new Registry(); + // Return the specific instance when an IWidget named "Julia" is requested + CloneableWidget julia = new CloneableWidget("Julia"); + registry.AddInstanceOf<IWidget>(julia).WithName("Julia"); + + manager = registry.BuildInstanceManager(); + + CloneableWidget widget1 = (CloneableWidget)manager.CreateInstance<IWidget>("Julia"); + CloneableWidget widget2 = (CloneableWidget)manager.CreateInstance<IWidget>("Julia"); + CloneableWidget widget3 = (CloneableWidget)manager.CreateInstance<IWidget>("Julia"); + + Assert.AreSame(julia, widget1); + Assert.AreSame(julia, widget2); + Assert.AreSame(julia, widget3); + } + + + [Test] public void SpecifyANewInstanceWithADependency() { Registry registry = new Registry(); @@ -182,7 +190,7 @@ // Specify a new Instance, override a dependency with a named instance registry.AddInstanceOf<Rule>().UsingConcreteType<WidgetRule>().WithName("RuleThatUsesMyInstance") - .Child("widget").IsNamedInstance("Purple"); + .Child<IWidget>("widget").IsNamedInstance("Purple"); manager = registry.BuildInstanceManager(); Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -1,5 +1,4 @@ using NUnit.Framework; -using Rhino.Mocks; using StructureMap.Attributes; using StructureMap.Configuration.DSL; using StructureMap.Graph; @@ -9,33 +8,35 @@ namespace StructureMap.Testing.Configuration.DSL { - [TestFixture, Explicit] + [TestFixture] public class CreatePluginFamilyTester { [SetUp] public void SetUp() { - PluginGraph pluginGraph = new PluginGraph(); - using (Registry registry = new Registry(pluginGraph)) - { - // Define the default instance of IWidget - registry.BuildInstancesOfType<IWidget>().AndTheDefaultIs( - Registry.Instance<IWidget>() - .UsingConcreteType<ColorWidget>() - .WithProperty("Color").EqualTo("Red") - ); - - } } [Test] + public void TheDefaultInstanceIsConcreteType() + { + Registry registry = new Registry(); + + // Needs to blow up if the concrete type can't be used + registry.BuildInstancesOf<Rule>().TheDefaultIsConcreteType<ARule>(); + + InstanceManager manager = registry.BuildInstanceManager(); + + Assert.IsInstanceOfType(typeof(ARule), manager.CreateInstance<Rule>()); + } + + [Test] public void BuildInstancesOfType() { PluginGraph pluginGraph = new PluginGraph(); using (Registry registry = new Registry(pluginGraph)) { - registry.BuildInstancesOfType<IGateway>(); + registry.BuildInstancesOf<IGateway>(); } Assert.IsTrue(pluginGraph.PluginFamilies.Contains<IGateway>()); @@ -47,7 +48,7 @@ PluginGraph pluginGraph = new PluginGraph(); using (Registry registry = new Registry(pluginGraph)) { - registry.BuildInstancesOfType<IGateway>(); + registry.BuildInstancesOf<IGateway>(); } Assert.IsTrue(pluginGraph.PluginFamilies.Contains<IGateway>()); @@ -65,7 +66,7 @@ using (Registry registry = new Registry(pluginGraph)) { // Specify the default implementation for an interface - registry.BuildInstancesOfType<IGateway>().WithDefaultConcreteType<StubbedGateway>(); + registry.BuildInstancesOf<IGateway>().WithDefaultConcreteType<StubbedGateway>(); } Assert.IsTrue(pluginGraph.PluginFamilies.Contains<IGateway>()); @@ -77,13 +78,28 @@ } [Test] + public void CreatePluginFamilyWithADefault() + { + Registry registry = new Registry(); + registry.BuildInstancesOf<IWidget>().TheDefaultIs( + Registry.Instance<IWidget>().UsingConcreteType<ColorWidget>().WithProperty("Color").EqualTo("Red") + ); + + InstanceManager manager = registry.BuildInstanceManager(); + + ColorWidget widget = (ColorWidget) manager.CreateInstance<IWidget>(); + Assert.AreEqual("Red", widget.Color); + } + + + [Test] public void BuildPluginFamilyAsPerRequest() { PluginGraph pluginGraph = new PluginGraph(); using (Registry registry = new Registry(pluginGraph)) { CreatePluginFamilyExpression expression = - registry.BuildInstancesOfType<IGateway>(); + registry.BuildInstancesOf<IGateway>(); Assert.IsNotNull(expression); } @@ -97,8 +113,7 @@ PluginGraph pluginGraph = new PluginGraph(); using (Registry registry = new Registry(pluginGraph)) { - CreatePluginFamilyExpression expression = - registry.BuildInstancesOfType<IGateway>().CacheInstanceAtScope(InstanceScope.ThreadLocal); + CreatePluginFamilyExpression expression = registry.BuildInstancesOf<IGateway>().CacheBy(InstanceScope.ThreadLocal); Assert.IsNotNull(expression); } @@ -113,7 +128,7 @@ using (Registry registry = new Registry(pluginGraph)) { CreatePluginFamilyExpression expression = - registry.BuildInstancesOfType<IGateway>().AsASingleton(); + registry.BuildInstancesOf<IGateway>().AsSingletons(); Assert.IsNotNull(expression); } @@ -127,7 +142,7 @@ PluginGraph pluginGraph = new PluginGraph(); using (Registry registry = new Registry(pluginGraph)) { - registry.BuildInstancesOfType<IGateway>().WithDefaultConcreteType<FakeGateway>(); + registry.BuildInstancesOf<IGateway>().WithDefaultConcreteType<FakeGateway>(); } Assert.IsTrue(pluginGraph.PluginFamilies.Contains<IGateway>()); @@ -138,24 +153,5 @@ Assert.IsInstanceOfType(typeof(FakeGateway), gateway); } - [Test] - public void CanAddAdditionalConcreteTypes() - { - Registry registry = new Registry(); - - registry.BuildInstancesOfType<IGateway>() - .PluginConcreteType<FakeGateway>().AliasedAs("Fake") - .PluginConcreteType<Fake2Gateway>() - .PluginConcreteType<StubbedGateway>() - .PluginConcreteType<Fake3Gateway>().AliasedAs("Fake3"); - - InstanceManager manager = registry.BuildInstanceManager(); - IGateway gateway = (IGateway)manager.CreateInstance(typeof(IGateway), "Fake"); - - Assert.IsInstanceOfType(typeof(FakeGateway), gateway); - Assert.IsInstanceOfType(typeof(Fake3Gateway), manager.CreateInstance(typeof(IGateway), "Fake3")); - Assert.IsInstanceOfType(typeof(StubbedGateway), manager.CreateInstance(typeof(IGateway), "Stubbed")); - Assert.IsInstanceOfType(typeof(Fake2Gateway), manager.CreateInstance(typeof(IGateway), TypePath.GetAssemblyQualifiedName(typeof(Fake2Gateway)))); - } } } Added: trunk/Source/StructureMap.Testing/Configuration/DSL/LiteralExpressionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/LiteralExpressionTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/LiteralExpressionTester.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -0,0 +1,63 @@ +using NUnit.Framework; +using Rhino.Mocks; +using StructureMap.Configuration.DSL; +using StructureMap.Graph; +using StructureMap.Testing.Widget; + +namespace StructureMap.Testing.Configuration.DSL +{ + [TestFixture] + public class LiteralExpressionTester + { + [SetUp] + public void SetUp() + { + } + + [Test] + public void ConfiguresALiteral() + { + ColorWidget theWidget = new ColorWidget("Red"); + LiteralExpression<IWidget> expression = new LiteralExpression<IWidget>(theWidget); + PluginGraph graph = new PluginGraph(); + ((IExpression)expression).Configure(graph); + + PluginFamily family = graph.PluginFamilies[typeof (IWidget)]; + Assert.IsNotNull(family); + + LiteralMemento memento = (LiteralMemento) family.Source.GetMemento(expression.InstanceKey); + Assert.AreSame(theWidget, memento.Build(null)); + + } + + [Test] + public void BuildFromInstanceManager() + { + ColorWidget theWidget = new ColorWidget("Red"); + LiteralExpression<IWidget> expression = new LiteralExpression<IWidget>(theWidget); + PluginGraph graph = new PluginGraph(); + ((IExpression)expression).Configure(graph); + + InstanceManager manager = new InstanceManager(graph); + + IWidget actualWidget = manager.CreateInstance<IWidget>(expression.InstanceKey); + Assert.AreSame(theWidget, actualWidget); + } + + [Test] + public void OverrideTheInstanceKey() + { + ColorWidget theWidget = new ColorWidget("Red"); + LiteralExpression<IWidget> expression = new LiteralExpression<IWidget>(theWidget); + expression.WithName("Blue"); + PluginGraph graph = new PluginGraph(); + ((IExpression)expression).Configure(graph); + + PluginFamily family = graph.PluginFamilies[typeof(IWidget)]; + Assert.IsNotNull(family); + + LiteralMemento memento = (LiteralMemento)family.Source.GetMemento(expression.InstanceKey); + Assert.AreSame(theWidget, memento.Build(null)); + } + } +} Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2007-03-08 11:51:48 UTC (rev 26) @@ -24,10 +24,6 @@ IExpression expression2 = mocks.CreateMock<IExpression>(); IExpression expression3 = mocks.CreateMock<IExpression>(); - Expect.Call(expression1.ChildExpressions).Return(new IExpression[0]); - Expect.Call(expression2.ChildExpressions).Return(new IExpression[0]); - Expect.Call(expression3.ChildExpressions).Return(new IExpression[0]); - PluginGraph graph = new PluginGraph(); expression1.Configure(graph); expression2.Configure(graph); @@ -53,10 +49,6 @@ IExpression expression2 = mocks.CreateMock<IExpression>(); IExpression expression3 = mocks.CreateMock<IExpression>(); - Expect.Call(expression1.ChildExpressions).Return(new IExpression[0]); - Expect.Call(expression2.ChildExpressions).Return(new IExpression[0]); - Expect.Call(expression3.ChildExpressions).Return(new IExpression[0]); - PluginGraph graph = new PluginGraph(); expression1.Configure(graph); expression2.Configure(graph); Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2007-02-26 03:09:23 UTC (rev 25) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2007-03-08 11:51:48 UTC (rev 26) @@ -188,6 +188,7 @@ <Compile Include="Configuration\DSL\CreatePluginFamilyTester.cs" /> <Compile Include="Configuration\DSL\CreateProfileTester.cs" /> <Compile Include="Configuration\DSL\InstanceExpressionTester.cs" /> + <Compile Include="Configuration\DSL\LiteralExpressionTester.cs" /> <Compile Include="Configuration\DSL\RegistryTester.cs" /> <Compile Include="Configuration\DSL\ScanAssembliesTester.cs" /> <Compile Include="Configuration\FamilyParserTester.cs"> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |