Permalink
Browse files

added <Registry> to the xml configuration. Finishing the 2.5.3 release

  • Loading branch information...
1 parent 8d9dcb2 commit a50939024fb960f8f09ba7b92b44a5c13f818c23 @jeremydmiller jeremydmiller committed Feb 1, 2009
@@ -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.&nbsp; This may be valuable in systems where you might be
+ completely unable to do programmatic bootstrapping or modify the application
+ startup to add bootstrapping.&nbsp; Just add a &lt;Registry&gt; 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 &lt;?\cf3 xml\cf1 \cf4 version\cf1 =\cf0 "\cf1 1.0\cf0 "\cf1 \cf4 encoding\cf1 =\cf0 "\cf1 utf-8\cf0 "\cf1 ?&gt;\par ??&lt;\cf3 StructureMap\cf1 &gt;\par ?? &lt;!--\cf6 Configure a Registry by just specifying the Assembly Qualified Name of a Registry Type \cf1 --&gt;\par ?? &lt;\cf3 Registry\cf1 \cf4 Type\cf1 =\cf0 "\cf1 StructureMap.Testing.XmlFileRegistry, StructureMap.Testing\cf0 "\cf1 &gt;&lt;/\cf3 Registry\cf1 &gt;\par ??&lt;/\cf3 StructureMap\cf1 &gt;}
+-->
+ <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;">&lt;?</span><span style="color: #a31515;">xml</span><span
+ style="color: blue;"> </span><span style="color: red;">version</span><span
+ style="color: blue;">=</span>&quot;<span style="color: blue;">1.0</span>&quot;<span
+ style="color: blue;"> </span><span style="color: red;">encoding</span><span
+ style="color: blue;">=</span>&quot;<span style="color: blue;">utf-8</span>&quot;<span
+ style="color: blue;"> ?&gt;</span></p>
+ <p style="margin: 0px;">
+ <span style="color: blue;">&lt;</span><span style="color: #a31515;">StructureMap</span><span
+ style="color: blue;">&gt;</span></p>
+ <p style="margin: 0px;">
+ <span style="color: blue;">&nbsp; &lt;!--</span><span style="color: green;">
+ Configure a Registry by just specifying the Assembly Qualified Name of a
+ Registry Type </span><span style="color: blue;">--&gt;</span></p>
+ <p style="margin: 0px;">
+ <span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">Registry</span><span
+ style="color: blue;"> </span><span style="color: red;">Type</span><span
+ style="color: blue;">=</span>&quot;<span style="color: blue;">StructureMap.Testing.XmlFileRegistry,
+ StructureMap.Testing</span>&quot;<span style="color: blue;">&gt;&lt;/</span><span
+ style="color: #a31515;">Registry</span><span style="color: blue;">&gt;</span></p>
+ <p style="margin: 0px;">
+ <span style="color: blue;">&lt;/</span><span style="color: #a31515;">StructureMap</span><span
+ style="color: blue;">&gt;</span></p>
+ </div>
+<!--EndFragment-->
+<hr />
</body>
</html>
@@ -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>
@@ -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");
+ }
+ }
+}
@@ -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" />
@@ -123,7 +123,11 @@ public BuildStack BuildStack
public Type ParentType
{
- get { return _buildStack.Parent.ConcreteType; }
+ get
+ {
+ if (_buildStack.Parent != null) return _buildStack.Parent.ConcreteType;
+ return null;
+ }
}
T IContext.GetInstance<T>()
@@ -96,6 +96,11 @@ public void ParseAssemblies(IGraphBuilder builder)
_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)
{
@@ -152,6 +152,11 @@ public CreatePluginFamilyExpression<PLUGINTYPE> OnCreation(Action<PLUGINTYPE> ha
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 @@ private void registerDefault(Instance instance)
});
}
+ /// <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());
@@ -58,6 +58,16 @@ public void AddAssembly(string assemblyName)
}
}
+ 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);
@@ -17,6 +17,7 @@ public interface IGraphBuilder
{
PluginGraph PluginGraph { get; }
void AddAssembly(string assemblyName);
+ void AddRegistry(string registryTypeName);
void FinishFamilies();
@@ -32,5 +32,6 @@ public class XmlConstants
public const string SCOPE = "Scope";
public const string STRUCTUREMAP = "StructureMap";
public const string TYPE_ATTRIBUTE = "Type";
+ public const string REGISTRY = "Registry";
}
}
@@ -62,6 +62,7 @@ public PluginGraph Build()
{
_graph.Log.StartSource(p.Description);
p.ParseAssemblies(graphBuilder);
+ p.ParseRegistries(graphBuilder);
});
forAllParsers(p => p.Parse(graphBuilder));
@@ -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>

0 comments on commit a509390

Please sign in to comment.