<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Unity</title>
        <link>http://agilior.pt/blogs/pedro.rainho/category/52.aspx</link>
        <description>Unity</description>
        <language>pt-PT</language>
        <copyright>Pedro Rainho</copyright>
        <managingEditor>pedro.rainho@agilior.pt</managingEditor>
        <generator>Subtext Version 1.9.0.27</generator>
        <item>
            <title>Playing with Unity</title>
            <link>http://agilior.pt/blogs/pedro.rainho/archive/2008/07/14/5017.aspx</link>
            <description>&lt;p&gt;Lately I'm using &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt; and to me has one big problem.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;But first I will talk a little about .Net Framework in particular the configuration. &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;In .Net Framework there are tree classes named ConfigurationElement, ConfigurationElementCollection and ConfigurationSection and these classes are available in System.Configuration. They exists so we can access configuration within our code, and change something's. I say something's because we could not change much because the .Net team put the usefully methods as protected and only useless methods are public.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;So let's imagine that I have a class XXXConfigurationSection and that I want to serialize this class to another .config file different that web.config/app.config or I simple want to serialize and insert configuration on DB.&lt;/p&gt;  &lt;p&gt;2 solutions, the person that create the class XXXConfigurationSection expose as public the SerializeSection and DeserializeSection methods or I have to create a SerializableXXXConfiguration that inheritance XXXConfigurationSection and then that class exposes the protected methods and do something like this:&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;SerializableXXXConfiguration : XXXConfigurationSection
{
    &lt;span style="color: blue"&gt;private const string &lt;/span&gt;XXXConfigurationName = &lt;span style="color: #a31515"&gt;"xxx"&lt;/span&gt;;

    &lt;span style="color: blue"&gt;public void &lt;/span&gt;ReadConfiguration(XmlReader reader)
    {
        reader.Read();
        DeserializeSection(reader);
    }

    &lt;span style="color: blue"&gt;public void &lt;/span&gt;WriteConfiguration(XmlWriter writer)
    {
        &lt;span style="color: #2b91af"&gt;String &lt;/span&gt;serialized = SerializeSection(&lt;span style="color: blue"&gt;this&lt;/span&gt;, XXXConfigurationName, ConfigurationSaveMode.Full);
        writer.WriteRaw(serialized);
    }&lt;/pre&gt;

&lt;pre class="code"&gt;}&lt;/pre&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;But the story continues and let's imagine that I have a class XXXCollection  :   ConfigurationElementCollection and me programmer want to do this&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;XXXCollection xxxCol = new XXXCollection();&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;and then I want to add some elements to the collection I ask, where are the add methods???? well either the person that had done class XXXCollection expose the protected methods as public or I'm literally fu.... &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;In conclusion this configuration is only to read and not to write. But then a question appear... How can I deploy my configuration dynamically??? Do I need to have different configuration files for every case, do I need inherit form every configuration class and expose the protected methods... &lt;/p&gt;

&lt;p&gt;what is the best solution for this problem??? the answer is there is no best solution :(&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;And now I will enter again in &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt;. The &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt; configuration suffers the same problem.&lt;/p&gt;

&lt;p&gt;if you want to use &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt; configuration you will have to write configuration all by hand in the .config file, and now I ask again, what if I want to have different configurations depending on my deploy, how will I manage this problem. &lt;/p&gt;

&lt;p&gt;A simple example:&lt;/p&gt;

&lt;p&gt;imagine that I have a interface ILogger and I want to deploy 2 web applications one for customers and other for backoffice. For one web app want to resolve ILogger as class LoggerTrace that implements ILogger and in the other application I want resolve ILogger as class NullLogger that also implements ILogger.&lt;/p&gt;

&lt;p&gt;Now, do I have to write configuration by hand ?&lt;/p&gt;

&lt;p&gt;Do I have 2 configuration files prepared and the it's just copy the files ?&lt;/p&gt;

&lt;p&gt;What do I need to do, what I want is just dynamic deploy the applications....&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Personally I don't know any directly way to resolve this problem.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;But... there is always a but, with help of &lt;a href="http://nunogomes.net/"&gt;Nuno Gomes&lt;/a&gt; there is a solution.&lt;/p&gt;

&lt;p&gt;The solution consists in access directly the protected methods or properties depending in what we want to do.&lt;/p&gt;

&lt;p&gt;So if I want to deploy my &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt; configuration I will only need to have a few auxiliary methods that manage to access protected methods.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;With this solution I don't have to inherit from anything.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;So in case of &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt; Configuration I had create this solution:&lt;/p&gt;

&lt;p&gt;Create a class SerializableUnityConfiguration that enables me to Serialize and Deserialize &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt; configuration, exposing PUBLIC methods&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;SerializableUnityConfiguration : UnityConfigurationSection
{
    &lt;span style="color: blue"&gt;private const string &lt;/span&gt;UnityConfigurationName = &lt;span style="color: #a31515"&gt;"unity"&lt;/span&gt;;

    &lt;span style="color: blue"&gt;public void &lt;/span&gt;ReadConfiguration(XmlReader reader)
    {
        reader.Read();
        DeserializeSection(reader);
    }

    &lt;span style="color: blue"&gt;public void &lt;/span&gt;WriteConfiguration(XmlWriter writer)
    {
        &lt;span style="color: #2b91af"&gt;String &lt;/span&gt;serialized = SerializeSection(&lt;span style="color: blue"&gt;this&lt;/span&gt;, UnityConfigurationName, ConfigurationSaveMode.Full);
        writer.WriteRaw(serialized);
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;XmlReader GetReader()
    {
        &lt;span style="color: #2b91af"&gt;String &lt;/span&gt;serialized = SerializeSection(&lt;span style="color: blue"&gt;this&lt;/span&gt;, UnityConfigurationName, ConfigurationSaveMode.Full);

        &lt;span style="color: #2b91af"&gt;StringReader &lt;/span&gt;strReader = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StringReader&lt;/span&gt;(serialized);

        &lt;span style="color: blue"&gt;return &lt;/span&gt;XmlReader.Create(strReader);

    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Then I have a few methods that enable me to manipulate the protected methods that exists in configuration classes. &lt;/p&gt;

&lt;p&gt;If I want to add a container to &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt; configuration I just had to do this:&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;UnityContainerElement AddContainer(UnityConfigurationSection unitySection, &lt;span style="color: blue"&gt;string &lt;/span&gt;containerName)
{
    &lt;span style="color: blue"&gt;#region &lt;/span&gt;Validation
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(unitySection == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
    {
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"unitySection is null"&lt;/span&gt;);
    }

    &lt;span style="color: blue"&gt;if &lt;/span&gt;(containerName == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
    {
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"containerName is null"&lt;/span&gt;);
    }
    &lt;span style="color: blue"&gt;#endregion

    if &lt;/span&gt;(unitySection.Containers.Count == 0 ||
        unitySection.Containers[containerName] == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
    {
        UnityContainerElement containerElement = &lt;span style="color: blue"&gt;new &lt;/span&gt;UnityContainerElement();

        &lt;span style="color: blue"&gt;if &lt;/span&gt;(!&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(containerName))
        {
            containerElement.Name = containerName;
        }

        AddCollectionItem(unitySection.Containers, containerElement);
    }

    &lt;span style="color: blue"&gt;return &lt;/span&gt;unitySection.Containers[containerName];
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;AddCollectionItem(ConfigurationElementCollection collection, ConfigurationElement element)
{&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;    if &lt;/span&gt;(collection == &lt;span style="color: blue"&gt;null&lt;/span&gt;) &lt;/pre&gt;

&lt;pre class="code"&gt;    {&lt;/pre&gt;

&lt;pre class="code"&gt;        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"collection is null"&lt;/span&gt;); &lt;/pre&gt;

&lt;pre class="code"&gt;    }&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;    if &lt;/span&gt;(element== &lt;span style="color: blue"&gt;null&lt;/span&gt;) &lt;/pre&gt;

&lt;pre class="code"&gt;    {&lt;/pre&gt;

&lt;pre class="code"&gt;        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"element is null"&lt;/span&gt;); &lt;/pre&gt;

&lt;pre class="code"&gt;    }&lt;/pre&gt;

&lt;pre class="code"&gt;    

    &lt;span style="color: #2b91af"&gt;MethodInfo &lt;/span&gt;baseAddMethod = collection.GetType().GetMethod(&lt;span style="color: #a31515"&gt;"BaseAdd"&lt;/span&gt;, System.Reflection.&lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance | System.Reflection.&lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Public | System.Reflection.&lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.NonPublic, &lt;span style="color: blue"&gt;null&lt;/span&gt;, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;[] { &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(ConfigurationElement) }, &lt;span style="color: blue"&gt;null&lt;/span&gt;);
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(baseAddMethod != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
    {
        baseAddMethod.Invoke(collection, &lt;span style="color: blue"&gt;new object&lt;/span&gt;[] { element });
        &lt;span style="color: blue"&gt;return&lt;/span&gt;;
    }
    &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"Not a collection"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"collection"&lt;/span&gt;);
}&lt;/pre&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;SerializableUnityConfiguration  config = new SerializableUnityConfiguration ();&lt;/p&gt;

&lt;p&gt;AddContainer(config , "TestContainer");&lt;/p&gt;

&lt;p&gt;config.WriteConfiguration(...)&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;And I will have the &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt; configuration like this:&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;unity&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;containers&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;clear &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;container &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;TestContainer&lt;/span&gt;"&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;clear &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;
      &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;extensions&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;clear &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
      &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;extensions&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;instances&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;clear &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
      &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;instances&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;extensionConfig&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;clear &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
      &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;extensionConfig&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;container&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;containers&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;typeAliases&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;clear &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;typeAliases&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;unity&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;/pre&gt;
Now it's just few methods and I will be enable to write to every section of the configuration :). And since this code will only run during deployment there will be no problems will speed because I'm using reflection :)&lt;img src="http://agilior.pt/blogs/pedro.rainho/aggbug/5017.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Pedro Rainho</dc:creator>
            <guid>http://agilior.pt/blogs/pedro.rainho/archive/2008/07/14/5017.aspx</guid>
            <pubDate>Mon, 14 Jul 2008 16:48:50 GMT</pubDate>
            <wfw:comment>http://agilior.pt/blogs/pedro.rainho/comments/5017.aspx</wfw:comment>
            <comments>http://agilior.pt/blogs/pedro.rainho/archive/2008/07/14/5017.aspx#feedback</comments>
            <wfw:commentRss>http://agilior.pt/blogs/pedro.rainho/comments/commentRss/5017.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>