<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1319</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1319" rel="self"/>
  <title>dotnetchris</title>
  <updated>Fri Sep 25 19:20:56 -0700 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1049</id>
    <published>2009-09-25T19:20:56-07:00</published>
    <updated>2009-10-05T16:58:54-07:00</updated>
    <title>[C#] XML Object Serializer to XSLT Transformer</title>
    <content type="html">&lt;p&gt;I'm using XSLT for document generation and have code to serialize my objects into XML and then apply to the transform. Since I built alot of it based on intellisense suggestions and random postings on the internet I assume it could probably be improved marginally atleast.&lt;/p&gt;

&lt;pre&gt;public static class ObjectToXmlConverter
{
    #region Class Methods

    /// &amp;lt;summary&amp;gt;
    /// Renders the specified serializable obj.
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;serializableObj&amp;quot;&amp;gt;The serializable obj.&amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
    public static string Render&amp;lt;T&amp;gt;(T serializableObj)
    {
        if (!typeof (T).IsSerializable)
            throw new SerializationException(string.Format(&amp;quot;{0} is not marked Serializable.&amp;quot;, typeof(T)));

        var xmls = new XmlSerializer(typeof (T));
        using (var ms = new MemoryStream())
        {
            var settings = new XmlWriterSettings
                               {
                                   Encoding = Encoding.UTF8,
                                   Indent = true,
                                   IndentChars = &amp;quot;\t&amp;quot;,
                                   NewLineChars = Environment.NewLine,
                                   ConformanceLevel = ConformanceLevel.Document
                               };

            using (var writer = XmlWriter.Create(ms, settings))
            {
                xmls.Serialize(writer, serializableObj);
            }


            string xml = Encoding.UTF8.GetString(ms.ToArray());

            if (xml.Length &amp;gt; 0 &amp;amp;&amp;amp; xml[0] != '&amp;lt;')
            {
                xml = xml.Substring(1, xml.Length - 1);
            }

            return xml;
        }
    }

    #endregion
}

public static class XsltTransformer
{
    #region Class Methods

    /// &amp;lt;summary&amp;gt;
    /// Renders the specified serializable obj.
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;param name=&amp;quot;serializableObj&amp;quot;&amp;gt;The serializable obj.&amp;lt;/param&amp;gt;
    /// &amp;lt;param name=&amp;quot;xsltPath&amp;quot;&amp;gt;The XSLT path.&amp;lt;/param&amp;gt;
    public static XmlDocument Render&amp;lt;T&amp;gt;(T serializableObj, string xsltPath)
    {
        var rawXml = ObjectToXmlConverter.Render(serializableObj);
        rawXml = StripInvalidCharacters(rawXml);

        var document = new XmlDocument();
        document.LoadXml(rawXml);

        var xDoc = XDocument.Load(new XmlNodeReader(document));
        var transformedDoc = new XDocument();

        using (var writer = transformedDoc.CreateWriter())
        {
            var transform = new XslCompiledTransform();
            transform.Load(XmlReader.Create(new StreamReader(xsltPath)));
            transform.Transform(xDoc.CreateReader(), writer);
        }


        var xmlDocument = new XmlDocument();
        xmlDocument.Load(transformedDoc.CreateReader());

        return xmlDocument;
    }

    /// &amp;lt;summary&amp;gt;
    /// Strips the illegal characters.
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;rawXml&amp;quot;&amp;gt;The raw XML.&amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
    private static string StripInvalidCharacters(string rawXml)
    {
        rawXml = rawXml.Replace(&amp;quot;\r&amp;quot;, string.Empty).Replace(&amp;quot;\n&amp;quot;, string.Empty).Replace(&amp;quot;\t&amp;quot;, string.Empty);
        return rawXml;
    }

    #endregion
}&lt;/pre&gt;</content>
    <author>
      <name>dotnetchris</name>
      <email>chris@marisic.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1049-xml-object-serializer-to-xslt-transformer" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code736</id>
    <published>2009-02-05T04:05:56-08:00</published>
    <updated>2009-02-05T04:05:56-08:00</updated>
    <title>[C#] Presenter injection in Model-View-Presenter pattern with StructureMap</title>
    <content type="html">&lt;p&gt;I've implemented my own copy of the model view presenter pattern (in vein of web client software factory) so I can leverage my own DI framework instead of being tied to WCSF's ObjectBuilder which I had numerous problems with. I've come up with a few ways to do it but none of them particularly make me happy. I wanted to know if anyone else had some other ideas.&lt;/p&gt;

&lt;pre&gt;//Solution #1a
//Uses a HttpModule to intercept context.PreRequestHandlerExecute to call ObjectFactory.BuildUp(HttpContext.Current.Handler)

public partial class _Default : Page, IEmployeeView
{
    private EmployeePresenter _presenter;

    private EmployeePresenter Presenter
    {
        set
        {
            _presenter = value;
            _presenter.View = this;
        }
    }
}

//Solution #1b
//Call buildup in page load instead of using a HttpModule

public partial class _Default : Page, IEmployeeView
{
    private EmployeePresenter _presenter;

    private EmployeePresenter Presenter
    {
        set
        {
            _presenter = value;
            _presenter.View = this;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ObjectFactory.BuildUp(this);
    }
}

//Solution #2

public partial class _Default : Page, IEmployeeView
{
    private EmployeePresenter _presenter;

    private EmployeePresenter Presenter
    {
        get
        {
            if (_presenter == null)
            {
                _presenter = ObjectFactory.GetInstance&amp;lt;EmployeePresenter&amp;gt;();
                _presenter.View = this;
            }

            return _presenter;
        }
    }
}

&lt;/pre&gt;</content>
    <author>
      <name>dotnetchris</name>
      <email>chris@marisic.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/736-presenter-injection-in-model-view-presenter-pattern-with-structuremap" rel="alternate"/>
  </entry>
</feed>

