From: <jer...@us...> - 2009-02-01 20:02:18
|
Revision: 231 http://structuremap.svn.sourceforge.net/structuremap/?rev=231&view=rev Author: jeremydmiller Date: 2009-02-01 20:02:05 +0000 (Sun, 01 Feb 2009) Log Message: ----------- added <Registry> to the xml configuration. Finishing the 2.5.3 release Modified Paths: -------------- trunk/Source/HTML/XmlConfiguration.htm trunk/Source/StructureMap/BuildSession.cs trunk/Source/StructureMap/Configuration/ConfigurationParser.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Configuration/GraphBuilder.cs trunk/Source/StructureMap/Configuration/IGraphBuilder.cs trunk/Source/StructureMap/Configuration/XmlConstants.cs trunk/Source/StructureMap/PluginGraphBuilder.cs trunk/Source/StructureMap/StructureMapException.resx trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap.Testing/Examples/AddRegistryInXml.xml trunk/Source/StructureMap.Testing/ReadingRegistriesFromXmlTester.cs Modified: trunk/Source/HTML/XmlConfiguration.htm =================================================================== --- trunk/Source/HTML/XmlConfiguration.htm 2009-02-01 18:34:03 UTC (rev 230) +++ trunk/Source/HTML/XmlConfiguration.htm 2009-02-01 20:02:05 UTC (rev 231) @@ -542,8 +542,43 @@ </div> <!--EndFragment--> <hr /> - + <h2>Referencing Registries from Xml</h2> + <p>StructureMap 2.5.3 adds the ability to include Registry configuration through Xml + configuration. This may be valuable in systems where you might be + completely unable to do programmatic bootstrapping or modify the application + startup to add bootstrapping. Just add a <Registry> node to Xml + configuration and put the assembly qualified name of the Registry class into the + @Type attribute like this:</p> +<!-- +{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red163\green21\blue21;\red255\green0\blue0;\red0\green0\blue0;\red0\green128\blue0;}??\fs20 \cf1 <?\cf3 xml\cf1 \cf4 version\cf1 =\cf0 "\cf1 1.0\cf0 "\cf1 \cf4 encoding\cf1 =\cf0 "\cf1 utf-8\cf0 "\cf1 ?>\par ??<\cf3 StructureMap\cf1 >\par ?? <!--\cf6 Configure a Registry by just specifying the Assembly Qualified Name of a Registry Type \cf1 -->\par ?? <\cf3 Registry\cf1 \cf4 Type\cf1 =\cf0 "\cf1 StructureMap.Testing.XmlFileRegistry, StructureMap.Testing\cf0 "\cf1 ></\cf3 Registry\cf1 >\par ??</\cf3 StructureMap\cf1 >} +--> + <div style="font-family: Courier New; font-size: 10pt; color: black; background: white; border: black thin solid;"> + <p style="margin: 0px;"> + <span style="color: blue;"><?</span><span style="color: #a31515;">xml</span><span + style="color: blue;"> </span><span style="color: red;">version</span><span + style="color: blue;">=</span>"<span style="color: blue;">1.0</span>"<span + style="color: blue;"> </span><span style="color: red;">encoding</span><span + style="color: blue;">=</span>"<span style="color: blue;">utf-8</span>"<span + style="color: blue;"> ?></span></p> + <p style="margin: 0px;"> + <span style="color: blue;"><</span><span style="color: #a31515;">StructureMap</span><span + style="color: blue;">></span></p> + <p style="margin: 0px;"> + <span style="color: blue;"> <!--</span><span style="color: green;"> + Configure a Registry by just specifying the Assembly Qualified Name of a + Registry Type </span><span style="color: blue;">--></span></p> + <p style="margin: 0px;"> + <span style="color: blue;"> <</span><span style="color: #a31515;">Registry</span><span + style="color: blue;"> </span><span style="color: red;">Type</span><span + style="color: blue;">=</span>"<span style="color: blue;">StructureMap.Testing.XmlFileRegistry, + StructureMap.Testing</span>"<span style="color: blue;">></</span><span + style="color: #a31515;">Registry</span><span style="color: blue;">></span></p> + <p style="margin: 0px;"> + <span style="color: blue;"></</span><span style="color: #a31515;">StructureMap</span><span + style="color: blue;">></span></p> + </div> +<!--EndFragment--> +<hr /> - </body> </html> \ No newline at end of file Modified: trunk/Source/StructureMap/BuildSession.cs =================================================================== --- trunk/Source/StructureMap/BuildSession.cs 2009-02-01 18:34:03 UTC (rev 230) +++ trunk/Source/StructureMap/BuildSession.cs 2009-02-01 20:02:05 UTC (rev 231) @@ -123,7 +123,11 @@ public Type ParentType { - get { return _buildStack.Parent.ConcreteType; } + get + { + if (_buildStack.Parent != null) return _buildStack.Parent.ConcreteType; + return null; + } } T IContext.GetInstance<T>() Modified: trunk/Source/StructureMap/Configuration/ConfigurationParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ConfigurationParser.cs 2009-02-01 18:34:03 UTC (rev 230) +++ trunk/Source/StructureMap/Configuration/ConfigurationParser.cs 2009-02-01 20:02:05 UTC (rev 231) @@ -96,7 +96,12 @@ _structureMapNode.ForTextInChild("Assembly/@Name").Do(name => builder.AddAssembly(name)); } + public void ParseRegistries(IGraphBuilder builder) + { + _structureMapNode.ForTextInChild("Registry/@Type").Do(name => builder.AddRegistry(name)); + } + private XmlExtensions.XmlNodeExpression forEachNode(string xpath) { return _structureMapNode.ForEachChild(xpath); Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-02-01 18:34:03 UTC (rev 230) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-02-01 20:02:05 UTC (rev 231) @@ -152,6 +152,11 @@ return this; } + /// <summary> + /// Adds an Interceptor to only this PluginType + /// </summary> + /// <param name="interceptor"></param> + /// <returns></returns> public CreatePluginFamilyExpression<PLUGINTYPE> InterceptWith(InstanceInterceptor interceptor) { _children.Add( @@ -302,6 +307,11 @@ }); } + /// <summary> + /// Forces StructureMap to always use a unique instance to + /// stop the "BuildSession" caching + /// </summary> + /// <returns></returns> public CreatePluginFamilyExpression<PLUGINTYPE> AlwaysUnique() { return InterceptConstructionWith(new UniquePerRequestInterceptor()); Modified: trunk/Source/StructureMap/Configuration/GraphBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/GraphBuilder.cs 2009-02-01 18:34:03 UTC (rev 230) +++ trunk/Source/StructureMap/Configuration/GraphBuilder.cs 2009-02-01 20:02:05 UTC (rev 231) @@ -58,6 +58,16 @@ } } + public void AddRegistry(string registryTypeName) + { + _pluginGraph.Log.Try(()=> + { + var type = new TypePath(registryTypeName).FindType(); + Registry registry = (Registry) Activator.CreateInstance(type); + registry.ConfigurePluginGraph(_pluginGraph); + }).AndReportErrorAs(290, registryTypeName); + } + public IProfileBuilder GetProfileBuilder() { return new ProfileBuilder(_pluginGraph); Modified: trunk/Source/StructureMap/Configuration/IGraphBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/IGraphBuilder.cs 2009-02-01 18:34:03 UTC (rev 230) +++ trunk/Source/StructureMap/Configuration/IGraphBuilder.cs 2009-02-01 20:02:05 UTC (rev 231) @@ -17,6 +17,7 @@ { PluginGraph PluginGraph { get; } void AddAssembly(string assemblyName); + void AddRegistry(string registryTypeName); void FinishFamilies(); Modified: trunk/Source/StructureMap/Configuration/XmlConstants.cs =================================================================== --- trunk/Source/StructureMap/Configuration/XmlConstants.cs 2009-02-01 18:34:03 UTC (rev 230) +++ trunk/Source/StructureMap/Configuration/XmlConstants.cs 2009-02-01 20:02:05 UTC (rev 231) @@ -32,5 +32,6 @@ public const string SCOPE = "Scope"; public const string STRUCTUREMAP = "StructureMap"; public const string TYPE_ATTRIBUTE = "Type"; + public const string REGISTRY = "Registry"; } } \ No newline at end of file Modified: trunk/Source/StructureMap/PluginGraphBuilder.cs =================================================================== --- trunk/Source/StructureMap/PluginGraphBuilder.cs 2009-02-01 18:34:03 UTC (rev 230) +++ trunk/Source/StructureMap/PluginGraphBuilder.cs 2009-02-01 20:02:05 UTC (rev 231) @@ -62,6 +62,7 @@ { _graph.Log.StartSource(p.Description); p.ParseAssemblies(graphBuilder); + p.ParseRegistries(graphBuilder); }); forAllParsers(p => p.Parse(graphBuilder)); Modified: trunk/Source/StructureMap/StructureMapException.resx =================================================================== --- trunk/Source/StructureMap/StructureMapException.resx 2009-02-01 18:34:03 UTC (rev 230) +++ trunk/Source/StructureMap/StructureMapException.resx 2009-02-01 20:02:05 UTC (rev 231) @@ -268,4 +268,7 @@ <data name="285" xml:space="preserve"> <value>Only open generic types can be used in the call to Container.ForGenericType(type)</value> </data> + <data name="290" xml:space="preserve"> + <value>Could not load the designated Registry class '{0}'</value> + </data> </root> \ No newline at end of file Added: trunk/Source/StructureMap.Testing/Examples/AddRegistryInXml.xml =================================================================== --- trunk/Source/StructureMap.Testing/Examples/AddRegistryInXml.xml (rev 0) +++ trunk/Source/StructureMap.Testing/Examples/AddRegistryInXml.xml 2009-02-01 20:02:05 UTC (rev 231) @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="utf-8" ?> +<StructureMap> + <!-- Configure a Registry by just specifying the Assembly Qualified Name of a Registry Type --> + <Registry Type="StructureMap.Testing.XmlFileRegistry, StructureMap.Testing"></Registry> +</StructureMap> \ No newline at end of file Added: trunk/Source/StructureMap.Testing/ReadingRegistriesFromXmlTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/ReadingRegistriesFromXmlTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/ReadingRegistriesFromXmlTester.cs 2009-02-01 20:02:05 UTC (rev 231) @@ -0,0 +1,78 @@ +using System; +using System.Diagnostics; +using System.Xml; +using NUnit.Framework; +using StructureMap.Configuration; +using StructureMap.Configuration.DSL; +using StructureMap.Graph; +using StructureMap.Testing.Widget; + +namespace StructureMap.Testing +{ + [TestFixture] + public class ReadingRegistriesFromXmlTester + { + [SetUp] + public void SetUp() + { + } + + public void TheXmlFileRegistryWasLoadedInto(IContainer container) + { + container.GetInstance<ColorRule>().Color.ShouldEqual("Cornflower"); + } + + [Test] + public void graphbuilder_can_add_a_registry_directly() + { + var graph = new PluginGraph(); + var builder = new GraphBuilder(new Registry[0], graph); + builder.AddRegistry(typeof(XmlFileRegistry).AssemblyQualifiedName); + + var container = new Container(graph); + TheXmlFileRegistryWasLoadedInto(container); + } + + [Test] + public void handles_failures_gracefully_if_the_registry_cannot_be_loaded() + { + //290 + var graph = new PluginGraph(); + var builder = new GraphBuilder(new Registry[0], graph); + builder.AddRegistry("an invalid type name"); + + graph.Log.ErrorCount.ShouldEqual(1); + graph.Log.AssertHasError(290); + + } + + [Test] + public void read_registry_from_xml() + { + var document = new XmlDocument(); + document.LoadXml("<StructureMap><Registry></Registry></StructureMap>"); + document.DocumentElement.FirstChild.ShouldBeOfType<XmlElement>().SetAttribute("Type", + typeof (XmlFileRegistry). + AssemblyQualifiedName); + + Debug.WriteLine(document.OuterXml); + + var container = new Container(x => + { + x.AddConfigurationFromNode(document.DocumentElement); + }); + + TheXmlFileRegistryWasLoadedInto(container); + + } + + } + + public class XmlFileRegistry : Registry + { + public XmlFileRegistry() + { + ForConcreteType<ColorRule>().Configure.WithCtorArg("color").EqualTo("Cornflower"); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-02-01 18:34:03 UTC (rev 230) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-02-01 20:02:05 UTC (rev 231) @@ -367,6 +367,7 @@ <DesignTimeSharedInput>True</DesignTimeSharedInput> <DependentUpon>Settings.settings</DependentUpon> </Compile> + <Compile Include="ReadingRegistriesFromXmlTester.cs" /> <Compile Include="SpecificationExtensions.cs" /> <Compile Include="StructureMapConfigCreator.cs" /> <Compile Include="StructureMapConfigurationDefensiveChecksTester.cs" /> @@ -444,6 +445,7 @@ <EmbeddedResource Include="TestData\PluggedTypeTest.xml" /> </ItemGroup> <ItemGroup> + <Content Include="Examples\AddRegistryInXml.xml" /> <Content Include="Sample.xml" /> <Content Include="TestData\ProfileSample.xml" /> <EmbeddedResource Include="TestData\ShortInstance.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |