<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://consultingblogs.emc.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Simon Brown's Blog</title><link>http://consultingblogs.emc.com/simonbrown/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2.1 SP3 (Build: 20423.1)</generator><item><title>Using Castle Windsor to manage NHibernate sessions</title><link>http://consultingblogs.emc.com/simonbrown/archive/2009/12/14/using-castle-windsor-to-manage-nhibernate-sessions.aspx</link><pubDate>Mon, 14 Dec 2009 13:36:00 GMT</pubDate><guid isPermaLink="false">e847c0e7-38d9-45c0-b593-56747303e088:16694</guid><dc:creator>simon.brown</dc:creator><slash:comments>0</slash:comments><comments>http://consultingblogs.emc.com/simonbrown/comments/16694.aspx</comments><wfw:commentRss>http://consultingblogs.emc.com/simonbrown/commentrss.aspx?PostID=16694</wfw:commentRss><description>&lt;p&gt;This post follows on from my previous blog post about a &lt;a href="http://consultingblogs.emc.com/simonbrown/archive/2009/10/27/castle-windsor-custom-wcf-lifestyle.aspx" target="_blank"&gt;custom Castle Windsor lifestyle for WCF&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I was developing an application which consumed a WCF service which handled all database interactions using NHibernate. The WCF service consumed some business services, mapped the resulting domain entities to data contracts and returned them to the consuming application.&amp;#160; The architecture is summarised in the following diagram:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://consultingblogs.emc.com/blogs/simonbrown/Architecture_1219C4A4.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="Architecture" border="0" alt="Architecture" src="http://consultingblogs.emc.com/blogs/simonbrown/Architecture_thumb_020D869E.jpg" width="428" height="291" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;There were two main technical requirements I needed to address around connecting to the database:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Re-use the same NHibernate session (and therefore database connection) within the same WCF service method call.&lt;/li&gt;    &lt;li&gt;Ensure that the NHibernate session (and therefore database connection)&amp;#160; was closed when the WCF service method call was completed (after all lazy loading had taken place)&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The domain entities in the application implemented lazy loading, so I couldn’t close the database connection in the NHibernate repository or business service, it needed to be closed later after the WCF Service had mapped the domain entities to data contracts. &lt;/p&gt;  &lt;p&gt;To solve this problem I decided to extend the Castle Windsor custom lifestyle I had created previously.&amp;#160; I created a new custom lifestyle &lt;strong&gt;PerNHibernateWcfRequestLifestyleManager&lt;/strong&gt; which inherited from my WCF lifestyle.&amp;#160; This would ensure that I would always retrieve the same NHibernate session from the container within the context of a WCF operation call.&amp;#160; It would also allow me to close the NHibernate session when the WCF operation was complete by hooking into the OperationCompleted event of System.ServiceModel.OperationContext.Current.&lt;/p&gt;  &lt;p&gt;The code for the lifestyle is:&lt;/p&gt;  &lt;div style="border-bottom:gray 1px solid;border-left:gray 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;padding-top:4px;"&gt;   &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, 'Courier New', courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// A custom Castle Windsor IoC lifestyle manager which stores the resolved object in the &lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// current OperationContext, therefore giving you a new instance of the requested &lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// component for each WCF request. It also closes the NHibernate session when the &lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// request is completed.&lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PerNHibernateWcfRequestLifestyleManager : PerWcfRequestLifestyleManager
{
    &lt;span style="color:#cc6633;"&gt;#region&lt;/span&gt; AbstractLifestyleManager Members

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Resolves the specified context.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;param name=&amp;quot;context&amp;quot;&amp;gt;The context.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;returns&amp;gt;The resolved object.&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; Resolve(CreationContext context)
    {
        &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; resolvedObject = &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.Resolve(context);
 
        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (OperationContext.Current != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)
        {
            OperationContext.Current.OperationCompleted += OperationContextCompleted;
        }
 
        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; resolvedObject;
    }

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Releases unmanaged and - optionally - managed resources&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Dispose()
    {
    }

    &lt;span style="color:#cc6633;"&gt;#endregion&lt;/span&gt;

    &lt;span style="color:#cc6633;"&gt;#region&lt;/span&gt; Event Handlers

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Closes the NHibernate session for this OperationContext when the operation has completed.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Closing the session also disconnects the database connection.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;param name=&amp;quot;sender&amp;quot;&amp;gt;The sender.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;param name=&amp;quot;e&amp;quot;&amp;gt;The &amp;lt;see cref=&amp;quot;System.EventArgs&amp;quot;/&amp;gt; instance containing the event data.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; OperationContextCompleted(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; sender, EventArgs e)
    {
        var sessionStore = ServiceLocator.Resolve&amp;lt;INHibernateSessionStore&amp;gt;();

        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(sessionStore != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)
        {
            var sessionFactory = ServiceLocator.Resolve&amp;lt;INHibernateSessionFactory&amp;gt;();
            sessionFactory.Close(sessionStore.CurrentSession);
        }
    }

    &lt;span style="color:#cc6633;"&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The Castle Windsor configuration entries looked like this:&lt;/p&gt;

&lt;div style="border-bottom:gray 1px solid;border-left:gray 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;padding-top:4px;"&gt;
  &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, 'Courier New', courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&amp;lt;!-- NHibernate Session Store --&amp;gt;
&amp;lt;component id=&lt;span style="color:#006080;"&gt;&amp;quot;NHibernateSessionStore&amp;quot;&lt;/span&gt;
           service=&lt;span style="color:#006080;"&gt;&amp;quot;Data.INHibernateSessionStore, Data&amp;quot;&lt;/span&gt;
           type=&lt;span style="color:#006080;"&gt;&amp;quot;Data.NHibernateSessionStore, Data&amp;quot;&lt;/span&gt;
           lifestyle=&lt;span style="color:#006080;"&gt;&amp;quot;custom&amp;quot;&lt;/span&gt;
           customLifestyleType=&lt;span style="color:#006080;"&gt;&amp;quot;Container.Lifestyles.PerNHibernateWcfRequestLifestyleManager, Container.Lifestyles&amp;quot;&lt;/span&gt;&amp;gt;
&amp;lt;/component&amp;gt;

&amp;lt;!-- NHibernate Session Factory --&amp;gt;
&amp;lt;component id=&lt;span style="color:#006080;"&gt;&amp;quot;NHibernateSessionFactory&amp;quot;&lt;/span&gt;
           service=&lt;span style="color:#006080;"&gt;&amp;quot;Data.INHibernateSessionFactory, Data&amp;quot;&lt;/span&gt;
           type=&lt;span style="color:#006080;"&gt;&amp;quot;Data.NHibernateSessionFactory, Data&amp;quot;&lt;/span&gt;
           lifestyle=&lt;span style="color:#006080;"&gt;&amp;quot;singleton&amp;quot;&lt;/span&gt;&amp;gt;
&amp;lt;/component&amp;gt;

&amp;lt;!-- Repositories --&amp;gt;
&amp;lt;component id=&lt;span style="color:#006080;"&gt;&amp;quot;NHibernateRepository&amp;quot;&lt;/span&gt;
           service=&lt;span style="color:#006080;"&gt;&amp;quot;Data.IRepository, Data&amp;quot;&lt;/span&gt;
           type=&lt;span style="color:#006080;"&gt;&amp;quot;Data.NHibernate.NHibernateRepository, Data&amp;quot;&lt;/span&gt;&amp;gt;
  &amp;lt;parameters&amp;gt;
    &amp;lt;sessionFactory&amp;gt;${NHibernateSessionFactory}&amp;lt;/sessionFactory&amp;gt;
  &amp;lt;/parameters&amp;gt;
&amp;lt;/component&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The NHibernateRepository asks the NHibernateSessionFactory to give it a session which it then consumes.&amp;#160; The NHibernateSessionFactory uses the NHibernateSessionStore to store its sessions. An example piece of code from the NHibernateRepository is included below:&lt;/p&gt;

&lt;div style="border-bottom:gray 1px solid;border-left:gray 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;padding-top:4px;"&gt;
  &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, 'Courier New', courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;var session = sessionFactory.Get();
var entity = session.Get&amp;lt;T&amp;gt;(id);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;For completeness, I’ve included the relevant source code from the NHibernateSessionFactory and NHibernateSessionStore classes below:&lt;/p&gt;

&lt;div style="border-bottom:gray 1px solid;border-left:gray 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;padding-top:4px;"&gt;
  &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, 'Courier New', courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// Manages the creation and closure of NHibernate sessions.&lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateSessionFactory : INHibernateSessionFactory
{
    &lt;span style="color:#cc6633;"&gt;#region&lt;/span&gt; Implementation of INHibernateSessionFactory

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Gets a new NHibernate session.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;returns&amp;gt;A new NHibernate session.&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ISession Get()
    {
        var sessionStore = ServiceLocator.Resolve&amp;lt;INHibernateSessionStore&amp;gt;();
        ISession session = sessionStore.CurrentSession;

        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(session == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)
        {
            session = &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.CreateNewSession();
            sessionStore.CurrentSession = session;
        }

        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session;
    }

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Closes the specified session.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;param name=&amp;quot;session&amp;quot;&amp;gt;The session.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Close(ISession session)
    {
        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (session != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt; &amp;amp;&amp;amp;
            session.IsOpen)
        {
            session.Close();
        }
    }

    &lt;span style="color:#cc6633;"&gt;#endregion&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISession CreateNewSession()
    {
        &lt;span style="color:#008000;"&gt;// Implementation goes here&lt;/span&gt;
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;div style="border-bottom:gray 1px solid;border-left:gray 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;padding-top:4px;"&gt;
  &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, 'Courier New', courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// Container for storing an NHibernate session.&lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateSessionStore : INHibernateSessionStore
{
    &lt;span style="color:#cc6633;"&gt;#region&lt;/span&gt; Implementation of INHibernateSessionStore

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Gets or sets the current session.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;value&amp;gt;The current session.&amp;lt;/value&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ISession CurrentSession { get; set; }

    &lt;span style="color:#cc6633;"&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://consultingblogs.emc.com/aggbug.aspx?PostID=16694" width="1" height="1"&gt;</description><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/IoC/default.aspx">IoC</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Castle+Windsor/default.aspx">Castle Windsor</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Custom+Lifestyle/default.aspx">Custom Lifestyle</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/WCF/default.aspx">WCF</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Session/default.aspx">Session</category></item><item><title>Castle Windsor Custom WCF Lifestyle</title><link>http://consultingblogs.emc.com/simonbrown/archive/2009/10/27/castle-windsor-custom-wcf-lifestyle.aspx</link><pubDate>Tue, 27 Oct 2009 14:43:13 GMT</pubDate><guid isPermaLink="false">e847c0e7-38d9-45c0-b593-56747303e088:16457</guid><dc:creator>simon.brown</dc:creator><slash:comments>1</slash:comments><comments>http://consultingblogs.emc.com/simonbrown/comments/16457.aspx</comments><wfw:commentRss>http://consultingblogs.emc.com/simonbrown/commentrss.aspx?PostID=16457</wfw:commentRss><description>&lt;p&gt;It’s been a while since my last post and I’ve been meaning to blog about this for ages now.&lt;/p&gt;  &lt;p&gt;Recently, I’ve been creating a lot of WCF services and using Castle Windsor for managing dependencies, and I came across a scenario which required the same object instance to be returned from the container at any point it was requested during the same WCF method call.&lt;/p&gt;  &lt;p&gt;Seeing as I was already using Castle Windsor to manage my dependencies it seemed appropriate to create a custom Windsor lifestyle for this purpose.&lt;/p&gt;  &lt;p&gt;First I had to work out how to hook into the WCF context.&amp;#160; Luckily in the System.ServiceModel namespace there is a class provided just for this purpose, OperationContext.&amp;#160; OperationContext has a Current property which gives you access to the execution context for the current thread. The OperationContext also contains an Extensions collection which allows you to add custom data to the current context. To create your own extension you need to create a class which implements the IExtension&amp;lt;OperationContext&amp;gt; interface.&amp;#160; I created an extension called WcfContext which contained a dictionary of objects to store in the context:&lt;/p&gt;  &lt;div style="border-bottom:gray 1px solid;border-left:gray 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;padding-top:4px;"&gt;   &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, 'Courier New', courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// ServiceModel.OperationContext extension for managing items.&lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; WcfContext : IExtension&amp;lt;OperationContext&amp;gt;
{
    &lt;span style="color:#cc6633;"&gt;#region&lt;/span&gt; Properties

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Gets or sets the items.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;value&amp;gt;The items.&amp;lt;/value&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; IDictionary&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt;&amp;gt; Items { get; set; }

    &lt;span style="color:#cc6633;"&gt;#endregion&lt;/span&gt;

    &lt;span style="color:#cc6633;"&gt;#region&lt;/span&gt; Implementation of IExtension&amp;lt;OperationContext&amp;gt;

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Enables an extension object to find out when it has been aggregated. Called when the extension is added to the &amp;lt;see cref=&amp;quot;P:System.ServiceModel.IExtensibleObject`1.Extensions&amp;quot; /&amp;gt; property.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;param name=&amp;quot;owner&amp;quot;&amp;gt;The extensible object that aggregates this extension.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Attach(OperationContext owner)
    {
        Items = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt;&amp;gt;();
    }

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Enables an object to find out when it is no longer aggregated. Called when an extension is removed from the &amp;lt;see cref=&amp;quot;P:System.ServiceModel.IExtensibleObject`1.Extensions&amp;quot; /&amp;gt; property.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;param name=&amp;quot;owner&amp;quot;&amp;gt;The extensible object that aggregates this extension.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Detach(OperationContext owner)
    {
    }

    &lt;span style="color:#cc6633;"&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Next I had to create the custom Windsor lifestyle.&amp;#160; This is achieved by creating a class which inherits from Castle.MicroKernel.Lifestyle.AbstractLifestyleManager (for more information on this check out the Castle Windsor site &lt;a href="http://www.castleproject.org/container/documentation/trunk/usersguide/lifestyles.html" target="_blank"&gt;here&lt;/a&gt;). Then it was just a case of checking the current OperationContext, adding my WcfContext extension if it wasn’t already present, resolving the component, then adding the component to the WcfContext.Items dictionary.&amp;#160; The next time the lifestyle is used within the same OperationContext the component is retrieved from the OperationContext instead of being resolved again by Windsor.&amp;#160; Here’s the code:&lt;/p&gt;

&lt;div style="border-bottom:gray 1px solid;border-left:gray 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;padding-top:4px;"&gt;
  &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, 'Courier New', courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// A custom Castle Windsor IoC lifestyle manager which stores the resolved object in the &lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// current OperationContext, therefore giving you a new instance of the requested &lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// component for each WCF request. &lt;/span&gt;
&lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PerWcfRequestLifestyleManager : AbstractLifestyleManager
{
    &lt;span style="color:#cc6633;"&gt;#region&lt;/span&gt; AbstractLifestyleManager Members

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Resolves the specified context.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;param name=&amp;quot;context&amp;quot;&amp;gt;The context.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;returns&amp;gt;The resolved object.&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; Resolve(CreationContext context)
    {
        &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; resolvedObject;

        &lt;span style="color:#008000;"&gt;// Get the type of the IoC component being resolved to use as the key for the OperationContext item. &lt;/span&gt;
        var contextKey = GetContextKeyForComponent(context);

        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (OperationContext.Current != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)
        {
            var itemStorage = &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.GetWcfContext();

            &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(itemStorage == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)
            {
                OperationContext.Current.Extensions.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; WcfContext());
                itemStorage = &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.GetWcfContext();
            }

            &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!itemStorage.Items.ContainsKey(contextKey))
            {
                resolvedObject = &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.Resolve(context);
                itemStorage.Items[contextKey] = resolvedObject;
            }
            &lt;span style="color:#0000ff;"&gt;else&lt;/span&gt;
            {
                resolvedObject = itemStorage.Items[contextKey];
            }
        }
        &lt;span style="color:#0000ff;"&gt;else&lt;/span&gt;
        {
            resolvedObject = &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.Resolve(context);
        }
 
        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; resolvedObject;
    }

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Releases unmanaged and - optionally - managed resources&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Dispose()
    {
    }

    &lt;span style="color:#cc6633;"&gt;#endregion&lt;/span&gt;

    &lt;span style="color:#cc6633;"&gt;#region&lt;/span&gt; Protected Methods

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Gets the context key for the component being resolved.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;param name=&amp;quot;context&amp;quot;&amp;gt;The context.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;returns&amp;gt;The context key for the component being resolved.&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; GetContextKeyForComponent(CreationContext context)
    {
        &lt;span style="color:#008000;"&gt;// Default the key to be the full type name of the component being resolved.&lt;/span&gt;
        &lt;span style="color:#008000;"&gt;// NB: This will be the same for each dependency (e.g. constructor parameter) &lt;/span&gt;
        &lt;span style="color:#008000;"&gt;//     being resolved for the component.&lt;/span&gt;
        &lt;span style="color:#008000;"&gt;//     The context.Handler is null when resolving a constructor parameter which is a &amp;lt;list&amp;gt;.&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; componentKey = context.Handler != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt; 
                                  ? context.Handler.ComponentModel.Implementation.FullName
                                  : Guid.NewGuid().ToString();

        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (context.Dependencies.Count &amp;gt; 0)
        {
            &lt;span style="color:#008000;"&gt;// This is actually a component dependency which is being resolved.&lt;/span&gt;
            &lt;span style="color:#008000;"&gt;// The key of the last dependency is the one which is being resolved.&lt;/span&gt;
            IEnumerator enumerator = context.Dependencies.GetEnumerator();
            &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; i = 0;
            &lt;span style="color:#0000ff;"&gt;while&lt;/span&gt; (enumerator.MoveNext())
            {
                i++;
                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (i == context.Dependencies.Count)
                {
                    componentKey = ((DependencyModel)enumerator.Current).DependencyKey;
                }
            }
        }

        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; componentKey;
    }

    &lt;span style="color:#008000;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// Gets the WCF context from the OperationContext.&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;/// &amp;lt;returns&amp;gt;The WCF context.&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; WcfContext GetWcfContext()
    {
        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; OperationContext.Current.Extensions.FirstOrDefault(x =&amp;gt; x.GetType() == &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(WcfContext)) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; WcfContext;
    }

    &lt;span style="color:#cc6633;"&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I had a few issues generating a unique context key to use as the index to the WcfContext.Items collection but the solution I came up with seems to work for all of the scenarios I have encountered (see method &lt;strong&gt;GetContextKeyForComponent&lt;/strong&gt; in the code snippet above).&amp;#160; Essentially it uses the component name unless a component dependency (constructor parameter or property) is being resolved in which case the dependency name is used. You may need to amend this to account for other edge cases I have not encountered.&lt;/p&gt;

&lt;p&gt;The custom lifestyle is attached to the relevant component via the Castle Windsor configuration file as follows:&lt;/p&gt;

&lt;div style="border-bottom:gray 1px solid;border-left:gray 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;padding-top:4px;"&gt;
  &lt;pre style="border-bottom-style:none;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:consolas, 'Courier New', courier, monospace;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&amp;lt;component id=&lt;span style="color:#006080;"&gt;&amp;quot;MyClass&amp;quot;&lt;/span&gt;
           service=&lt;span style="color:#006080;"&gt;&amp;quot;Data.IMyInterface, Data&amp;quot;&lt;/span&gt;
           type=&lt;span style="color:#006080;"&gt;&amp;quot;Data.MyClass, Data&amp;quot;&lt;/span&gt;
           lifestyle=&lt;span style="color:#006080;"&gt;&amp;quot;custom&amp;quot;&lt;/span&gt;
           customLifestyleType=&lt;span style="color:#006080;"&gt;&amp;quot;Container.Lifestyles.PerWcfRequestLifestyleManager, Container.Lifestyles&amp;quot;&lt;/span&gt;&amp;gt;
&amp;lt;/component&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;I hope somebody else finds this useful. I’ve used it on a few different projects now and it’s working well for me.&lt;/p&gt;&lt;img src="http://consultingblogs.emc.com/aggbug.aspx?PostID=16457" width="1" height="1"&gt;</description><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/IoC/default.aspx">IoC</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Castle+Windsor/default.aspx">Castle Windsor</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Custom+Lifestyle/default.aspx">Custom Lifestyle</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/WCF/default.aspx">WCF</category></item><item><title>VMWare AddIn Crashes Visual Studio</title><link>http://consultingblogs.emc.com/simonbrown/archive/2008/12/03/vmware-addin-crashes-visual-studio.aspx</link><pubDate>Wed, 03 Dec 2008 10:39:00 GMT</pubDate><guid isPermaLink="false">e847c0e7-38d9-45c0-b593-56747303e088:13506</guid><dc:creator>simon.brown</dc:creator><slash:comments>1</slash:comments><comments>http://consultingblogs.emc.com/simonbrown/comments/13506.aspx</comments><wfw:commentRss>http://consultingblogs.emc.com/simonbrown/commentrss.aspx?PostID=13506</wfw:commentRss><description>&lt;p&gt;Just a note of caution to anybody out there who is installing VMWare 
Workstation on a PC with Visual Studio installed.&lt;/p&gt;
&lt;p&gt;Part of the VMWare install is to add the VMDebugger add-in to Visual 
Studio.&lt;/p&gt;
&lt;p&gt;On my machine I was able to open Visual Studio but as soon as I tried to open 
or create a new project Visual Studio would crash.&lt;/p&gt;
&lt;p&gt;I narrowed the issue down to the VMDebugger add-in.&lt;/p&gt;
&lt;p&gt;To rectify the issue, go to Tools | Add-in Manager in Visual Studio and 
uncheck the VMDebugger add-in and Startup checkboxes.&lt;/p&gt;
&lt;p&gt;It should look like this:&lt;/p&gt;&lt;img src="http://blogs.conchango.com/photos/conchango_bloggers/images/13508/original.aspx" title="Visual Studio 2008 Add-In Manager" alt="Visual Studio 2008 Add-In Manager" height="360" width="542"&gt;&lt;br&gt;&lt;p&gt;I found this to be an issue in both Visual Studio 2005 and 2008.&lt;/p&gt;&lt;img src="http://consultingblogs.emc.com/aggbug.aspx?PostID=13506" width="1" height="1"&gt;</description><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Crash/default.aspx">Crash</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/VMWare/default.aspx">VMWare</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/VMDebugger/default.aspx">VMDebugger</category></item><item><title>MSDN Roadshow Re-Run</title><link>http://consultingblogs.emc.com/simonbrown/archive/2008/11/08/msdn-roadshow-re-run.aspx</link><pubDate>Sat, 08 Nov 2008 23:41:00 GMT</pubDate><guid isPermaLink="false">e847c0e7-38d9-45c0-b593-56747303e088:13145</guid><dc:creator>simon.brown</dc:creator><slash:comments>1</slash:comments><comments>http://consultingblogs.emc.com/simonbrown/comments/13145.aspx</comments><wfw:commentRss>http://consultingblogs.emc.com/simonbrown/commentrss.aspx?PostID=13145</wfw:commentRss><description>&lt;p&gt;I recently attended a re-run of the &lt;a href="http://blogs.msdn.com/ukdevevents/" target="_blank"&gt;MSDN Roadshow&lt;/a&gt; at Microsoft's office in Victoria, London.&lt;/p&gt;  &lt;p&gt;It was an overview of the latest offerings from Microsoft in the development space.&lt;/p&gt;  &lt;p&gt;The agenda was as follows:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ADO.Next - Entity Framework and ADO.NET Data Services - &lt;a href="http://blogs.msdn.com/ericnel/" target="_blank"&gt;Eric Nelson&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;ASP.Next - ASP.NET 3.5 SP1 and MVC - &lt;a href="http://blogs.msdn.com/mikeormond/" target="_blank"&gt;Mike Ormond&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;Silverlight V2 - &lt;a href="http://mtaulty.com/communityserver/blogs/mike_taultys_blog/default.aspx" target="_blank"&gt;Mike Taulty&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;Visual Studio Team Suite - &lt;a href="http://blogs.msdn.com/neilkidd/default.aspx" target="_blank"&gt;Neil Kidd&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The most interesting bit for me was the first session on the &lt;a href="http://msdn.microsoft.com/en-us/library/aa697427%28VS.80%29.aspx" target="_blank"&gt;Entity Framework&lt;/a&gt; and ADO.NET Data Services.&amp;nbsp; Entity Framework is an Object Relational Mapper (ORM) and Microsoft's answer to the hugely popular &lt;a href="http://www.nhibernate.org" target="_blank"&gt;nHibernate&lt;/a&gt;.&amp;nbsp; The Entity Framework still has a few quirks and is lacking in certain areas, and Microsoft seem to acknowledge this fact to their credit. It definitely seemed like a move in the right direction though with some pretty neat features.&lt;/p&gt;  &lt;p&gt;As you would expect, Microsoft have included a wizard in Visual Studio (Entity Data Model Wizard) which you can point to a database, select the tables you're interested in, and it will generate your Entity Data Model for you.&amp;nbsp; The data model consists of three different layers, each having it's own XML file and generated classes as follows:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Conceptual layer - The business entity model which is defined in an XML file using the Conceptual Schema Definition Language (CSDL) &lt;/li&gt;    &lt;li&gt;Storage layer - A representation of the database schema which is defined in an XML file using the Store Schema Definition Language (SSDL)&lt;/li&gt;    &lt;li&gt;Mapping layer - The mapping between the Conceptual and Storage layers which is defined in an XML file using the Mapping Schema Language (MSL)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;An entity class is generated for each table in the database and also handles relationships by for example adding an Orders property (collection of Order objects) on the Customer class.&amp;nbsp; Once the Entity Data Model has been generated it can then be manipulated by changing the XML files or the entity classes.&lt;/p&gt;  &lt;p&gt;The Conceptual layer of the data model is queried by the developer using either Entity SQL (T-SQL like syntax for querying entities) or Linq (Linq for Entities). I can't imagine that anybody is going to be using Entity SQL when they can use Linq with it's expressive fluent interface.&amp;nbsp; Eric recommended a tool to assist with writing Linq queries called &lt;a href="http://www.linqpad.net/" target="_blank"&gt;LinqPad&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The Entity Framework also handles updates to the database including features such as Unit of Work and Identity Tracking (handling dirty data - updates which have occurred since the last database read) and Lazy Loading (this is possible, but not out-of-the-box, Eager Loading is the default mode).&lt;/p&gt;  &lt;p&gt;I'm not sure whether Microsoft have done enough to convince nHibernate users to convert to using Entity Framework.&lt;/p&gt;  &lt;p&gt;When Eric then went on to talk about ADO.NET Data Services that's when I started to see some real benefit to using the Entity Framework instead of nHibernate.&amp;nbsp; Eric demonstrated the creation of a fully RESTful service with only a couple of lines of code.&amp;nbsp; You can essentially just create a service which inherits from your Entity Data Model and "Hey Presto!" you have a RESTful service covering all CRUD operations throughout your entire data model!&amp;nbsp; No need to create services anymore with methods like GetProductsById() or GetProductsByUserIdSortedByDate() or AddProduct() for each entity. This is all handled by ADO.NET Data Services.&amp;nbsp; Very impressive stuff indeed!&lt;/p&gt;  &lt;p&gt;I was even more impressed when Mike Ormond went on to talk about &lt;a href="http://www.asp.net/dynamicdata/" target="_blank"&gt;ASP.NET Dynamic Data&lt;/a&gt;.&amp;nbsp; You can register your Entity Framework Data Model with Dynamic Data and a fully functional CRUD website with friendly urls is created for you using data driven page templates.&amp;nbsp; RequiredFieldValidators are added for non-nullable fields, entry is limited to the number of characters the field can hold in the database, different controls are rendered based on the type of the field (e.g. check boxes for boolean values). This would be great for getting a fully functional site up and running really quickly, maybe for a prototype, or for a back end administration or simple Call Centre application.&lt;/p&gt;  &lt;p&gt;To me this is where Microsoft really starts to excel.&amp;nbsp; Their development teams seem to be working towards a common goal, with each product being built on a common framework. They develop functionality which can be utilised across their range of products.&amp;nbsp; They make it easy for developers to get up and running really quickly and concentrate on delivering real business value, rather than writing boilerplate code. My only worry with this approach is that it's fine for simple applications but when things become more complex you start to hit problems.&amp;nbsp; I'll have to give Microsoft the benefit of the doubt on that one as I haven't started to use any of this in anger yet!&lt;/p&gt;  &lt;p&gt;There were some other interesting features demonstrated by Mike Ormond such as the AJAX history control, a preview of ASP.NET MVC and it's routing engine (which is also used for DynamicData), and the Silverlight ASP.NET controls &amp;lt;asp:Silverlight&amp;gt; and &amp;lt;asp:MediaPlayer&amp;gt;.&lt;/p&gt;  &lt;p&gt;Mike Taulty discussed Silverlight 2 and Deep Zoom, and developed some Media Player Silverlight applications.&amp;nbsp; He also showed us some good examples of Silverlight/Deep Zoom applications (&lt;a href="http://memorabilia.hardrock.com/" target="_blank"&gt;Hard Rock Cafe&lt;/a&gt;, Media Toaster and &lt;a href="http://www.microsoft.com/beta/downloads/" target="_blank"&gt;Microsoft Downloads&lt;/a&gt;).&amp;nbsp; He talked about the use of the HTMLPage and HTMLElement classes which can be used to manipulate the web page DOM from within a Silverlight application, retrieving data using limited SOAP or JSON services, and the storage of data on the client's machine using Isolated Storage.&lt;/p&gt;  &lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;"&gt;   &lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;"&gt;IsolatedStorageFile.GetUserStoreForApplication();&lt;br&gt;IsolatedStorageSettings.ApplicationSettings[&lt;span&gt;"SettingName"&lt;/span&gt;];&lt;br&gt;IsolatedStorageSettings.ApplicationSettings.Save();&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Neil Kidd then went on to give a demonstration of the Visual Studio Team Suite.&amp;nbsp; This was mainly a demonstration of the test tools which have been around for a while now but also Database Professional (previously Data Dude) which was hugely impressive.&amp;nbsp; Database Professional creates a separate file for each artifact (table, index, constraint, stored procedure etc) and contains impressive refactoring capabilities.&amp;nbsp; You can change the name of a field in a table and it will automatically change the name in each of the places this is used (stored procedures, constraints etc). You can use the tool to compare the database project to an existing instance of the database and it will create update scripts for you to deploy changes. Neil also demonstrated writing database unit tests for testing stored procedures. DBA's have no excuses now!&amp;nbsp; Database Professional also has an impressive tool for generating large amounts of varied test data based on rules you can specify.&amp;nbsp; Very useful for performance testing.&amp;nbsp; All-in-all a very nice looking product.&lt;/p&gt;

&lt;p&gt;So, to conclude it was a very good event, with some very impressive new technology demonstrated, especially around Entity Framework, ADO.NET Data Services and ASP.NET Dynamic Data.&amp;nbsp; Lots more for me to look at in my quest for continuous improvement!&lt;/p&gt;&lt;img src="http://consultingblogs.emc.com/aggbug.aspx?PostID=13145" width="1" height="1"&gt;</description><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Team+Suite/default.aspx">Team Suite</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/LinqPad/default.aspx">LinqPad</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Dynamic+Data/default.aspx">Dynamic Data</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Linq/default.aspx">Linq</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Database+Professional/default.aspx">Database Professional</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/MSDN/default.aspx">MSDN</category></item><item><title>Castle Windsor - Fluent Interface</title><link>http://consultingblogs.emc.com/simonbrown/archive/2008/10/28/castle-windsor-fluent-interface.aspx</link><pubDate>Tue, 28 Oct 2008 11:47:00 GMT</pubDate><guid isPermaLink="false">e847c0e7-38d9-45c0-b593-56747303e088:12971</guid><dc:creator>simon.brown</dc:creator><slash:comments>0</slash:comments><comments>http://consultingblogs.emc.com/simonbrown/comments/12971.aspx</comments><wfw:commentRss>http://consultingblogs.emc.com/simonbrown/commentrss.aspx?PostID=12971</wfw:commentRss><description>&lt;p&gt;I attended a &lt;a href="http://skillsmatter.com/" title="Skills Matter" target="_blank"&gt;Skills Matter&lt;/a&gt; event recently on Castle Windsor.&amp;nbsp; The presenters of the session were &lt;a href="http://mikehadlow.blogspot.com/" target="_blank"&gt;Mike Hadlow&lt;/a&gt; and &lt;a href="http://gojko.net/" target="_blank"&gt;Gojko Adzic&lt;/a&gt;. Slides, code samples and screencasts of the event can be found on their blogs.&amp;nbsp; Mike's session was a very good introduction to dependency injection with IoC and Gojko tackled some of the more advanced features of Windsor such as facilities.&lt;/p&gt;  &lt;p&gt;I've been using Castle Windsor for a while now on my current project favouring the use of a configuration file rather than registering components using code.&lt;/p&gt;  &lt;p&gt;However, those of you who prefer registering your components in code will be pleased to find out that Castle have added a fluent interface to their API for Windsor.&amp;nbsp; I've included an example below:&lt;/p&gt;  &lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;"&gt;   &lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;"&gt;var container = &lt;span&gt;new&lt;/span&gt; WindsorContainer();&lt;br&gt;container.Register(Component.For&amp;lt;ICatalogService&amp;gt;().ImplementedBy&amp;lt;MyCatalogService&amp;gt;().Lifestyle.Singleton);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The fluent interface is only available in the trunk version, so you'll need to download the source and compile it yourself.&lt;/p&gt;

&lt;p&gt;I've been injecting all of my dependencies using the constructor but Mike also pointed out that Windsor also supports injecting dependencies using a property setter. This is useful if the dependency is optional. The configuration required to achieve this is the same as for constructor injection.&amp;nbsp; In the following example catalogRepository is a constructor parameter and Logger is a property on the MyCatalogService class.&lt;/p&gt;

&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;"&gt;
  &lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;"&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;container&lt;/span&gt;  &lt;span&gt;id&lt;/span&gt;&lt;span&gt;="MyCatalogService"&lt;/span&gt; &lt;br&gt;            &lt;span&gt;service&lt;/span&gt;&lt;span&gt;="MyNamespace.ICatalogService, MyAssembly"&lt;/span&gt; &lt;br&gt;            &lt;span&gt;type&lt;/span&gt;&lt;span&gt;="MyNamespace.MyCatalogService, MyAssembly"&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;    &lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;parameters&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;        &lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;catalogRepository&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;${MyCatalogRepository}&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;catalogRepository&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;        &lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;Logger&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;${MyLogger}&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;Logger&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;    &lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;parameters&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;container&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Something else mentioned was Windsor's support for generics.&amp;nbsp; This is something I've had to tackle recently on my own project and is especially useful for mapping classes. The following is an example of how register a class (MyProductTypeMapper) which inherits from interface IMapper&amp;lt;Input, Output&amp;gt;.&lt;/p&gt;

&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;"&gt;
  &lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;"&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;interface&lt;/span&gt; IMapper&amp;lt;Input, Output&amp;gt;&lt;br&gt;{&lt;br&gt;    Output Map(Input input);&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; MyProductTypeMapper : IMapper&amp;lt;DBProductType, EntityProductType&amp;gt;&lt;br&gt;{&lt;br&gt;    &lt;span&gt;public&lt;/span&gt; EntityProductType Map(DBProductType input)&lt;br&gt;    {&lt;br&gt;        ...&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;"&gt;
  &lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;"&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;container&lt;/span&gt;  &lt;span&gt;id&lt;/span&gt;&lt;span&gt;="MyProductTypeMapper"&lt;/span&gt; &lt;br&gt;            &lt;span&gt;service&lt;/span&gt;&lt;span&gt;="MyNamespace.IMapper`2[[MyNamespace.DBProductType, MyAssembly],[MyNamespace.EntityProductType, MyAssembly]], MyAssembly"&lt;/span&gt; &lt;br&gt;            &lt;span&gt;type&lt;/span&gt;&lt;span&gt;="MyNamespace.MyProductTypeMapper, MyAssembly"&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;container&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Mike also mentioned the new &lt;a href="http://www.codeplex.com/CommonServiceLocator" target="_blank"&gt;Common Service Locator&lt;/a&gt; library which is available on Codeplex.&amp;nbsp; It is an abstraction around your IoC container of choice (Castle Windsor, Spring, Unity, StructureMap etc).&amp;nbsp; &lt;/p&gt;

&lt;p&gt;Also covered at the event was the use of Castle Windsor facilities.&amp;nbsp; Castle provide a set of facilities out of the box including Logging, FactorySupport and NHibernate.&amp;nbsp; There is also a &lt;a href="http://using.castleproject.org/display/Contrib/Castle.Facilities.MethodValidator" target="_blank"&gt;MethodValidator&lt;/a&gt; facility available which allows you to add Design by Contract style functionality into your applications.&amp;nbsp; An example was given using the &lt;a href="http://castleproject.org/container/facilities/trunk/startable/index.html" target="_blank"&gt;StartableFacility&lt;/a&gt; which can be used to add standard functionality before and after each method call.&amp;nbsp; This could be used for instance to log the entry and exit of each method. It is also possible to create custom facilities.&lt;/p&gt;

&lt;p&gt;This is just a subset of the areas covered in the session.&amp;nbsp; If you're interested in Castle Windsor then I'd urge you to watch the &lt;a href="http://gojko.net/2008/11/04/dependency-injection-with-castle-windsor-video/" target="_blank"&gt;screencast&lt;/a&gt; of the session on Gojko's blog.&lt;/p&gt;&lt;img src="http://consultingblogs.emc.com/aggbug.aspx?PostID=12971" width="1" height="1"&gt;</description><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Setter+Injection/default.aspx">Setter Injection</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/MethodValidator/default.aspx">MethodValidator</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/StartableFacility/default.aspx">StartableFacility</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Common+Service+Locator/default.aspx">Common Service Locator</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/IoC/default.aspx">IoC</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Castle+Windsor/default.aspx">Castle Windsor</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Generics/default.aspx">Generics</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Fluent+Interface/default.aspx">Fluent Interface</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/Design+by+Contract/default.aspx">Design by Contract</category></item><item><title>Continuous Improvement</title><link>http://consultingblogs.emc.com/simonbrown/archive/2008/10/04/continuous-improvement.aspx</link><pubDate>Sat, 04 Oct 2008 14:09:00 GMT</pubDate><guid isPermaLink="false">e847c0e7-38d9-45c0-b593-56747303e088:12684</guid><dc:creator>simon.brown</dc:creator><slash:comments>1</slash:comments><comments>http://consultingblogs.emc.com/simonbrown/comments/12684.aspx</comments><wfw:commentRss>http://consultingblogs.emc.com/simonbrown/commentrss.aspx?PostID=12684</wfw:commentRss><description>Sometimes in life you really come down to earth with a bump!&lt;br&gt;&lt;br&gt;This is what happened to me on the first day of J.P. Boodhoo's recent &lt;a href="http://blog.jpboodhoo.com/NothinButNetDuesseldorfRecap.aspx"&gt;Nothin But .NET bootcamp in Dusseldorf&lt;/a&gt;.&lt;br&gt;&lt;br&gt;I like to think that I've been developing applications in the right way.&amp;nbsp; I've been trying to follow good design principles, using design patterns and following best practice agile processes such as automated unit testing.&amp;nbsp; Not only that, but trying to encourage others to do the same and championing some of these processes within my project.&amp;nbsp; I don't think I'm the best developer in the world but I do pride myself on developing quality solutions in a professional manner.&amp;nbsp; I try to keep up with new trends, tools and techniques and new language features and products. &lt;br&gt;&lt;br&gt;This is however, a very fast moving industry and it doesn't take long to fall behind.&amp;nbsp; This was the first lesson I learnt on day 1 of JP's course.&lt;br&gt;&lt;br&gt;JP is a consummate professional.&amp;nbsp; He is probably the most capable developer I have ever met.&amp;nbsp; All of the course attendees were in awe of him, and these are no ordinary developers.&amp;nbsp; The attendees were all very talented developers too, some of the best in Europe. JP writes code at the speed of light, having complete command of the keyboard (he never uses a mouse).&amp;nbsp; In fact it seems that the only thing that's slowing him down is the speed of the virtual machine which is struggling to keep up with the ferocious battering of the keyboard.&amp;nbsp; This is not only due to the fact that JP is fast at typing but more due to the fact that he has configured his development environment for optimum performance, using any tools at his disposal and learning every aspect of them.&amp;nbsp; Resharper is his main tool of choice, as it is for many of us, but JP knows it inside out.&amp;nbsp; He has an encyclopedic knowledge of keyboard shortcuts, it's a joy to watch.&lt;br&gt;&lt;br&gt;Not only is he fast but the quality and design of the code is very high.&amp;nbsp; JP is very well read and has extensive knowledge of design patterns and OO design principles, but the impressive bit is that he knows exactly when to use them.&amp;nbsp; In the past I've worked with developers who have learnt a new pattern and then decided to use it everywhere like it's a silver bullet.&amp;nbsp; Not so with JP.&amp;nbsp; So what do we end up with?&amp;nbsp; A very well designed application.&amp;nbsp; Small classes containing small methods adhering to the Single Responsibility and Open Closed principles amongst others.&lt;br&gt;&lt;br&gt;The way JP works is also very efficient.&amp;nbsp; He takes a top down approach to development, starting with the UI, working down to the domain layer, introducing stubs as early as possible.&amp;nbsp; Taking this approach the client gets very early visibility to the UI and is able to see if it works for them.&amp;nbsp; Let's face it, a lot of the time the UI is the main part of the application that the client is interested in, so it makes sense to get this to them as early as possible.&amp;nbsp; And let me be clear here, we're not talking about a prototype UI, but production quality code.&amp;nbsp; Once the UI is finished, we can then move on to the Service and Domain layers.&amp;nbsp; Taking a top down approach also stops you from over engineering a solution and being too reliant on underlying legacy data structures.&lt;br&gt;&lt;br&gt;JP is also a vociferous proponent of Test Driven Design, or more specifically Behaviour Driven Design.&amp;nbsp; Not only does he "talk the talk" but he also "walks the walk".&amp;nbsp; JP never writes a line of production code without first writing a failing test, no excuses.&amp;nbsp; In the past I’ve worked with developers who are very resistant to concepts such as TDD and good design principles, always having an excuse for not doing it (haven’t got enough time, the managers/clients aren’t interested etc.).&amp;nbsp; This is not the case with JP. The tests are written using human readable English and fluent interfaces and constructed using interfaces and mocks meaning even the least technical of us can understand the functionality of the code we are testing.&amp;nbsp; Writing the tests in this way helps to flesh out the design.&lt;br&gt;&lt;br&gt;By now you're probably thinking that JP's bootcamp course is some kind of a cult, and this is the start of a big recruitment drive!&amp;nbsp; Well, you'd be wrong.&amp;nbsp; In fact one of the things that JP discourages is putting people on a pedestal.&amp;nbsp; It's not healthy and it holds you back.&amp;nbsp; Everybody has something to offer and improvements can always be made.&lt;br&gt;&lt;br&gt;I learnt an awful lot about C#, OO Design, patterns, and best practice development on the course, and got to spend the week with a fantastic group of talented developers.&amp;nbsp; However, the main thing I've learnt is that you can't rest on you laurels or past glories in this industry.&amp;nbsp; Every single one of us can improve.&amp;nbsp; We'll never know everything, we may never be a big shot in the industry, but that shouldn't stop us putting in the effort to improve ourselves.&lt;br&gt;&lt;br&gt;Attending JP's course was like a big kick up the backside for me.&amp;nbsp; That's enough talking for now.&amp;nbsp; I'm wasting valuable study time! &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;For a more detailed and humorous account of JP's Dusseldorf course, checkout Michel Grootjans blog at &lt;a href="http://geekswithblogs.net/alternativedotnet"&gt;http://geekswithblogs.net/alternativedotnet&lt;/a&gt;.&lt;br&gt;&lt;br&gt;Talking of humour, I said I'd dedicate my first blog post to my good friend Ciaran Roarty, a fellow developer and the hottest new act on the Glasgow comedy scene!&amp;nbsp; Check out his blog at &lt;a href="http://www.ciaranroarty.com"&gt;http://www.ciaranroarty.com&lt;/a&gt;. &lt;br&gt;&lt;img src="http://consultingblogs.emc.com/aggbug.aspx?PostID=12684" width="1" height="1"&gt;</description><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/TDD/default.aspx">TDD</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/bootcamp/default.aspx">bootcamp</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/BDD/default.aspx">BDD</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/J.P.+Boodhoo/default.aspx">J.P. Boodhoo</category><category domain="http://consultingblogs.emc.com/simonbrown/archive/tags/improvement/default.aspx">improvement</category></item></channel></rss>
