From: <jer...@us...> - 2007-12-22 17:45:22
|
Revision: 50 http://structuremap.svn.sourceforge.net/structuremap/?rev=50&view=rev Author: jeremydmiller Date: 2007-12-22 09:45:18 -0800 (Sat, 22 Dec 2007) Log Message: ----------- shortcuts for <AddInstance> to simplify Xml configuration, interceptors and enrichment at both the Instance and PluginType level. The very beginnings of an AutoMocking approach with RhinoMocks Modified Paths: -------------- trunk/AssembliesBuildOrder.xml trunk/AssembliesDependencies.xml trunk/AssembliesMetrics.xml trunk/ComponentDependenciesDiagram.png trunk/Source/CommonAssemblyInfo.cs trunk/Source/StructureMap/Configuration/ConfigurationParser.cs trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs trunk/Source/StructureMap/Configuration/DSL/MementoBuilder.cs trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/Configuration/FamilyParser.cs trunk/Source/StructureMap/Configuration/UserControlMemento.cs trunk/Source/StructureMap/Configuration/XmlConstants.cs trunk/Source/StructureMap/ConstructorMemento.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/IInstanceManager.cs trunk/Source/StructureMap/InstanceFactory.cs trunk/Source/StructureMap/InstanceManager.cs trunk/Source/StructureMap/InstanceMemento.cs trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/StructureMapConfiguration.cs trunk/Source/StructureMap/StructureMapException.resx trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs trunk/Source/StructureMap.Testing/Configuration/DefaultInstanceNodeTester.cs trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing.Widget3/IService.cs trunk/Source/StructureMap.sln trunk/TypesDependencies.xml trunk/TypesMetrics.xml Added Paths: ----------- trunk/Source/StructureMap/Configuration/DSL/ChildArrayExpression.cs trunk/Source/StructureMap/Configuration/DSL/ReferenceMementoBuilder.cs trunk/Source/StructureMap/Delegates.cs trunk/Source/StructureMap.AutoMocking/ trunk/Source/StructureMap.AutoMocking/AutoMockedInstanceManager.cs trunk/Source/StructureMap.AutoMocking/Properties/ trunk/Source/StructureMap.AutoMocking/Properties/AssemblyInfo.cs trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs trunk/Source/StructureMap.AutoMocking/StructureMap.AutoMocking.csproj trunk/Source/StructureMap.Testing/AutoMocking/ trunk/Source/StructureMap.Testing/AutoMocking/RhinoAutoMockerTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptAllInstancesOfPluginTypeTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptorTesting.cs trunk/Source/StructureMap.Testing/Configuration/DSL/ReferenceMementoBuilderTester.cs trunk/Source/StructureMap.Testing/Configuration/ShortcuttedInstanceNodeTester.cs trunk/Source/StructureMap.Testing/InstanceMementoTester.cs trunk/Source/StructureMap.Testing/TestData/ShortInstance.xml Modified: trunk/AssembliesBuildOrder.xml =================================================================== --- trunk/AssembliesBuildOrder.xml 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/AssembliesBuildOrder.xml 2007-12-22 17:45:18 UTC (rev 50) @@ -1,6 +1,2 @@ <?xml version="1.0" encoding="utf-8" standalone="yes"?> -<AssemblySortForCompilOrObfusk> - <Assembly Name="StructureMap">1</Assembly> - <Assembly Name="StructureMap.DeploymentTasks">2</Assembly> - <Assembly Name="StructureMap.Client">3</Assembly> -</AssemblySortForCompilOrObfusk> \ No newline at end of file +<AssemblySortForCompilOrObfusk /> \ No newline at end of file Modified: trunk/AssembliesDependencies.xml =================================================================== --- trunk/AssembliesDependencies.xml 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/AssembliesDependencies.xml 2007-12-22 17:45:18 UTC (rev 50) @@ -1,24 +1,2 @@ <?xml version="1.0" encoding="utf-8" standalone="yes"?> -<AssemblyDependencies> - <Dependencies_For Assembly="StructureMap"> - <DependsOn> - <DependsOn_Name>nmock</DependsOn_Name> - </DependsOn> - <ReferencedBy> - <ReferencedBy_Name>StructureMap.DeploymentTasks</ReferencedBy_Name> - <ReferencedBy_Name>StructureMap.Client</ReferencedBy_Name> - </ReferencedBy> - </Dependencies_For> - <Dependencies_For Assembly="StructureMap.DeploymentTasks"> - <DependsOn> - <DependsOn_Name>nant.core</DependsOn_Name> - <DependsOn_Name>StructureMap</DependsOn_Name> - </DependsOn> - </Dependencies_For> - <Dependencies_For Assembly="StructureMap.Client"> - <DependsOn> - <DependsOn_Name>StructureMap</DependsOn_Name> - <DependsOn_Name>AxInterop.SHDocVw</DependsOn_Name> - </DependsOn> - </Dependencies_For> -</AssemblyDependencies> \ No newline at end of file +<AssemblyDependencies /> \ No newline at end of file Modified: trunk/AssembliesMetrics.xml =================================================================== --- trunk/AssembliesMetrics.xml 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/AssembliesMetrics.xml 2007-12-22 17:45:18 UTC (rev 50) @@ -1,6 +1,2 @@ <?xml version="1.0" encoding="utf-8" standalone="yes"?> -<AssembliesMetrics> - <Assembly Assembly="StructureMap" NTypes="147" NAbstractTypes="26" NILInstructionInAsm="18779" AfferentCoupling="60" EfferentCoupling="2" RelationalCohesion="4.69" Instability="0.03" Abstractness="0.18" DistFrMainSeq="0.56" NormDistFrMainSeq="0.79" /> - <Assembly Assembly="StructureMap.DeploymentTasks" NTypes="14" NAbstractTypes="1" NILInstructionInAsm="1661" AfferentCoupling="0" EfferentCoupling="18" RelationalCohesion="1" Instability="1" Abstractness="0.07" DistFrMainSeq="0.05" NormDistFrMainSeq="0.07" /> - <Assembly Assembly="StructureMap.Client" NTypes="30" NAbstractTypes="6" NILInstructionInAsm="2997" AfferentCoupling="0" EfferentCoupling="38" RelationalCohesion="1.93" Instability="1" Abstractness="0.2" DistFrMainSeq="0.14" NormDistFrMainSeq="0.2" /> -</AssembliesMetrics> \ No newline at end of file +<AssembliesMetrics /> \ No newline at end of file Modified: trunk/ComponentDependenciesDiagram.png =================================================================== (Binary files differ) Modified: trunk/Source/CommonAssemblyInfo.cs =================================================================== --- trunk/Source/CommonAssemblyInfo.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/CommonAssemblyInfo.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -5,7 +5,7 @@ //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. -// Runtime Version:2.0.50727.832 +// Runtime Version:2.0.50727.1433 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. Modified: trunk/Source/StructureMap/Configuration/ConfigurationParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ConfigurationParser.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Configuration/ConfigurationParser.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -179,6 +179,13 @@ { familyParser.ParseDefaultElement(element); } + + XmlNodeList instanceNodes = findNodes(XmlConstants.ADD_INSTANCE_NODE); + foreach (XmlElement element in instanceNodes) + { + familyParser.ParseInstanceElement(element); + } + } Added: trunk/Source/StructureMap/Configuration/DSL/ChildArrayExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ChildArrayExpression.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DSL/ChildArrayExpression.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -0,0 +1,50 @@ +using System; +using StructureMap.Graph; + +namespace StructureMap.Configuration.DSL +{ + public class ChildArrayExpression<PLUGINTYPE> : IExpression + { + private readonly InstanceExpression _parent; + private readonly MemoryInstanceMemento _memento; + private readonly string _propertyName; + private IMementoBuilder[] _builders; + private Type _pluginType = typeof (PLUGINTYPE); + + public ChildArrayExpression(InstanceExpression parent, MemoryInstanceMemento memento, string propertyName) + { + _parent = parent; + _memento = memento; + _propertyName = propertyName; + + _pluginType = typeof (PLUGINTYPE).GetElementType(); + } + + void IExpression.Configure(PluginGraph graph) + { + PluginFamily family = graph.LocateOrCreateFamilyForType(_pluginType); + InstanceMemento[] childMementos = new InstanceMemento[_builders.Length]; + for (int i = 0; i < _builders.Length; i++) + { + InstanceMemento memento = processMementoBuilder(_builders[i], family, graph); + childMementos[i] = memento; + } + + _memento.AddChildArray(_propertyName, childMementos); + } + + private InstanceMemento processMementoBuilder(IMementoBuilder builder, PluginFamily family, PluginGraph graph) + { + builder.ValidatePluggability(_pluginType); + builder.Configure(graph); + return builder.BuildMemento(family); + } + + public InstanceExpression Contains(params IMementoBuilder[] builders) + { + _builders = builders; + + return _parent; + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -11,16 +11,16 @@ /// <summary> /// Represents the parameters for creating instances of a given Type /// </summary> - public class CreatePluginFamilyExpression : IExpression + public class CreatePluginFamilyExpression<PLUGINTYPE> : IExpression { private Type _pluginType; private List<AlterPluginFamilyDelegate> _alterations = new List<AlterPluginFamilyDelegate>(); private InstanceScope _scope = InstanceScope.PerRequest; private List<IExpression> _children = new List<IExpression>(); - public CreatePluginFamilyExpression(Type pluginType) + public CreatePluginFamilyExpression() { - _pluginType = pluginType; + _pluginType = typeof(PLUGINTYPE); } void IExpression.Configure(PluginGraph graph) @@ -50,7 +50,7 @@ /// </summary> /// <param name="builder"></param> /// <returns></returns> - public CreatePluginFamilyExpression TheDefaultIs(IMementoBuilder builder) + public CreatePluginFamilyExpression<PLUGINTYPE> TheDefaultIs(IMementoBuilder builder) { builder.ValidatePluggability(_pluginType); @@ -65,7 +65,7 @@ return this; } - public CreatePluginFamilyExpression AddInstance(IMementoBuilder builder) + public CreatePluginFamilyExpression<PLUGINTYPE> AddInstance(IMementoBuilder builder) { builder.ValidatePluggability(_pluginType); @@ -86,7 +86,7 @@ /// </summary> /// <typeparam name="CONCRETETYPE"></typeparam> /// <returns></returns> - public CreatePluginFamilyExpression TheDefaultIsConcreteType<CONCRETETYPE>() + public CreatePluginFamilyExpression<PLUGINTYPE> TheDefaultIsConcreteType<CONCRETETYPE>() where CONCRETETYPE : PLUGINTYPE { ExpressionValidator.ValidatePluggabilityOf(typeof (CONCRETETYPE)).IntoPluginType(_pluginType); @@ -105,7 +105,7 @@ /// </summary> /// <param name="scope"></param> /// <returns></returns> - public CreatePluginFamilyExpression CacheBy(InstanceScope scope) + public CreatePluginFamilyExpression<PLUGINTYPE> CacheBy(InstanceScope scope) { _alterations.Add(delegate(PluginFamily family) { @@ -120,13 +120,32 @@ /// Convenience method to mark a PluginFamily as a Singleton /// </summary> /// <returns></returns> - public CreatePluginFamilyExpression AsSingletons() + public CreatePluginFamilyExpression<PLUGINTYPE> AsSingletons() { _alterations.Add( delegate(PluginFamily family) { family.InterceptionChain.AddInterceptor(new SingletonInterceptor()); }); return this; } - + + public CreatePluginFamilyExpression<PLUGINTYPE> OnCreation(StartupHandler<PLUGINTYPE> handler) + { + _alterations.Add(delegate (PluginFamily family) + { + family.InstanceInterceptor = new StartupInterceptor<PLUGINTYPE>(handler); + }); + + return this; + } + + public CreatePluginFamilyExpression<PLUGINTYPE> EnrichWith(EnrichmentHandler<PLUGINTYPE> handler) + { + _alterations.Add(delegate(PluginFamily family) + { + family.InstanceInterceptor = new EnrichmentInterceptor<PLUGINTYPE>(handler); + }); + + return this; + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -71,42 +71,44 @@ /// in the case of a constructor function that consumes more than one argument /// of type T /// </summary> - /// <typeparam name="T"></typeparam> + /// <typeparam name="CONSTRUCTORARGUMENTTYPE"></typeparam> /// <param name="propertyName"></param> /// <returns></returns> - public ChildInstanceExpression Child<T>(string propertyName) + public ChildInstanceExpression Child<CONSTRUCTORARGUMENTTYPE>(string propertyName) { ChildInstanceExpression child = new ChildInstanceExpression(this, _memento, propertyName); addChildExpression(child); - child.ChildType = typeof (T); + child.ChildType = typeof (CONSTRUCTORARGUMENTTYPE); return child; } /// <summary> - /// Start the definition of a child instance for type T + /// Start the definition of a child instance for type CONSTRUCTORARGUMENTTYPE /// </summary> - /// <typeparam name="T"></typeparam> + /// <typeparam name="CONSTRUCTORARGUMENTTYPE"></typeparam> /// <returns></returns> - public ChildInstanceExpression Child<T>() + public ChildInstanceExpression Child<CONSTRUCTORARGUMENTTYPE>() { - string propertyName = findPropertyName<T>(); + string propertyName = findPropertyName<CONSTRUCTORARGUMENTTYPE>(); - if (string.IsNullOrEmpty(propertyName)) - { - throw new StructureMapException(305, TypePath.GetAssemblyQualifiedName(typeof (T))); - } - ChildInstanceExpression child = new ChildInstanceExpression(this, _memento, propertyName); addChildExpression(child); - child.ChildType = typeof (T); + child.ChildType = typeof (CONSTRUCTORARGUMENTTYPE); return child; } private string findPropertyName<T>() { Plugin plugin = Plugin.CreateImplicitPlugin(_pluggedType); - return plugin.FindFirstConstructorArgumentOfType<T>(); + string propertyName = plugin.FindFirstConstructorArgumentOfType<T>(); + + if (string.IsNullOrEmpty(propertyName)) + { + throw new StructureMapException(305, TypePath.GetAssemblyQualifiedName(typeof(T))); + } + + return propertyName; } public override void ValidatePluggability(Type pluginType) @@ -158,5 +160,33 @@ return _parent; } } + + public ChildArrayExpression<PLUGINTYPE> ChildArray<PLUGINTYPE>() + { + validateTypeIsArray<PLUGINTYPE>(); + + string propertyName = findPropertyName<PLUGINTYPE>(); + return ChildArray<PLUGINTYPE>(propertyName); + } + + public ChildArrayExpression<PLUGINTYPE> ChildArray<PLUGINTYPE>(string propertyName) + { + validateTypeIsArray<PLUGINTYPE>(); + + ChildArrayExpression<PLUGINTYPE> expression = new ChildArrayExpression<PLUGINTYPE>(this, _memento, propertyName); + addChildExpression(expression); + + return expression; + } + + private static void validateTypeIsArray<PLUGINTYPE>() + { + if (!typeof(PLUGINTYPE).IsArray) + { + throw new StructureMapException(307); + } + } + + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -59,7 +59,7 @@ get { throw new NotImplementedException(); } } - public override object Build(IInstanceCreator creator) + protected override object buildInstance(IInstanceCreator creator) { return _instance; } Modified: trunk/Source/StructureMap/Configuration/DSL/MementoBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/MementoBuilder.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Configuration/DSL/MementoBuilder.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -51,6 +51,22 @@ return thisInstance; } + public T OnCreation<TYPE>(StartupHandler<TYPE> handler) + { + StartupInterceptor<TYPE> interceptor = new StartupInterceptor<TYPE>(handler); + memento.Interceptor = interceptor; + + return thisInstance; + } + + public T EnrichWith<TYPE>(EnrichmentHandler<TYPE> handler) + { + EnrichmentInterceptor<TYPE> interceptor = new EnrichmentInterceptor<TYPE>(handler); + memento.Interceptor = interceptor; + + return thisInstance; + } + public string InstanceKey { get { return memento.InstanceKey; } Modified: trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Configuration/DSL/PrototypeMemento.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -20,7 +20,7 @@ set { _prototype = value; } } - public override object Build(IInstanceCreator creator) + protected override object buildInstance(IInstanceCreator creator) { return _prototype.Clone(); } Added: trunk/Source/StructureMap/Configuration/DSL/ReferenceMementoBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ReferenceMementoBuilder.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DSL/ReferenceMementoBuilder.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -0,0 +1,41 @@ +using System; +using StructureMap.Graph; + +namespace StructureMap.Configuration.DSL +{ + public class ReferenceMementoBuilder : IMementoBuilder + { + private InstanceMemento _memento; + + public ReferenceMementoBuilder(string referenceKey) + { + _memento = MemoryInstanceMemento.CreateReferencedInstanceMemento(referenceKey); + } + + InstanceMemento IMementoBuilder.BuildMemento(PluginFamily family) + { + return _memento; + } + + InstanceMemento IMementoBuilder.BuildMemento(PluginGraph graph) + { + return _memento; + } + + void IMementoBuilder.SetInstanceName(string instanceKey) + { + + } + + void IMementoBuilder.ValidatePluggability(Type pluginType) + { + + } + + + void IExpression.Configure(PluginGraph graph) + { + // no-op; + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -54,9 +54,9 @@ /// </summary> /// <typeparam name="PLUGINTYPE"></typeparam> /// <returns></returns> - public CreatePluginFamilyExpression BuildInstancesOf<PLUGINTYPE>() + public CreatePluginFamilyExpression<PLUGINTYPE> BuildInstancesOf<PLUGINTYPE>() { - CreatePluginFamilyExpression expression = new CreatePluginFamilyExpression(typeof (PLUGINTYPE)); + CreatePluginFamilyExpression<PLUGINTYPE> expression = new CreatePluginFamilyExpression<PLUGINTYPE>(); addExpression(expression); return expression; @@ -70,9 +70,9 @@ /// </summary> /// <typeparam name="PLUGINTYPE"></typeparam> /// <returns></returns> - public CreatePluginFamilyExpression ForRequestedType<PLUGINTYPE>() + public CreatePluginFamilyExpression<PLUGINTYPE> ForRequestedType<PLUGINTYPE>() { - CreatePluginFamilyExpression expression = new CreatePluginFamilyExpression(typeof(PLUGINTYPE)); + CreatePluginFamilyExpression<PLUGINTYPE> expression = new CreatePluginFamilyExpression<PLUGINTYPE>(); addExpression(expression); return expression; @@ -218,5 +218,11 @@ { return new ConstructorExpression<PLUGINTYPE>(builder); } + + public static ReferenceMementoBuilder Instance(string referencedKey) + { + return new ReferenceMementoBuilder(referencedKey); + } + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/FamilyParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/FamilyParser.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Configuration/FamilyParser.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -51,6 +51,17 @@ _builder.RegisterMemento(pluginTypePath, memento); } + public void ParseInstanceElement(XmlElement element) + { + TypePath pluginTypePath = TypePath.GetTypePath(element.GetAttribute(XmlConstants.PLUGIN_TYPE)); + InstanceScope scope = findScope(element); + + InstanceMemento memento = _mementoCreator.CreateMemento(element); + + _builder.AddPluginFamily(pluginTypePath, null, new string[0], scope); + _builder.RegisterMemento(pluginTypePath, memento); + } + private InstanceScope findScope(XmlElement familyElement) { InstanceScope returnValue = InstanceScope.PerRequest; @@ -108,5 +119,6 @@ } + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/UserControlMemento.cs =================================================================== --- trunk/Source/StructureMap/Configuration/UserControlMemento.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Configuration/UserControlMemento.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -25,7 +25,7 @@ set { _url = value; } } - public override object Build(IInstanceCreator creator) + protected override object buildInstance(IInstanceCreator creator) { return new Page().LoadControl(_url); } Modified: trunk/Source/StructureMap/Configuration/XmlConstants.cs =================================================================== --- trunk/Source/StructureMap/Configuration/XmlConstants.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Configuration/XmlConstants.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -17,6 +17,7 @@ public const string CONCRETE_KEY_ATTRIBUTE = "ConcreteKey"; public const string INTERCEPTORS_NODE = "Interceptors"; public const string INSTANCE_NODE = "Instance"; + public const string ADD_INSTANCE_NODE = "AddInstance"; public const string INSTANCES_NODE = "Instances"; public const string TYPE_ATTRIBUTE = "Type"; public const string KEY_ATTRIBUTE = "Key"; Modified: trunk/Source/StructureMap/ConstructorMemento.cs =================================================================== --- trunk/Source/StructureMap/ConstructorMemento.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/ConstructorMemento.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -25,7 +25,7 @@ } - public override object Build(IInstanceCreator creator) + protected override object buildInstance(IInstanceCreator creator) { return _builder(); } Added: trunk/Source/StructureMap/Delegates.cs =================================================================== --- trunk/Source/StructureMap/Delegates.cs (rev 0) +++ trunk/Source/StructureMap/Delegates.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -0,0 +1,51 @@ +namespace StructureMap +{ + public delegate T EnrichmentHandler<T>(T target); + public delegate void StartupHandler<T>(T target); + + public interface InstanceInterceptor + { + object Process(object target); + } + + public class NulloInterceptor : InstanceInterceptor + { + public object Process(object target) + { + return target; + } + } + + public class StartupInterceptor<T> : InstanceInterceptor + { + private readonly StartupHandler<T> _handler; + + public StartupInterceptor(StartupHandler<T> handler) + { + _handler = handler; + } + + + public object Process(object target) + { + _handler((T) target); + return target; + } + } + + public class EnrichmentInterceptor<T> : InstanceInterceptor + { + private readonly EnrichmentHandler<T> _handler; + + + public EnrichmentInterceptor(EnrichmentHandler<T> handler) + { + _handler = handler; + } + + public object Process(object target) + { + return _handler((T) target); + } + } +} Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -36,6 +36,7 @@ private string _pluginTypeName; private InterceptionChain _interceptionChain; private PluginCollection _plugins; + private InstanceInterceptor _instanceInterceptor = new NulloInterceptor(); public const string CONCRETE_KEY = "CONCRETE"; @@ -109,6 +110,12 @@ #endregion + public InstanceInterceptor InstanceInterceptor + { + get { return _instanceInterceptor; } + set { _instanceInterceptor = value; } + } + public PluginFamily CreateTemplatedClone(params Type[] templateTypes) { Type templatedType = _pluginType.MakeGenericType(templateTypes); Modified: trunk/Source/StructureMap/IInstanceManager.cs =================================================================== --- trunk/Source/StructureMap/IInstanceManager.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/IInstanceManager.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -13,5 +13,6 @@ void SetDefaultsToProfile(string profile); InstanceDefaultManager DefaultManager { get; } + T CreateInstance<T>(InstanceMemento memento); } } \ No newline at end of file Modified: trunk/Source/StructureMap/InstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/InstanceFactory.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/InstanceFactory.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -1,9 +1,9 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Collections.Specialized; using System.Data; using System.Reflection; +using StructureMap.Configuration.DSL; using StructureMap.Emitting; using StructureMap.Graph; using StructureMap.Source; @@ -18,7 +18,20 @@ private Type _pluginType; private Dictionary<string, InstanceBuilder> _instanceBuilders; private MementoSource _source; + private InstanceInterceptor _interceptor = new NulloInterceptor(); + #region static constructors + public static InstanceFactory CreateFactoryWithDefault(Type pluginType, object defaultInstance) + { + PluginFamily family = new PluginFamily(pluginType); + InstanceFactory factory = new InstanceFactory(family, true); + factory.SetDefault(new LiteralMemento(defaultInstance)); + + return factory; + } + #endregion + + #region constructor functions private InstanceFactory() @@ -41,6 +54,7 @@ try { + _interceptor = family.InstanceInterceptor; determineMementoSource(family); _pluginType = family.PluginType; processPlugins(family.Plugins.All); @@ -162,10 +176,8 @@ private InstanceMemento findMemento(string instanceKey) { - InstanceMemento memento = null; + InstanceMemento memento = _source.GetMemento(instanceKey); - memento = _source.GetMemento(instanceKey); - if (memento == null) { throw new StructureMapException(200, instanceKey, _pluginType.FullName); @@ -198,11 +210,12 @@ InstanceMemento resolvedMemento = _source.ResolveMemento(memento); - return resolvedMemento.Build(this); + object instance = resolvedMemento.Build(this); + return _interceptor.Process(instance); } - public object BuildInstance(InstanceMemento memento) + object IInstanceCreator.BuildInstance(InstanceMemento memento) { if (!_instanceBuilders.ContainsKey(memento.ConcreteKey)) { @@ -210,7 +223,7 @@ 201, memento.ConcreteKey, memento.InstanceKey, PluginType.FullName); } - InstanceBuilder builder = (InstanceBuilder) _instanceBuilders[memento.ConcreteKey]; + InstanceBuilder builder = _instanceBuilders[memento.ConcreteKey]; try { Modified: trunk/Source/StructureMap/InstanceManager.cs =================================================================== --- trunk/Source/StructureMap/InstanceManager.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/InstanceManager.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -4,7 +4,6 @@ using StructureMap.Configuration.DSL; using StructureMap.Exceptions; using StructureMap.Graph; -using StructureMap.Interceptors; namespace StructureMap { @@ -158,6 +157,11 @@ return (T) CreateInstance(typeof (T), instanceKey); } + public T CreateInstance<T>(InstanceMemento memento) + { + return (T) CreateInstance(typeof (T), memento); + } + /// <summary> /// Creates a new object instance of the requested type /// </summary> @@ -263,7 +267,7 @@ instanceFactory.SetDefault(instanceKey); } - public IInstanceFactory this[Type pluginType] + public virtual IInstanceFactory this[Type pluginType] { get { @@ -337,9 +341,9 @@ return (T) FillDependencies(typeof (T)); } - private delegate InstanceFactory CreateFactoryDelegate(Type type); + protected delegate InstanceFactory CreateFactoryDelegate(Type type); - private IInstanceFactory getOrCreateFactory(Type type, CreateFactoryDelegate createFactory) + protected IInstanceFactory getOrCreateFactory(Type type, CreateFactoryDelegate createFactory) { if (!_factories.ContainsKey(type)) { Modified: trunk/Source/StructureMap/InstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/InstanceMemento.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/InstanceMemento.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -17,6 +17,7 @@ private DefinitionSource _definitionSource = DefinitionSource.Explicit; private string _concreteKey; private string _instanceKey; + private InstanceInterceptor _interceptor = new NulloInterceptor(); /// <summary> /// The named type of the object instance represented by the InstanceMemento. Translates to a concrete @@ -268,12 +269,32 @@ protected virtual string getPluggedType() { - return this.getPropertyValue(XmlConstants.PLUGGED_TYPE); + return getPropertyValue(XmlConstants.PLUGGED_TYPE); } - public virtual object Build(IInstanceCreator creator) + public object Build(IInstanceCreator creator) { + object instance = buildInstance(creator); + try + { + return _interceptor.Process(instance); + } + catch (Exception e) + { + throw new StructureMapException(308, e, InstanceKey, TypePath.GetAssemblyQualifiedName(instance.GetType())); + } + } + + protected virtual object buildInstance(IInstanceCreator creator) + { return creator.BuildInstance(this); } + + + public InstanceInterceptor Interceptor + { + get { return _interceptor; } + set { _interceptor = value; } + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Xml; +using StructureMap.Configuration; using StructureMap.Configuration.Tokens; namespace StructureMap.Source @@ -20,17 +21,17 @@ protected override string innerConcreteKey { - get { return _element.GetAttribute("Type"); } + get { return _element.GetAttribute(XmlConstants.TYPE_ATTRIBUTE); } } public void SetConcreteKey(string concreteKey) { - _element.SetAttribute("Type", concreteKey); + _element.SetAttribute(XmlConstants.TYPE_ATTRIBUTE, concreteKey); } protected override string innerInstanceKey { - get { return _element.GetAttribute("Key"); } + get { return _element.GetAttribute(XmlConstants.KEY_ATTRIBUTE); } } public XmlElement InnerElement Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/StructureMap.csproj 2007-12-22 17:45:18 UTC (rev 50) @@ -209,6 +209,7 @@ <Compile Include="Configuration\DiagnosticGraphBuilder.cs"> <SubType>Code</SubType> </Compile> + <Compile Include="Configuration\DSL\ChildArrayExpression.cs" /> <Compile Include="Configuration\DSL\ChildInstanceExpression.cs" /> <Compile Include="Configuration\DSL\ConstructorExpression.cs" /> <Compile Include="Configuration\DSL\CreatePluginFamilyExpression.cs" /> @@ -224,6 +225,7 @@ <Compile Include="Configuration\DSL\PropertyExpression.cs" /> <Compile Include="Configuration\DSL\PrototypeExpression.cs" /> <Compile Include="Configuration\DSL\PrototypeMemento.cs" /> + <Compile Include="Configuration\DSL\ReferenceMementoBuilder.cs" /> <Compile Include="Configuration\DSL\Registry.cs" /> <Compile Include="Configuration\DSL\ScanAssembliesExpression.cs" /> <Compile Include="Configuration\DSL\UserControlExpression.cs" /> @@ -333,6 +335,7 @@ <SubType>Code</SubType> </Compile> <Compile Include="ConstructorMemento.cs" /> + <Compile Include="Delegates.cs" /> <Compile Include="DeploymentTasks\DeploymentConfiguration.cs"> <SubType>Code</SubType> </Compile> Modified: trunk/Source/StructureMap/StructureMapConfiguration.cs =================================================================== --- trunk/Source/StructureMap/StructureMapConfiguration.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/StructureMapConfiguration.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -164,11 +164,11 @@ /// <summary> /// Direct StructureMap to create instances of Type T /// </summary> - /// <typeparam name="T">The Type to build</typeparam> + /// <typeparam name="PLUGINTYPE">The Type to build</typeparam> /// <returns></returns> - public static CreatePluginFamilyExpression BuildInstancesOf<T>() + public static CreatePluginFamilyExpression<PLUGINTYPE> BuildInstancesOf<PLUGINTYPE>() { - return _registry.BuildInstancesOf<T>(); + return _registry.BuildInstancesOf<PLUGINTYPE>(); } /// <summary> Modified: trunk/Source/StructureMap/StructureMapException.resx =================================================================== --- trunk/Source/StructureMap/StructureMapException.resx 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap/StructureMapException.resx 2007-12-22 17:45:18 UTC (rev 50) @@ -252,4 +252,10 @@ <data name="306" xml:space="preserve"> <value>The configured BuilderDelegate of type {0} does not match the PluginFamily type {1}</value> </data> + <data name="307" xml:space="preserve"> + <value>In the call to ChildArray<T>(), the type T must be an array</value> + </data> + <data name="308" xml:space="preserve"> + <value>A configured instance interceptor has failed for Instance '{0}' and concrete type '{1}'</value> + </data> </root> \ No newline at end of file Added: trunk/Source/StructureMap.AutoMocking/AutoMockedInstanceManager.cs =================================================================== --- trunk/Source/StructureMap.AutoMocking/AutoMockedInstanceManager.cs (rev 0) +++ trunk/Source/StructureMap.AutoMocking/AutoMockedInstanceManager.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -0,0 +1,39 @@ +using System; + +namespace StructureMap.AutoMocking +{ + public interface ServiceLocator + { + T Service<T>(); + object Service(Type serviceType); + } + + public class AutoMockedInstanceManager : InstanceManager + { + private readonly ServiceLocator _locator; + + public AutoMockedInstanceManager(ServiceLocator locator) + { + _locator = locator; + } + + public override IInstanceFactory this[Type pluginType] + { + get + { + return getOrCreateFactory(pluginType, + delegate + { + object service = _locator.Service(pluginType); + InstanceFactory factory + = InstanceFactory.CreateFactoryWithDefault(pluginType, service); + + return factory; + }); + + + } + set { base[pluginType] = value; } + } + } +} Added: trunk/Source/StructureMap.AutoMocking/Properties/AssemblyInfo.cs =================================================================== --- trunk/Source/StructureMap.AutoMocking/Properties/AssemblyInfo.cs (rev 0) +++ trunk/Source/StructureMap.AutoMocking/Properties/AssemblyInfo.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -0,0 +1,9 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("StructureMap.AutoMocking")] +[assembly: AssemblyDescription("")] Added: trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs =================================================================== --- trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs (rev 0) +++ trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using Rhino.Mocks; + +namespace StructureMap.AutoMocking +{ + public delegate void GenericVoidMethod<TARGETCLASS>(TARGETCLASS target); + public delegate void VoidMethod(); + + public class RhinoAutoMocker : MockRepository, ServiceLocator + { + private Dictionary<Type, object> _services; + private AutoMockedInstanceManager _manager; + + public RhinoAutoMocker() + { + _services = new Dictionary<Type, object>(); + _manager = new AutoMockedInstanceManager(this); + } + + public TARGETCLASS Create<TARGETCLASS>() + { + throw new NotImplementedException(); + } + + public T Service<T>() + { + throw new NotImplementedException(); + } + + public T UsePartialMock<T>() + { + throw new NotImplementedException(); + } + + public void Inject<T>(T stub) + { + throw new NotImplementedException(); + } + + public object Service(Type serviceType) + { + throw new NotImplementedException(); + } + } +} Added: trunk/Source/StructureMap.AutoMocking/StructureMap.AutoMocking.csproj =================================================================== --- trunk/Source/StructureMap.AutoMocking/StructureMap.AutoMocking.csproj (rev 0) +++ trunk/Source/StructureMap.AutoMocking/StructureMap.AutoMocking.csproj 2007-12-22 17:45:18 UTC (rev 50) @@ -0,0 +1,61 @@ +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>8.0.50727</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{0ED1B206-A1C9-4A52-BA87-3BA416C8725C}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>StructureMap.AutoMocking</RootNamespace> + <AssemblyName>StructureMap.AutoMocking</AssemblyName> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="Rhino.Mocks, Version=2.9.1.10183, Culture=neutral, PublicKeyToken=0b3305902db7183f, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\bin\Rhino.Mocks.dll</HintPath> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="..\CommonAssemblyInfo.cs"> + <Link>CommonAssemblyInfo.cs</Link> + </Compile> + <Compile Include="AutoMockedInstanceManager.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="RhinoAutoMocker.cs" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\StructureMap\StructureMap.csproj"> + <Project>{3F36EA80-2F9A-4DAD-BA27-5AC6163A2EE3}</Project> + <Name>StructureMap</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file Added: trunk/Source/StructureMap.Testing/AutoMocking/RhinoAutoMockerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/AutoMocking/RhinoAutoMockerTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/AutoMocking/RhinoAutoMockerTester.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -0,0 +1,14 @@ +using NUnit.Framework; +using Rhino.Mocks; + +namespace StructureMap.Testing.AutoMocking +{ + [TestFixture] + public class RhinoAutoMockerTester + { + [SetUp] + public void SetUp() + { + } + } +} Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -1,12 +1,11 @@ using NUnit.Framework; -using Rhino.Mocks; using StructureMap.Configuration.DSL; -using StructureMap.Testing.Container; +using StructureMap.Testing.Graph; namespace StructureMap.Testing.Configuration.DSL { [TestFixture] - public class ConstructorExpressionTester + public class ConstructorExpressionTester : Registry { [SetUp] public void SetUp() @@ -15,9 +14,13 @@ ObjectFactory.Reset(); } - public interface Abstraction { } + public interface Abstraction + { + } - public class Concretion : Abstraction { } + public class Concretion : Abstraction + { + } [Test] public void ConstructSomething() @@ -26,7 +29,7 @@ Registry registry = new Registry(); registry.ForRequestedType<Abstraction>().TheDefaultIs( - Registry.ConstructedBy<Abstraction>(delegate { return concretion; }) + ConstructedBy<Abstraction>(delegate { return concretion; }) ); IInstanceManager manager = registry.BuildInstanceManager(); @@ -40,15 +43,14 @@ Registry registry = new Registry(); registry.ForRequestedType<Abstraction>().AddInstance( - Registry.ConstructedBy<Abstraction>(delegate { return concretion; }) + ConstructedBy<Abstraction>(delegate { return concretion; }) ); - + IInstanceManager manager = registry.BuildInstanceManager(); Abstraction actual = manager.GetAllInstances<Abstraction>()[0]; Assert.AreSame(concretion, actual); - } [Test] @@ -59,18 +61,17 @@ Registry registry = new Registry(); registry.ForRequestedType<Abstraction>().AddInstance( - Registry.ConstructedBy<Abstraction>(delegate { return concretion1; }).WithName("One") + ConstructedBy<Abstraction>(delegate { return concretion1; }).WithName("One") ); registry.ForRequestedType<Abstraction>().AddInstance( - Registry.ConstructedBy<Abstraction>(delegate { return concretion2; }).WithName("Two") + ConstructedBy<Abstraction>(delegate { return concretion2; }).WithName("Two") ); IInstanceManager manager = registry.BuildInstanceManager(); Assert.AreSame(concretion1, manager.CreateInstance<Abstraction>("One")); Assert.AreSame(concretion2, manager.CreateInstance<Abstraction>("Two")); - } [Test] @@ -82,19 +83,16 @@ Registry registry = new Registry(); registry.ForRequestedType<Abstraction>() .AddInstance( - Registry.ConstructedBy<Abstraction>(delegate { return concretion1; }).WithName("One") + ConstructedBy<Abstraction>(delegate { return concretion1; }).WithName("One") ) .AddInstance( - Registry.ConstructedBy<Abstraction>(delegate { return concretion2; }).WithName("Two") + ConstructedBy<Abstraction>(delegate { return concretion2; }).WithName("Two") ); IInstanceManager manager = registry.BuildInstanceManager(); Assert.AreSame(concretion1, manager.CreateInstance<Abstraction>("One")); Assert.AreSame(concretion2, manager.CreateInstance<Abstraction>("Two")); - - } + } } - - -} +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2007-12-14 11:16:38 UTC (rev 49) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -16,13 +16,6 @@ { } - [Test, ExpectedException(typeof (StructureMapException))] - public void TheConceteTypeDoesNotCase() - { - Registry registry = new Registry(); - registry.BuildInstancesOf<Rule>().TheDefaultIsConcreteType<IWidget>(); - } - [Test] public void TheDefaultInstanceIsConcreteType() { @@ -104,7 +97,7 @@ PluginGraph pluginGraph = new PluginGraph(); using (Registry registry = new Registry(pluginGraph)) { - CreatePluginFamilyExpression expression = + CreatePluginFamilyExpression<IGateway> expression = registry.BuildInstancesOf<IGateway>(); Assert.IsNotNull(expression); } @@ -119,7 +112,7 @@ PluginGraph pluginGraph = new PluginGraph(); using (Registry registry = new Registry(pluginGraph)) { - CreatePluginFamilyExpression expression = + CreatePluginFamilyExpression<IGateway> expression = registry.BuildInstancesOf<IGateway>().CacheBy(InstanceScope.ThreadLocal); Assert.IsNotNull(expression); } @@ -134,7 +127,7 @@ PluginGraph pluginGraph = new PluginGraph(); using (Registry registry = new Registry(pluginGraph)) { - CreatePluginFamilyExpression expression = + CreatePluginFamilyExpression<IGateway> expression = registry.BuildInstancesOf<IGateway>().AsSingletons(); Assert.IsNotNull(expression); } Added: trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs 2007-12-22 17:45:18 UTC (rev 50) @@ -0,0 +1,216 @@ +using NUnit.Framework; +using StructureMap.Configuration.DSL; + +namespace StructureMap.Testing.Configuration.DSL +{ + [TestFixture] + public class InjectArrayTester + { + [SetUp] + public void SetUp() + { + } + + [Test] + public void ProgrammaticallyInjectArrayAllInline() + { + Registry registry = new Registry(); + + registry.ForRequestedType<Processor>() + .TheDefaultIs( + Registry.Instance<Processor>().UsingConcreteType<Processor>() + .ChildArray<IHandler[]>().Contains( + Registry.Instance<IHandler>().UsingConcreteType<Handler1>(), + Registry.Instance<IHandler>().UsingConcreteType<Handler2>(), + Registry.Instance<IHandler>().UsingConcreteType<Handler3>() + ) + .WithProperty("name").EqualTo("Jeremy") + ); + + IInstanceManager manager = registry.BuildInstanceManager(); + Processor processor = manager.CreateInstance<Processor>(); + + Assert.IsInstanceOfType(typeof (Handler1), processor.Handlers[0]); + Assert.IsInstanceOfType(typeof (Handler2), processor.Handlers[1]); + Assert.IsInstanceOfType(typeof (Handler3), processor.Handlers[2]); + } + + [Test] + public void CanStillAddOtherPropertiesAfterTheCallToChildArray() + { + Registry registry = new Registry(); + + registry.ForRequestedType<Processor>() + .TheDefaultIs( + Registry.Instance<Processor>().UsingConcreteType<Processor>() + .ChildArray<IHandler[]>().Contains( + Registry.Instance<IHandler>().UsingConcreteType<Handler1>(), + Registry.Instance<IHandler>().UsingConcreteType<Handler2>(), + Registry.Instance<IHandler>().UsingConcreteType<Handler3>() + ) + .WithProperty("name").EqualTo("Jeremy") + ); + + IInstanceManager manager = registry.BuildInstanceManager(); + Processor processor = manager.CreateInstance<Processor>(); + + Assert.AreEqual("Jeremy", processor.Name); + } + + [Test, + ExpectedException(typeof (StructureMapException), + ExpectedMessage = + "StructureMap Exception Code: 307\nIn the call to ChildArray<T>(), the type T must be an array")] + public void TryToInjectByTheElementTypeInsteadOfTheArrayType() + { + Registry registry = new Registry(); + + registry.ForRequestedType<Processor>() + .TheDefaultIs( + Registry.Instance<Processor>().UsingConcreteType<Processor>() + .WithProperty("name").EqualTo("Jeremy") + .ChildArray<IHandler>().Contains( + Registry.Instance<IHandler>().UsingConcreteType<Handler1>()) + ); + } + + + [Test, + ExpectedException(typeof (StructureMapException), + ExpectedMessage = + "StructureMap Exception Code: 307\nIn the call to ChildArray<T>(), the type T must be an array")] + public void InjectPropertiesByNameButUseTheElementType() + { + Registry registry = new Registry(); + + registry.ForRequestedType<Processor2>() + .TheDefaultIs( + Registry.Instance<Processor2>().UsingConcreteType<Processor2>() + .ChildArray<IHandler>("first").Contains( + Registry.Instance<IHandler>().UsingConcreteType<Handler1>(), + Registry.Instance<IHandler>().UsingConcreteType<Handler2>() + ) + .ChildArray<IHandler[]>("second").Contains( + Registry.Instance<IHandler>().UsingConcreteType<Handler2>(), + Registry.Instance<IHandler>().UsingConcreteType<Handler3>() + ) + ); + } + + [Test] + public void InjectPropertiesByName() + { + Registry registry = new Registry(); + + registry.ForRequestedType<Processor2>() + .TheDefaultIs( + Registry.Instance<Processor2>().UsingConcreteType<Processor2>() + .ChildArray<IHandler[]>("first").Contains( + Registry.Instance<IHandler>().UsingConcreteType<Handler1>(), + Registry.Instance<IHandler>().UsingConcreteType<Handler2>() + ) + .ChildArray<IHandler[]>("second").Contains( + Registry.Instance<IHandler>().UsingConcreteType<Handler2>(), + Registry.Instance<IHandler>().UsingConcreteType<Handler3>() + ) + ); + + IInstanceManager manager = registry.BuildInstanceManager(); + Processor2 processor = manager.CreateInstance<Processor2>(); + + Assert.IsInstanceOfType(typeof (Handler1), processor.First[0]); + Assert.IsInstanceOfType(typeof (Handler2), processor.First[1]); + Assert.IsInstanceOfType(typeof (Handler2), processor.Second[0]); + Assert.IsInstanceOfType(typeof (Handler3), processor.Second[1]); + } + + [Test] + public void PlaceMemberInArrayByReference() + { + Registry registry = new Registry(); + registry.AddInstanceOf<IHandler>().UsingConcreteType<Handler1>().WithName("One"); + registry.AddInstanceOf<IHandler>().UsingConcreteType<Handler2>().WithName("Two"); + + registry.ForRequestedType<Processor>() + .TheDefaultIs( + Registry.Instance<Processor>().UsingConcreteType<Processor>() + .WithProperty("name").EqualTo("Jeremy") + .ChildArray<IHandler[]>().Contains( + Registry.Instance("Two"), + Registry.Instance("One") + ) + ); + + IInstanceManager manager = registry.BuildInstanceManager(); + Processor processor = manager.CreateInstance<Processor>(); + + Assert.IsInstanceOfType(typeof(Handler2), processor.Handlers[0]); + Assert.IsInstanceOfType(typeof(Handler1), pr... [truncated message content] |