<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>rjarmstrong</title>
        <link>http://www.rjarmstrong.com/Default.aspx</link>
        <description>Design &amp; Architecture on the CLR</description>
        <language>en-GB</language>
        <copyright>rjarmstrong</copyright>
        <generator>Subtext Version 2.1.2.2</generator>
        <image>
            <title>rjarmstrong</title>
            <url>http://www.rjarmstrong.com/images/RSS2Image.gif</url>
            <link>http://www.rjarmstrong.com/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Your own NHibernate SessionFactory for Web applications</title>
            <category>NHibernate</category>
            <category>Castle Windsor</category>
            <link>http://www.rjarmstrong.com/archive/2009/10/21/your-own-nhibernate-sessionfactory-for-web-applications.aspx</link>
            <description>&lt;p&gt;The beauty of the &lt;a href="http://www.castleproject.org" target="_blank"&gt;Castle Windsor&lt;/a&gt; container is that it allows you the flexibility to do many things with a relatively small API without having to learn all the many pluggable components out there. In short, sometimes you don’t need to use contribs just because they are available, doing so increases your coupling and in turn reduces your flexibility – abstraction comes at a price. Case in point the &lt;a href="http://www.castleproject.org/container/facilities/trunk/nhibernate/webapp.html" target="_blank"&gt;NHibernate Facility&lt;/a&gt;, this allows you to create a session instance for each web request, the key benefit being that you can use lazy loading on your entities and take full advantage of the &lt;a href="http://martinfowler.com/eaaCatalog/unitOfWork.html" target="_blank"&gt;unit of work&lt;/a&gt; which NHibernate does so well. The point is you don’t need this facility, and I’ll show you how to create the same behaviour without it.     &lt;br /&gt;    &lt;br /&gt;These are the components you need to create: &lt;/p&gt;  &lt;h3&gt;1. Windsor Container implementation: a wrapper plus configuration. &lt;/h3&gt;  &lt;p&gt;This will be used for all your dependency injection needs, including supplying NHibernate sessions to services at runtime.  You will notice that I have named the NhibernateSessionOpener - “nhsessionopener”, you can find the component referring to this in the configuration file. &lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Container&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; IWindsorContainer _container;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; IWindsorContainer Instance&lt;br /&gt;    {&lt;br /&gt;        get&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_container == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;            {&lt;br /&gt;                _container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WindsorContainer(configuration);&lt;br /&gt;                _container.Register(Component.For&amp;lt;NhSessionOpener&amp;gt;()&lt;br /&gt;                    .Named(&lt;span style="color: #006080"&gt;"nhsessionopener"&lt;/span&gt;));&lt;br /&gt;            }&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _container;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;br /&gt;

&lt;h4&gt;Container Configuration:&lt;/h4&gt;

&lt;br /&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&amp;lt;?xml version=&lt;span style="color: #006080"&gt;"1.0"&lt;/span&gt; encoding=&lt;span style="color: #006080"&gt;"utf-8"&lt;/span&gt; ?&amp;gt;&lt;br /&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;facilities&amp;gt;&lt;br /&gt;        &amp;lt;facility id=&lt;span style="color: #006080"&gt;"factory.support"&lt;/span&gt; &lt;br /&gt;                  type=&lt;span style="color: #006080"&gt;"Castle.Facilities.FactorySupport.FactorySupportFacility, &lt;br /&gt;                  Castle.MicroKernel"&lt;/span&gt; /&amp;gt;&lt;br /&gt;    &amp;lt;/facilities&amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;components&amp;gt;&lt;br /&gt;        &amp;lt;component id=&lt;span style="color: #006080"&gt;"nhsessionimpl"&lt;/span&gt; lifestyle=&lt;span style="color: #006080"&gt;"PerWebRequest"&lt;/span&gt; &lt;br /&gt;                   type=&lt;span style="color: #006080"&gt;"NHibernate.Impl.SessionImpl, NHibernate"&lt;/span&gt; &lt;br /&gt;                   factoryId=&lt;span style="color: #006080"&gt;"nhsessionopener"&lt;/span&gt; &lt;br /&gt;                   factoryCreate=&lt;span style="color: #006080"&gt;"Open"&lt;/span&gt; /&amp;gt;&lt;br /&gt;        &lt;br /&gt;        &amp;lt;!-- Other components follow here --&amp;gt;&lt;br /&gt;    &amp;lt;/components&amp;gt;&lt;br /&gt;    &lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;br /&gt;

&lt;h3&gt;2. NHibernate SessionFactory wrapper&lt;/h3&gt;

&lt;p&gt;You only need one NHibernate SessionFactory which will be accessible from this wrapper instance.  &lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; NhibernateSessionFactory&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; ISessionFactory instance;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; ISessionFactory Instance&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (instance == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;) instance = create();&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; instance;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; ISessionFactory create()&lt;br /&gt;        {&lt;br /&gt;            var cfg = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; NHibernate.Cfg.Configuration();&lt;br /&gt;            cfg.AddAssembly(Assembly.GetAssembly(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(MyAssembly)));&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; cfg.BuildSessionFactory();&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;
&lt;/div&gt;

&lt;br /&gt;

&lt;h3&gt;3. A regular Factory class to return Nhibernate sessions.&lt;/h3&gt;

&lt;div&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; NhibernateSessionOpener&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; ISession Open()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; NhibernateSessionFactory.Instance.OpenSession();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;h3&gt;The delegation pipeline&lt;/h3&gt;

&lt;p&gt;This diagram demonstrates how it all fits together:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="pipeline" border="0" alt="pipeline" src="http://www.rjarmstrong.com/images/www_rjarmstrong_com/WindowsLiveWriter/YourownNHibernateSessionFactoryforWebapp_1433F/pipeline_thumb.png" width="272" height="430" /&gt;&lt;/p&gt;

&lt;br /&gt;

&lt;br /&gt;

&lt;h3&gt;Programmatic usage:&lt;/h3&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var session = Container.Instance.Resolve&amp;lt;SessionImpl&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;

&lt;h3&gt;
  &lt;br /&gt;Configuration for constructor or setter injection:&lt;/h3&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&amp;lt;component id=&lt;span style="color: #006080"&gt;"myComponent"&lt;/span&gt; type=&lt;span style="color: #006080"&gt;"MyAssembly.MyNamespace.MyComponent, &lt;br /&gt;           MyAssembly.MyNamespace"&lt;/span&gt;&amp;gt;&lt;br /&gt;    &amp;lt;parameters&amp;gt;&lt;br /&gt;        &amp;lt;Session&amp;gt;${nhsessionopener}&amp;lt;/Session&amp;gt;&lt;br /&gt;    &amp;lt;/parameters&amp;gt;&lt;br /&gt;&amp;lt;/component&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;So to wrap up, whilst you do need to do a little work setting this up this is really only the addition of two small classes, the benefit of course is that you have more flexibility and are not hampered by unnecessary abstraction.&lt;/p&gt;&lt;img src="http://www.rjarmstrong.com/aggbug/4.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>rjarmstrong</dc:creator>
            <guid>http://www.rjarmstrong.com/archive/2009/10/21/your-own-nhibernate-sessionfactory-for-web-applications.aspx</guid>
            <pubDate>Wed, 21 Oct 2009 23:16:34 GMT</pubDate>
            <wfw:comment>http://www.rjarmstrong.com/comments/4.aspx</wfw:comment>
            <comments>http://www.rjarmstrong.com/archive/2009/10/21/your-own-nhibernate-sessionfactory-for-web-applications.aspx#feedback</comments>
            <wfw:commentRss>http://www.rjarmstrong.com/comments/commentRss/4.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Unleashing Attributes with Dynamic Proxy</title>
            <category>Dynamic Proxy</category>
            <category>AOP</category>
            <link>http://www.rjarmstrong.com/archive/2009/10/19/unleashing-attributes-with-dynamic-proxy.aspx</link>
            <description>&lt;p&gt;A flexible way of hooking into attributes is by using a factory method to generate proxies of your entities. In my example I’m going to create a proxy using Dynamic Proxy and then write the name of the method called plus the exact time in milliseconds to the output screen in Visual Studio. The benefit of this is that this functionality does not dirty my entities with non-core detail and makes it easy to apply the functionality in a declarative style.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Desired Result      &lt;br /&gt;&lt;/strong&gt;By running the following code we can track the exact time between method calls to proxied instances of the class. This gives us a lightweight debugging performance monitor for our application.&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[Test]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; should_print_time_and_message_of_proxy_member_call()&lt;br /&gt;{&lt;br /&gt;    var dummy = ProxyFactory.Create&amp;lt;Dummy&amp;gt;();&lt;br /&gt;    dummy.Method1();&lt;br /&gt;    Thread.Sleep(100);&lt;br /&gt;    dummy.Method2();&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;the output from the console should look like this: 

  &lt;br /&gt;&lt;/div&gt;

&lt;div&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Dummy instance accessed. @ 00:38:34.8752500875&lt;br /&gt;Method1 Executed.&lt;br /&gt;Dummy instance accessed. @ 00:38:34.9846250984&lt;br /&gt;Method2 Executed.&lt;/pre&gt;
&lt;/div&gt;

&lt;div&gt;
  &lt;br /&gt;&lt;strong&gt;The Dummy Class 
    &lt;br /&gt;&lt;/strong&gt;As you can see the attribute has been applied to our ‘Dummy Class’ with a general message. 

  &lt;br /&gt;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[TimeLog(&lt;span style="color: #006080"&gt;"Dummy instance accessed."&lt;/span&gt;)]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Dummy&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Method1()&lt;br /&gt;    {&lt;br /&gt;        Debug.WriteLine(&lt;span style="color: #006080"&gt;"Method1 Executed."&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Method2()&lt;br /&gt;    {&lt;br /&gt;        Debug.WriteLine(&lt;span style="color: #006080"&gt;"Method2 Executed."&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;&lt;strong&gt;Defining the Attribute&lt;/strong&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;sealed&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; TimeLoggerAttribute : Attribute&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; _message;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; TimeLoggerAttribute(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message)&lt;br /&gt;    {&lt;br /&gt;        _message = message;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;As you can see our attribute simply allows us to add a message to a type member’s metadata.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating the Proxy factory 
    &lt;br /&gt;&lt;/strong&gt;The proxy factory creates a class proxy of the type passed into it as a type parameter, in our example above it is a class called ‘Dummy’. Note, that in order to create a proxy which is capable of being intercepted you must make the members virtual to allow the proxy to override the implementation of each member:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ProxyFactory&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; ProxyGenerator ProxyGenerator = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ProxyGenerator();&lt;br /&gt;&lt;br /&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; T Create&amp;lt;T&amp;gt;()&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (T)Create(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T));&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; Create(Type type)&lt;br /&gt;     {&lt;br /&gt;         var proxy = ProxyGenerator.CreateClassProxy(&lt;br /&gt;            type, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; IInterceptor[] { &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; TimeLogInterceptor() });&lt;br /&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; proxy;&lt;br /&gt;     }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; TimeLogInterceptor : IInterceptor&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Intercept(IInvocation invocation)&lt;br /&gt;     {&lt;br /&gt;         var type = invocation.Proxy.GetType().BaseType;&lt;br /&gt;         var attributes = type.GetCustomAttributes(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(TimeLogAttribute), &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;);&lt;br /&gt;         var attribute = attributes.FirstOrDefault() &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; TimeLogAttribute;&lt;br /&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (attribute != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;         {&lt;br /&gt;             Debug.WriteLine(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #006080"&gt;"{0} @ {1}{2}"&lt;/span&gt;, &lt;br /&gt;                attribute.Message, DateTime.Now.TimeOfDay, DateTime.Now.Millisecond));&lt;br /&gt;         }&lt;br /&gt;         invocation.Proceed();&lt;br /&gt;     }&lt;br /&gt; }&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The TimeLogInterceptor is doing the real work here, whilst the example given is somewhat fictional, by using attributes and proxies it opens up opportunities for aspect-oriented style programming you may not have attempted before. 
  &lt;br /&gt;

  &lt;br /&gt;&lt;strong&gt;Links:&lt;/strong&gt; 

  &lt;br /&gt;Excellent Multipart Tutorial about Dynamic Proxy by &lt;a href="http://kozmic.pl/archive/2008/12/16/castle-dynamicproxy-tutorial-part-i-introduction.aspx" target="_blank"&gt;Krzysztof Koźmic&lt;/a&gt;. 

  &lt;br /&gt;Castle Project &lt;a href="http://www.castleproject.org/dynamicproxy/index.html" target="_blank"&gt;Dynamic Proxy&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://www.rjarmstrong.com/aggbug/2.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>rjarmstrong</dc:creator>
            <guid>http://www.rjarmstrong.com/archive/2009/10/19/unleashing-attributes-with-dynamic-proxy.aspx</guid>
            <pubDate>Tue, 20 Oct 2009 00:03:22 GMT</pubDate>
            <wfw:comment>http://www.rjarmstrong.com/comments/2.aspx</wfw:comment>
            <comments>http://www.rjarmstrong.com/archive/2009/10/19/unleashing-attributes-with-dynamic-proxy.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://www.rjarmstrong.com/comments/commentRss/2.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>