<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xml:lang="en-US"><title type="text">Krzysztof Koźmic's blog</title><link rel="alternate" type="text/html" href="http://kozmic.pl/Atom.aspx" /><subtitle type="html">if it doesn't work, it's probably your fault.</subtitle><author><name>Krzysztof Koźmic</name><uri>http://kozmic.pl/Default.aspx</uri></author><updated>2009-10-30T11:20:23+00:00</updated><generator uri="http://subtextproject.com">Subtext</generator><id>http://kozmic.pl/Default.aspx</id><geo:lat>50.000000</geo:lat><geo:long>19.900000</geo:long><link rel="license" type="text/html" href="http://creativecommons.org/licenses/by/2.0/" /><logo>http://creativecommons.org/images/public/somerights20.gif</logo><link rel="self" href="http://feeds.feedburner.com/kozmic" type="application/atom+xml" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">kozmic</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><entry><title type="text">Castle Dynamic Proxy tutorial part XV: Patterns and Antipatterns</title><link rel="alternate" type="text/html" href="http://kozmic.pl/archive/2009/10/30/castle-dynamic-proxy-tutorial-part-xv-patterns-and-antipatterns.aspx" /><updated>2009-10-30T04:20:23-07:00</updated><id>http://kozmic.pl/archive/2009/10/30/castle-dynamic-proxy-tutorial-part-xv-patterns-and-antipatterns.aspx</id><content type="html">&lt;p&gt;We’ve &lt;a href="http://kozmic.pl/archive/2009/04/27/castle-dynamic-proxy-tutorial.aspx" target="_blank"&gt;covered almost all of Dynamic Proxy&lt;/a&gt;. If you followed along through this series, you now know 95% of Dynamic Proxy 2.1 features that get used 99,9% of the time. Now is the time to wrap up, and with that we’ll review some of the most common pitfalls that you may encounter when developing code on top of Dynamic Proxy.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h3&gt;Leaking this&lt;/h3&gt;  &lt;p&gt;Consider this simple interface/class pair&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IFoo&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    IFoo Bar();&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Foo : IFoo&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; IFoo Bar()&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;Now, let’s say we create a proxy for IFoo with target and use it like this:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;var foo = GetFoo(); &lt;span class="rem"&gt;// returns proxy&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;var bar = foo.Bar();&lt;/pre&gt;

  &lt;pre class="alt"&gt;bar.Bar();&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;Can you see the bug here? The second call is performed not on a proxy but on a target object itself! Our proxy is leaking its target.&lt;/p&gt;

&lt;p&gt;This issue obviously does not affect class proxies (since in that case proxy and target are the same object). Why does not Dynamic Proxy handle this scenario on its own? Because there’s no general easy way to handle this. The example I showed is the most trivial one, but proxied object can leak this in a myriad of different ways. It can leak it as a property of returned object, it can leak it as &lt;em&gt;sender&lt;/em&gt; argument of raised event, it can assign &lt;em&gt;this&lt;/em&gt; to some global variable, it can pass itself to a method on one of its own arguments etc. Dynamic Proxy can’t predict any of these, nor should it.&lt;/p&gt;

&lt;p&gt;In some of these cases there is often not much you can do about it, and its good to know that problem like this exist, and understand its consequences. In other cases though, fixing the issue is very simple indeed.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; LeakingThisInterceptor:IInterceptor&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Intercept(IInvocation invocation)&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        invocation.Proceed();&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt;(invocation.ReturnValue == invocation.InvocationTarget)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            invocation.ReturnValue = invocation.Proxy;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;You add an interceptor (put it as last one in the interceptors pipeline), that switches the leaking target back to proxy instance. It’s as simple as that. Notice that this interceptor is targeted specifically at the scenario from our example above (target leaking via return value). For each case you will need a dedicated interceptor.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;Override equality&lt;/h3&gt;

&lt;p&gt;One of the most common mistakes when it comes to Dynamic Proxy is not overriding Equals/GetHashCode methods on proxy generation hooks and interceptor selectors, which means you’re giving up &lt;a href="http://kozmic.pl/archive/0001/01/01/castle-dynamic-proxy-tutorial-part-xii-caching.aspx" target="_blank"&gt;caching&lt;/a&gt; and that in turn coupled with &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=472622&amp;amp;wa=wsignin1.0" target="_blank"&gt;bugs in BCL&lt;/a&gt; means performance hit (plus increased memory consumption).&lt;/p&gt;

&lt;p&gt;Solution is very simple, and there’s no exceptions to this rule – always override Equals/GetHashCode methods on all your classes implementing either IProxyGenerationHook or IInterceptorSelector.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;Make your Proxy Generation Hooks purely functional&lt;/h3&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Functional_programming#Pure_functions" target="_blank"&gt;Pure function&lt;/a&gt;, is a function that for given set of inputs always returns the same output. In case of proxy generation hook, it means that two equal (as specified by overriden Equals/GetHashCode methods) proxy generation hooks will for given type to proxy return the same values from their methods, and when asked again about the same type will again return the same values/throw the same exceptions.&lt;/p&gt;

&lt;p&gt;This is a major assumption that Dynamic Proxy makes, and that’s what makes the caching mechanism work. If proxy generation hook is equal to the one already used to generate a proxy type, Dynamic Proxy will assume it would return the same values as the other one, which would result in identical proxy type, so it cuts through the generation process and returns the existing proxy type.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;Make your supporting classes serializable&lt;/h3&gt;

&lt;p&gt;If you’re going to be serializing your proxies, you should make all the classes that go with it serializable. That includes proxy generation hooks, interceptors and interceptor selectors. Otherwise you will get an exception when trying to serialize your proxies. It is not mandatory, but I find it useful. Notice that you will need this also when persisting your proxy assembly to disk.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;Use ProxyGenerationHooks and InterceptorSelectors for fine grained control&lt;/h3&gt;

&lt;p&gt;Do your interceptor’s methods look like this?&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Intercept(IInvocation invocation)&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt;(invocation.TargetType!=&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Foo))&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        invocation.Proceed();&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt;(invocation.Method.Name!=&lt;span class="str"&gt;"Bar"&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        invocation.Proceed();&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt;(invocation.Method.GetParameters().Length!=3)&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        invocation.Proceed();&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;    DoSomeActualWork(invocation);&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;If they do this often means you’re doing something wrong. Move the decisions to proxy generation hook and interceptor selector&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Do I ever want to intercept this method? If the answer is no, use proxy generation hook to filter it out of methods to proxy. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Notice that due to bug in Dynamic Proxy 2.1, if you choose not to proxy method on interface proxy, you will get an exception. Workaround for this is to say you want to intercept the method, and then use interceptor selector to return no interceptors for the method. This bug is fixed in Dynamic Proxy 2.2&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;If I do want to intercept this method, which interceptors do I want to use? Do I need all of them? Do I need just a single one? Use interceptor selector to control this. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the other hand, remember that as every feature this one is also a double edged sword. Too liberal use of proxy generation hooks and interceptor selectors may greatly decrease efficiency of proxy type caching, which may hurt your performance. As always think how much control you need and what the implications on caching will be. Sometimes single if on top of your interceptor is lesser evil than increasing number of proxies required tenfold. As always – use the profiler in scenarios that mimic your production scenarios as closely as possible to check which option is the best for you.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;SRP applies to interceptors&lt;/h3&gt;

&lt;p&gt;SRP stands for &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" target="_blank"&gt;Single Responsibility Principle&lt;/a&gt;, which means that a class should do just one thing. Many people seem to forget about it when it comes to interceptors. They create one monstrous interceptor class that tries to do all the things they need from Dynamic Proxy – logging, security checking, parameter verification, augmenting target objects with behavior and many more.&lt;/p&gt;

&lt;p&gt;Remember that Dynamic Proxy lets you have many interceptors per method call. Use this ability to split behavior between interceptors. You may end up with some general purpose interceptors for things like logging that you use for each intercepted method on each class. As long as all it does is logging – that’s ok.&lt;/p&gt;

&lt;p&gt;You may end up with some interceptors that are used for methods on just some classes, like classes inheriting from common base class. As long as these interceptors do just one thing – that’s fine.&lt;/p&gt;

&lt;p&gt;You may end up with some interceptors that exist solely for the purpose of intercepting just a single method on specific class or interface. That also is fine. Use interceptor selectors to match interceptors to their respective targets, and don’t be afraid to have multiple interceptors per method.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:49834463-257e-4f2b-9ecc-f80d1e82e23e" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Dynamic+Proxy" rel="tag"&gt;Dynamic Proxy&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Castle" rel="tag"&gt;Castle&lt;/a&gt;&lt;/div&gt;&lt;img src="http://kozmic.pl/aggbug/254.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/kozmic/~4/zbG74hPrSTg" height="1" width="1"/&gt;</content><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/254.aspx</wfw:comment><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/commentRss/254.aspx</wfw:commentRss><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://kozmic.pl/services/trackbacks/254.aspx</trackback:ping></entry><entry><title type="text">Castle Windsor forwarded types and proxies</title><link rel="alternate" type="text/html" href="http://kozmic.pl/archive/2009/10/22/castle-windsor-forwarded-types-and-proxies.aspx" /><updated>2009-10-22T14:25:22-07:00</updated><id>http://kozmic.pl/archive/2009/10/22/castle-windsor-forwarded-types-and-proxies.aspx</id><content type="html">&lt;p&gt;Castle Windsor allows you to use single component for multiple services, which is called Forwarded Types.&lt;/p&gt;  &lt;h3&gt;Forwarded Types&lt;/h3&gt;  &lt;p&gt;In other words, you can tell Windsor – when IFoo is requested use FooBar as implementation, and when Bar is requested also use FooBar (when using default lifestyle of singleton you’ll get the same instance).&lt;/p&gt;  &lt;p&gt;Here’s some code:&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;var container = &lt;span class="kwrd"&gt;new&lt;/span&gt; WindsorContainer();&lt;/pre&gt;

  &lt;pre&gt;container.Register(Component.For&amp;lt;Bar&amp;gt;().Forward&amp;lt;IFoo&amp;gt;()&lt;/pre&gt;

  &lt;pre class="alt"&gt;                       .ImplementedBy&amp;lt;FooBar&amp;gt;());&lt;/pre&gt;

  &lt;pre&gt;var foo = container.Resolve&amp;lt;IFoo&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;var bar = container.Resolve&amp;lt;Bar&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;Debug.Assert(foo == bar);&lt;/pre&gt;

  &lt;pre class="alt"&gt;foo.DoFoo();&lt;/pre&gt;

  &lt;pre&gt;bar.DoBar();&lt;/pre&gt;

  &lt;pre class="alt"&gt;Console.ReadKey(&lt;span class="kwrd"&gt;true&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;h3&gt;Proxies&lt;/h3&gt;

&lt;p&gt;What if you want to use proxies for that component though?&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;var container = &lt;span class="kwrd"&gt;new&lt;/span&gt; WindsorContainer();&lt;/pre&gt;

  &lt;pre&gt;container.Register(Component.For&amp;lt;IInterceptor&amp;gt;()&lt;/pre&gt;

  &lt;pre class="alt"&gt;                       .Named(&lt;span class="str"&gt;"interceptor"&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre&gt;                       .ImplementedBy&amp;lt;FooBarInterceptor&amp;gt;(),&lt;/pre&gt;

  &lt;pre class="alt"&gt;                   Component.For&amp;lt;Bar&amp;gt;().Forward&amp;lt;IFoo&amp;gt;()&lt;/pre&gt;

  &lt;pre&gt;                       .ImplementedBy&amp;lt;FooBar&amp;gt;()&lt;/pre&gt;

  &lt;pre class="alt"&gt;                       .Interceptors(&lt;span class="kwrd"&gt;new&lt;/span&gt; InterceptorReference(&lt;span class="str"&gt;"interceptor"&lt;/span&gt;)).Anywhere);&lt;/pre&gt;

  &lt;pre&gt;var foo = container.Resolve&amp;lt;IFoo&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;var bar = container.Resolve&amp;lt;Bar&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;Debug.Assert(foo == bar);&lt;/pre&gt;

  &lt;pre class="alt"&gt;foo.DoFoo();&lt;/pre&gt;

  &lt;pre&gt;bar.DoBar();&lt;/pre&gt;

  &lt;pre class="alt"&gt;Console.ReadKey(&lt;span class="kwrd"&gt;true&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;Now, what happens next depends on how you implemented the interface IFoo on class FooBar. Say this is FooBar:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; FooBar : Bar, IFoo&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoFoo()&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        Console.WriteLine(&lt;span class="str"&gt;"DoFoo"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoBar()&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        Console.WriteLine(&lt;span class="str"&gt;"DoBar"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p /&gt;

&lt;p&gt;Notice that DoFoo is non-virtual. In this case, here’s what we’re gonna get.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/CastleWindsorforwardedtypesandproxies_12D2B/windsor_1.png" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="windsor_1" border="0" alt="windsor_1" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/CastleWindsorforwardedtypesandproxies_12D2B/windsor_1_thumb.png" width="316" height="178" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;DoFoo did not get intercepted. So what’s the issue here, and how do we fix it?&lt;/p&gt;

&lt;h3&gt;The What&lt;/h3&gt;

&lt;p&gt;When you forward a registration, Windsor runs just the first type through the complete registration pipeline, and subsequent forwarded types are treated just as additional piece of data “Oh, by the way, use this component I just registered for this type as well". Proxy registration is a part of the component model building, and since we end up having just one component only information about its main type gets recorded for proxying.&lt;/p&gt;

&lt;p&gt;While this might appear at first clearly as a bug, I think it’s rather a by-design feature. Forcing Windsor to figure it out by itself could pretty quickly become very tricky and we might not always get what we expected. There are however ways of getting what we want.&lt;/p&gt;

&lt;h3&gt;The fix no 1&lt;/h3&gt;

&lt;p&gt;Now that we know what we’re up against, how do we fix it? First and the most trivial fix would be simply to make the DoFoo virtual – this way it would get picked when proxying Bar base class and we could successfully proxy it. While this may not always be applicable (you may not be able to modify the class) this is the only option that is available if you’re using the released version. However if you’re using trunk there are two more possible ways of bending it to our will.&lt;/p&gt;

&lt;h3&gt;Meet my attributes&lt;/h3&gt;

&lt;p&gt;Due to some changes in how Dynamic Proxy 2.2 (current trunk) handles additional interfaces to proxy, it is possible to intercept non-virtually implemented interface members on a class proxy. Since Windsor by default will request just the class proxy (with no additional interfaces) we need to tell it to toss an IFoo attribute in as well. The quickest way of doing it is throwing an attribute on top of our class:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;[ComponentProxyBehavior(AdditionalInterfaces = &lt;span class="kwrd"&gt;new&lt;/span&gt;[] { &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IFoo) })]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; FooBar : Bar, IFoo&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoFoo()&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        Console.WriteLine(&lt;span class="str"&gt;"DoFoo"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoBar()&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        Console.WriteLine(&lt;span class="str"&gt;"DoBar"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;If we run the code now we’ll get this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/CastleWindsorforwardedtypesandproxies_12D2B/windsor_2.png" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="windsor_2" border="0" alt="windsor_2" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/CastleWindsorforwardedtypesandproxies_12D2B/windsor_2_thumb.png" width="268" height="178" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;This is not a solution most people would choose anyway. Even if you can do it (your service does not come from a 3rd party library), you’re decorating your service class with a Windsor-specific attribute which many consider an anti-pattern. There’s however a third, more pure way.&lt;/p&gt;

&lt;p&gt;Remember that you need trunk version of Dynamic Proxy for this to work. If you use version 2.1 you’ll end up with this instead:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/CastleWindsorforwardedtypesandproxies_12D2B/windsor_3.png" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="windsor_3" border="0" alt="windsor_3" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/CastleWindsorforwardedtypesandproxies_12D2B/windsor_3_thumb.png" width="475" height="290" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Windsor will implement the interface, but it will treat it as interface proxy without target. You can make it work by inserting a dedicated interceptor that will forward the calls to your class, but it’s something you’ll have to do manually all the way through.&lt;/p&gt;

&lt;h3&gt;Black belt approach&lt;/h3&gt;

&lt;p&gt;I said that forwarded types don’t get ran through whole registration pipeline. However, kernel does raise its component lifecycle events for them, so we can hook up to them and get notified when our forwarding handler gets registered, and modify its component model.&lt;/p&gt;

&lt;p&gt;We start by hooking up to kernels HandlerRegistered event, before we register any components.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;var container = &lt;span class="kwrd"&gt;new&lt;/span&gt; WindsorContainer();&lt;/pre&gt;

  &lt;pre&gt;container.Kernel.HandlerRegistered += OnHandlerRegistered;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p /&gt;

&lt;p /&gt;

&lt;p /&gt;

&lt;p&gt;In the OnHandlerRegistered method we check whether the handler at hand is a ForwardingHandler and if so we add its interface to the list of additional interfaces we want to proxy, just like we did using attribute in the example above.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnHandlerRegistered(Castle.MicroKernel.IHandler handler, &lt;span class="kwrd"&gt;ref&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; stateChanged)&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    var forwardingHandler = handler &lt;span class="kwrd"&gt;as&lt;/span&gt; ForwardingHandler;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (forwardingHandler == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt;; &lt;span class="rem"&gt;//we're only interested in forwarding handlers...&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt;(!forwardingHandler.Service.IsInterface)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt;; &lt;span class="rem"&gt;//we're only interested in interface services...&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;    var targetHandler = forwardingHandler.Target;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt;(!targetHandler.ComponentModel.ExtendedProperties.Contains(ProxyConstants.ProxyOptionsKey))&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt;; &lt;span class="rem"&gt;//apparently this service is not registered for proxying&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;    var options = targetHandler.ComponentModel.ExtendedProperties[ProxyConstants.ProxyOptionsKey] &lt;span class="kwrd"&gt;as&lt;/span&gt; ProxyOptions;&lt;/pre&gt;

  &lt;pre&gt;    Debug.Assert(options != &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;    options.AddAdditionalInterfaces(forwardingHandler.Service);&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;Now we get our interface proxied without having to touch our component’ code.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:55ae3962-ae22-4247-bc0c-ae91528a8244" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Castle+Windsor" rel="tag"&gt;Castle Windsor&lt;/a&gt;&lt;/div&gt;&lt;img src="http://kozmic.pl/aggbug/253.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/kozmic/~4/CzFstqKcDbE" height="1" width="1"/&gt;</content><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/253.aspx</wfw:comment><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/commentRss/253.aspx</wfw:commentRss><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://kozmic.pl/services/trackbacks/253.aspx</trackback:ping></entry><entry><title type="text">Mapping different types with NHibernate - IUserType</title><link rel="alternate" type="text/html" href="http://kozmic.pl/archive/2009/10/12/mapping-different-types-with-nhibernate-iusertype.aspx" /><updated>2009-10-13T01:04:44-07:00</updated><id>http://kozmic.pl/archive/2009/10/12/mapping-different-types-with-nhibernate-iusertype.aspx</id><content type="html">&lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/MappingdifferenttypeswithNHibernateIUser_14664/warninglabel_thumb1.jpg" rel="lightbox"&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="warninglabel_thumb[1]" border="0" alt="warninglabel_thumb[1]" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/MappingdifferenttypeswithNHibernateIUser_14664/warninglabel_thumb1_thumb.jpg" width="504" height="254" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Recently I had a problem with the application I’ve been working on. One of entity types in my domain had a property of type &lt;em&gt;uint&lt;/em&gt;. Not a big deal, until you want to store it in Microsoft SQL Server database which &lt;a href="http://msdn.microsoft.com/en-us/library/ms187752.aspx" target="_blank"&gt;does not support unsigned types&lt;/a&gt;. I’ve been scratching my head for a moment and then I found a solution – let’s map it as &lt;em&gt;long&lt;/em&gt; in our database. Since &lt;em&gt;long&lt;/em&gt; can represent any legal value of &lt;em&gt;uint&lt;/em&gt;, we should be all good, right? So let’s do it.&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;div class="csharpcode"&gt;     &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ClassWithUintProperty&lt;/pre&gt;

    &lt;pre&gt;{&lt;/pre&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; Guid Id { get; set; }&lt;/pre&gt;

    &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;uint&lt;/span&gt; UintProp { get ;set; }&lt;/pre&gt;

    &lt;pre class="alt"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p /&gt;

&lt;p /&gt;

&lt;p /&gt;

&lt;p /&gt;

&lt;p /&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0"&lt;/span&gt;?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;="urn:nhibernate-mapping-2.2"&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;="NHibernate.Test"&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;="NHibernate.Test"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="ClassWithUintProperty"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="Id"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="UIntProp"&lt;/span&gt; &lt;span class="attr"&gt;not-null&lt;/span&gt;&lt;span class="kwrd"&gt;="true"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="long"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p /&gt;

&lt;h3&gt; &lt;/h3&gt;

&lt;h3&gt;"Houston, we've had a problem"&lt;/h3&gt;

&lt;p&gt;All good. At least until you try to fetch saved value from the database. When you do, you’re up for an unpleasant surprise:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;System.InvalidCastException: Specified cast is not valid.&lt;/p&gt;

  &lt;p&gt;NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of NHibernate.Test.UIntAsLong&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The exception is a result of how NHibernate optimizes property access and quirks of mixing different conversion operators (unboxing and numeric conversion in this case) and are not really interesting. What’s important, is that we approached the problem from the wrong angle.&lt;/p&gt;

&lt;p&gt;What we’re dealing with here, is inability of our database engine to deal with datatype in our model, which we were trying to solve by pushing this onto NHibernate without telling it something is ‘”wrong”. While NHibernate is smart, it’s working based on a set of explicit information, so what we need to do, is to be explicit about what we want it to do.&lt;/p&gt;

&lt;p&gt;There are two places where we can tell NHibernate about it.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;IUserType, which will explicitly handle the mapping from &lt;em&gt;uint&lt;/em&gt; in our model to &lt;em&gt;long&lt;/em&gt; in the DB &lt;/li&gt;

  &lt;li&gt;custom Dialect which will basically lie to NHibernate telling it “yeah, sure this DB supports &lt;em&gt;uint&lt;/em&gt;s – whole dozens of ‘em!” and do some work under the covers to live up to its promise. (not shown in this post). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;Enter IUserType&lt;/h3&gt;

&lt;p&gt;&lt;a href="http://nhforge.org/doc/nh/en/index.html#mapping-types-custom" target="_blank"&gt;IUserType&lt;/a&gt; is an extension point in NHibernate that let’s you plug in to the mapping process and handle it yourself. The interface is quite big, but there’s very little real logic there:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; UIntAsLong:IUserType&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; SqlType[] SqlTypes&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt;[] { SqlTypeFactory.Int64 }; }&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; Type ReturnedType&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;typeof&lt;/span&gt;( &lt;span class="kwrd"&gt;uint&lt;/span&gt; ); }&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsMutable&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;; }&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; GetHashCode( &lt;span class="kwrd"&gt;object&lt;/span&gt; x )&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt;( x == &lt;span class="kwrd"&gt;null&lt;/span&gt; )&lt;/pre&gt;

  &lt;pre class="alt"&gt;            {&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; 0;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; x.GetHashCode();&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; NullSafeGet( IDataReader rs, &lt;span class="kwrd"&gt;string&lt;/span&gt;[] names, &lt;span class="kwrd"&gt;object&lt;/span&gt; owner )&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;object&lt;/span&gt; obj = NHibernateUtil.UInt32.NullSafeGet( rs, names0 );&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt;( obj == &lt;span class="kwrd"&gt;null&lt;/span&gt; )&lt;/pre&gt;

  &lt;pre class="alt"&gt;            {&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;uint&lt;/span&gt;)obj;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; NullSafeSet( IDbCommand cmd, &lt;span class="kwrd"&gt;object&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;, &lt;span class="kwrd"&gt;int&lt;/span&gt; index )&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            Debug.Assert( cmd != &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt;( &lt;span class="kwrd"&gt;value&lt;/span&gt; == &lt;span class="kwrd"&gt;null&lt;/span&gt; )&lt;/pre&gt;

  &lt;pre class="alt"&gt;            {&lt;/pre&gt;

  &lt;pre&gt;                ((IDataParameter)cmd.Parametersindex).Value = DBNull.Value;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            {&lt;/pre&gt;

  &lt;pre&gt;                var uintValue = (&lt;span class="kwrd"&gt;uint&lt;/span&gt;)&lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                ( (IDataParameter) cmd.Parametersindex ).Value = (&lt;span class="kwrd"&gt;long&lt;/span&gt;) uintValue;&lt;/pre&gt;

  &lt;pre&gt;            }&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; DeepCopy( &lt;span class="kwrd"&gt;object&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt; )&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="rem"&gt;// we can ignore it...&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; Replace( &lt;span class="kwrd"&gt;object&lt;/span&gt; original, &lt;span class="kwrd"&gt;object&lt;/span&gt; target, &lt;span class="kwrd"&gt;object&lt;/span&gt; owner )&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="rem"&gt;// we can ignore it...&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; original;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; Assemble( &lt;span class="kwrd"&gt;object&lt;/span&gt; cached, &lt;span class="kwrd"&gt;object&lt;/span&gt; owner )&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="rem"&gt;// we can ignore it...&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; cached;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; Disassemble( &lt;span class="kwrd"&gt;object&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt; )&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="rem"&gt;// we can ignore it...&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;bool&lt;/span&gt; IUserType.Equals( &lt;span class="kwrd"&gt;object&lt;/span&gt; x, &lt;span class="kwrd"&gt;object&lt;/span&gt; y )&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;.Equals( x, y );&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;} &lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;There are really two parts of the code that are interesting. SqlTypes / ReturnedType properties which tell NHibernate which types to expect on both sides of the mapping, and the NullSafeGet / NullSafeSet methods which perform the actual conversion.&lt;/p&gt;

&lt;p&gt;Now we just need to plug our custom user type to the mapping, and it goes like this:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0"&lt;/span&gt;?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;="urn:nhibernate-mapping-2.2"&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;="NHibernate.Test"&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;="NHibernate.Test"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="ClassWithUintProperty"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="Id"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="UIntProp"&lt;/span&gt; &lt;span class="attr"&gt;not-null&lt;/span&gt;&lt;span class="kwrd"&gt;="true"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="Foo.Namespace.UIntAsLong, Foo.Assembly"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p /&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:25bed83b-6bb8-4457-992f-7f47f3812c4c" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/NHibernate" rel="tag"&gt;NHibernate&lt;/a&gt;&lt;/div&gt;&lt;img src="http://kozmic.pl/aggbug/252.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/kozmic/~4/dLHX0k2PE6U" height="1" width="1"/&gt;</content><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/252.aspx</wfw:comment><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/commentRss/252.aspx</wfw:commentRss><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://kozmic.pl/services/trackbacks/252.aspx</trackback:ping></entry><entry><title type="text">Important milestone</title><link rel="alternate" type="text/html" href="http://kozmic.pl/archive/2009/10/08/important-milestone.aspx" /><updated>2009-10-08T14:53:27-07:00</updated><id>http://kozmic.pl/archive/2009/10/08/important-milestone.aspx</id><content type="html">&lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/Importantmilestone_133E2/SL_build.png" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="SL_build" border="0" alt="SL_build" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/Importantmilestone_133E2/SL_build_thumb.png" width="640" height="293" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As of now Dynamic Proxy is passing all the tests under Silverlight. There’s still some work to do, but it is pretty stable the way it is, and it’s been used by Silverlight versions of Rhino Mocks, Moq and probably some others as well for some time.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:58e3ad09-1880-4f76-ac72-b18eeed6bc3f" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Castle" rel="tag"&gt;Castle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Silverlight" rel="tag"&gt;Silverlight&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Dynamic+Proxy" rel="tag"&gt;Dynamic Proxy&lt;/a&gt;&lt;/div&gt;&lt;img src="http://kozmic.pl/aggbug/251.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/kozmic/~4/4aE5fpnDdR0" height="1" width="1"/&gt;</content><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/251.aspx</wfw:comment><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/commentRss/251.aspx</wfw:commentRss><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://kozmic.pl/services/trackbacks/251.aspx</trackback:ping></entry><entry><title type="text">Book review - The Seed of Hope</title><link rel="alternate" type="text/html" href="http://kozmic.pl/archive/2009/10/07/book-review-the-seed-of-hope.aspx" /><updated>2009-10-07T10:34:00-07:00</updated><id>http://kozmic.pl/archive/2009/10/07/book-review-the-seed-of-hope.aspx</id><content type="html">&lt;div&gt;   &lt;p&gt;&lt;img src="http://ecx.images-amazon.com/images/I/51dxkvobvIL._SL160_.jpg" rel="lightbox" /&gt; &lt;/p&gt;    &lt;p&gt;&lt;a href="http://www.amazon.com/Seed-Hope-Talent-Tree/dp/1934356352%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dkrzkomicsblo-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D1934356352"&gt;The Seed of Hope (Talent Tree)&lt;/a&gt; &lt;/p&gt; &lt;/div&gt;  &lt;p /&gt;  &lt;p&gt;I used to be a big fan of fantasy and science fiction books back in a day. I read a ton of books by &lt;a href="http://en.wikipedia.org/wiki/David_Eddings" target="_blank"&gt;David Eddings&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Mercedes_Lackey" target="_blank"&gt;Mercedes Lackey&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/R._A._Salvatore" target="_blank"&gt;R. A. Salvatore&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Janusz_Zajdel" target="_blank"&gt;Janusz A. Zajdel&lt;/a&gt; and countless other authors. I’m still completing my collection of &lt;a href="http://en.wikipedia.org/wiki/Discworld" target="_blank"&gt;Discworld&lt;/a&gt; books, and trying to find some time to read the monstrous (1000+ pages) &lt;a href="http://en.wikipedia.org/wiki/Ice_%28Dukaj_novel%29" target="_blank"&gt;Lód&lt;/a&gt; by &lt;a href="http://en.wikipedia.org/wiki/Jacek_Dukaj" target="_blank"&gt;Jacek Dukaj&lt;/a&gt;. When Pragmatic Bookshelf released their first non-technical book, “The Seed of Hope”, I thought it might be an interesting venture – fantasy book by technical publisher.&lt;/p&gt;  &lt;p&gt;We get quite a standard story, variations of which I’ve seen many times, so if you’re expecting some revolutionary plot… there isn’t any. We get the variation of an usual tale of an orphan child going on a trip to unveil its destiny and save the world along the way with the small help of some friends and their magical talents that they learn as they go.&lt;/p&gt;  &lt;p&gt;That in itself is not a downside by any means, the whole &lt;a href="http://www.amazon.com/Belgariad-Part-Books-1-3-Magicians/dp/B000R2UVE6%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dkrzkomicsblo-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB000R2UVE6" target="_blank"&gt;Belgariad&lt;/a&gt; &lt;a href="http://www.amazon.com/Belgariad-Vol-Books-Wizardry-Enchanters/dp/0345456319%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dkrzkomicsblo-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0345456319" target="_blank"&gt;saga&lt;/a&gt; (five books, 500+ pages each)  are basically following the same idea, with &lt;a href="http://www.amazon.com/Malloreon-Guardians-Karanda-Sorceress-Darshiva/dp/B0013FVGNQ%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dkrzkomicsblo-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0013FVGNQ" target="_blank"&gt;Malloreon&lt;/a&gt; (yet 5 more books) reiterating it once again, and I had a blast reading them, although it was close to ten years ago.&lt;/p&gt;  &lt;p&gt;I didn’t have a lot of fun reading The Seed of Hope though. First of all, the book is short - less than 300 pages, and it feels like a 700 pages book stripped of content to fit on the limited number of pages. This in turns forced the author to take a lot of shortcuts, not go into a lot of details, and as a result of that – the whole story feels flat. The world is pale, the characters shallow, and the storytelling just scratches the surface of the potential of the world. We don’t get to know the characters well enough to really understand their motivations, and their actions often felt random to me. The big story turning point when good characters crosses to the dark side felt naive for that very reason.&lt;/p&gt;  &lt;p&gt;So we end up with a mediocre book. Not a bad one, as it has all the required parts, but the execution leaves a lot to be desired. I think especially younger readers will find it worthwhile. On the bright side, it motivated me to pick up that &lt;a href="http://www.amazon.com/Carpe-Jugulum-Terry-Pratchett/dp/0061020397%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dkrzkomicsblo-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0061020397" target="_blank"&gt;Carpe Jugulum&lt;/a&gt;, that I’ve been meaning to read for quite some time.&lt;/p&gt;&lt;img src="http://kozmic.pl/aggbug/250.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/kozmic/~4/k-2tXU4pkNU" height="1" width="1"/&gt;</content><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/250.aspx</wfw:comment><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/commentRss/250.aspx</wfw:commentRss><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://kozmic.pl/services/trackbacks/250.aspx</trackback:ping></entry><entry><title type="text">How to contribute to open source without writing a single line of code</title><link rel="alternate" type="text/html" href="http://kozmic.pl/archive/2009/09/09/how-to-contribute-to-open-source-without-writing-a-single-again.aspx" /><updated>2009-09-07T04:01:06-07:00</updated><id>http://kozmic.pl/archive/2009/09/09/how-to-contribute-to-open-source-without-writing-a-single-again.aspx</id><content type="html">&lt;blockquote&gt;
&lt;p&gt;Read the introductory post &lt;a target="_blank" href="http://kozmic.pl/archive/2009/09/06/how-to-contribute-to-open-source.aspx"&gt;here&lt;/a&gt; first.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So, you found an open source project you really like, it saves you a lot of time, takes some friction away from your life, saves you money and you’re full of admiration and gratitude to the people who spend their free time working on it, so that you can stand on their shoulders. You feel like you really should give back to them, or to the (rest of the) community around the project, but you don’t really want or can do the obvious – start fixing bugs or implementing new features, so you don’t. But is it the only way to contribute to the project? Hardly – you can be of great help to the project without writing a single line of code, and dedicating just a tiny fraction of your time to contributing.&lt;/p&gt;
&lt;h3&gt;Give feedback&lt;/h3&gt;
&lt;p&gt;The smallest thing you can do to contribute is give feedback. Developers working on every healthy open source project are constantly looking for users feedback. How would you like a feature to work? How do you like the current feature set? What would be the feature you’d like to see the most in vNext? What behavior in this or that corner case would you find more logical? These the kinds of questions that tend to be asked by the developers to the community, and by speaking your mind on these issues you’re helping the developers make the right decisions.&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://castle.uservoice.com/pages/16605-official-castle-project-feedback-forum"&gt;Some project&lt;/a&gt; &lt;a target="_blank" href="http://ankhsvn.uservoice.com/pages/3267-general"&gt;have set up&lt;/a&gt; dedicated websites, using &lt;a target="_blank" href="http://uservoice.com/"&gt;uservoice.com&lt;/a&gt; where it’s incredibly simple to give feedback – you just click. For more elaborate questions &lt;a target="_blank" href="http://groups.google.com/group/castle-project-users?pli=1"&gt;discussion groups&lt;/a&gt;, or project blogs are used to ask open questions to the community. Take a moment of your time to write a few sentences to shed some light on your perspective on the matter. This may not seem like much but &lt;strong&gt;giving feedback is the most important thing &lt;/strong&gt;in ensuring that the project progresses in the right direction.&lt;/p&gt;
&lt;h3&gt;Help others&lt;/h3&gt;
&lt;p&gt;For commercial software where there’s a big company behind the product, there are usually people hired to build up the body of knowledge about the product. They write documentation, they write articles in magazines, they blog extensively, speak on conferences write books, answer questions on forums etc. Open source projects have usually just a few volunteers writing code, documentation, blogging, answering questions, all at once and on top of that – they do it in their own free (as in speech) time.&lt;/p&gt;
&lt;p&gt;What you can do, is to dedicate some of your own time to take some of the non-coding off of their shoulders. Do subscribe to discussion group of your favorite project, or to your favorite Q&amp;amp;A website where people ask for solutions of their problems with the project. If you see someone asking a question that you know the answer for – do answer. This won’t usually require a lot of time, and someone will be very grateful to you. Also this has another aspect – by stating clearly what you know, or think you know you’re learning better the product itself – you’re building up your expertise and gain recognition and respect in the community. This is especially visible on sites like &lt;a target="_blank" href="http://stackoverflow.com"&gt;StackOverflow.com&lt;/a&gt; where for giving good answers you’re rewarded with points.&lt;/p&gt;
&lt;h3&gt;Write documentation&lt;/h3&gt;
&lt;p&gt;If you find some aspect of using the project hard, and the documentation is not very helpful – improve the documentation. Many projects keep their documentation in the form of a wiki, which means that you’re free to edit and improve it. After all – the documentation is the place where everyone is the most likely to look for solutions in the first place. Help keep it up to date. Help it cover the areas you struggled with. If you blogged your problem – you’ve done most of the hard work already. Add parts of your blog post to the documentation, ensuring that they fit with the rest of the documentation and its rules.&lt;/p&gt;
&lt;h3&gt;It’s not only the code&lt;/h3&gt;
&lt;p&gt;Open Source projects are not only the coding and documentation – it’s also all the ‘meta’ stuff. If you can help improving the website of the project. Write the copy. Improve the design of the website. Design logo. This all is important stuff, although often there’s no person to take care of that.&lt;/p&gt;
&lt;h3&gt;Blog, speak on User Group meetings&lt;/h3&gt;
&lt;p&gt;If you have a blog – do write about the project. Post solutions event to what after the fact may seem as the most trivial problem. If it took you 15 minutes to figure out, it is worth posting about. You’ll be surprised how many people will find your post helpful.&lt;/p&gt;
&lt;p&gt;If you like the project – get others involved. Speak on your local user group meeting about it, show what you like about it, how it helped you, why you like it. By advocating good project you’re getting others involved. You’re expanding the community which means more people learn about it, use it and ultimately more people contribute to the project.&lt;/p&gt;
&lt;h3&gt;Donate money&lt;/h3&gt;
&lt;p&gt;Open source projects are free to use, but they certainly are not free to develop and maintain. Contributing with your wallet can be very effective way of helping the project progress, yet many people don’t even consider this. Small amounts to cover hosting costs, costs of hardware bandwidth etc may make make a whole world of difference to the maintainers of the project. &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Also developing software often requires software which is not free. You want project X to support version Y of Z? Donating money so that the developers can purchase the license of Z can be a great way of contributing. That is especially true on the Microsoft stack, which consists almost entirely of commercial software.&lt;/p&gt;
&lt;p&gt;On the larger scale donating can help someone to start working full time on certain aspects of the project to make what would otherwise be impossible, possible. Probably the most famous example of this is &lt;a target="_blank" href="http://imeta.co.uk/"&gt;IMeta corporation&lt;/a&gt;, which donated one of their &lt;a target="_blank" href="http://blogs.imeta.co.uk/sstrong/Default.aspx"&gt;top developers&lt;/a&gt; to work full time for several months on bringing LINQ support to &lt;a target="_blank" href="http://nhforge.org/Default.aspx"&gt;NHibernate&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://kozmic.pl/aggbug/246.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/kozmic/~4/VBYSVA2W8Lk" height="1" width="1"/&gt;</content><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/246.aspx</wfw:comment><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/commentRss/246.aspx</wfw:commentRss><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://kozmic.pl/services/trackbacks/246.aspx</trackback:ping></entry><entry><title type="text">How to contribute to open source</title><link rel="alternate" type="text/html" href="http://kozmic.pl/archive/2009/09/06/how-to-contribute-to-open-source.aspx" /><updated>2009-08-29T12:01:54-07:00</updated><id>http://kozmic.pl/archive/2009/09/06/how-to-contribute-to-open-source.aspx</id><content type="html">&lt;p&gt;Once in a while I talk to someone who sais something like this: “I love open source, especially project X – I would love to contribute back, but I don’t know how”. There are different variations. “I am not a programmer”, “I don’t understand the code – it’s so vast”, “I wouldn’t know how to submit these patch-things they ask for” etc.&lt;/p&gt;  &lt;p&gt;To help these people I decided to create a small (seriously – 3 posts, excluding this one) series about contributing to open source projects. I hope this will help more people get involved as they realize how much they can help with little effort.&lt;/p&gt;  &lt;p&gt;I intend to do the following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;How to contribute to open source without writing a single line of code &lt;/li&gt;    &lt;li&gt;How to contribute code to open source project without knowing its inner workings &lt;/li&gt;    &lt;li&gt;How to contribute code to open source – reporting bugs and creating patches &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This should cover most of the ground to get people started. The topic itself is certainly much broader, and one could write a whole book about it, but why waste time for that, when there’s some great open source waiting for your contribution!&lt;/p&gt;&lt;img src="http://kozmic.pl/aggbug/247.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/kozmic/~4/79ZiaHCTKP8" height="1" width="1"/&gt;</content><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/247.aspx</wfw:comment><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/commentRss/247.aspx</wfw:commentRss><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://kozmic.pl/services/trackbacks/247.aspx</trackback:ping></entry><entry><title type="text">Thoughts on C# 4.0&amp;rsquo; optional parameters</title><link rel="alternate" type="text/html" href="http://kozmic.pl/archive/2009/09/03/thoughts-on-c-4.0rsquo-optional-parameters.aspx" /><updated>2009-08-29T11:58:55-07:00</updated><id>http://kozmic.pl/archive/2009/09/03/thoughts-on-c-4.0rsquo-optional-parameters.aspx</id><content type="html">&lt;p&gt;C# 4.0 is just round the corner and along with it set of nice new additions to the language, including optional parameters. There’s been some historical resistance to add this feature to the language, but here' it is, and I’m glad it’s coming, or at least I was.&lt;/p&gt;  &lt;p&gt;In few words, optional parameters, have their default value specified in the signature of the method. You can then skip them when calling method, and the method will be called with their default values.&lt;/p&gt;  &lt;h3&gt;So, what’s the deal?&lt;/h3&gt;  &lt;blockquote&gt;   &lt;p&gt;To simplify the current discussion I will refer to the method containing default parameters (Foo in this example) as &lt;em&gt;called method&lt;/em&gt;, and to method providing the default value (DateTime.Now getter in the example few paragraphs below) as &lt;em&gt;value provider&lt;/em&gt;.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Take a look at the code below. Method Foo has two parameters, but we can call them as if it had none.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultParameters_0.png" rel="lightbox"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="defaultParameters_0" border="0" alt="defaultParameters_0" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultParameters_0_thumb.png" width="479" height="320" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Looking good so far, right? Let’s take a look at how Main method looks like in Reflector.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultArguments_1.png" rel="lightbox"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="defaultArguments_1" border="0" alt="defaultArguments_1" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultArguments_1_thumb.png" width="276" height="104" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As we can see there’s no magic here – simple compiler trick. Compiler binds the invocation to the method, and them puts the arguments for you. The code looks as if you had written it yourself in earlier version of C#.&lt;/p&gt;  &lt;p&gt;Still good, right? So how does exactly the compiler knows what to put on the calling side? Let’s take a look at the compiled signature of the method Foo.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultParameters_2.png" rel="lightbox"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="defaultParameters_2" border="0" alt="defaultParameters_2" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultParameters_2_thumb.png" width="713" height="107" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Ha! The values are encoded in DefaultParameterValue&lt;strong&gt;Attribute&lt;/strong&gt;. Do you see the problem here? No? Than let’s try something else. Let’s change the signature of the method to take DateTime instead of int.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultParameters_3.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="defaultParameters_3" border="0" alt="defaultParameters_3" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultParameters_3_thumb.png" width="603" height="182" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Notice we initialize the time to default value of DateTime.Now. All good? So let’s compile the code, shall we?&lt;/p&gt;  &lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultParameters_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="defaultParameters_4" border="0" alt="defaultParameters_4" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultParameters_4_thumb.png" width="436" height="131" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h3&gt;Oops.&lt;/h3&gt;  &lt;p&gt;Turns out we can’t. What seems like a really reasonable code is not allowed. Since the default values for arguments are being kept in attributes they must be a compile time constants, which includes primitive types (numeric types and strings), tokens ( typeof, methodof, which is not exposed in C# though ) and nulls. Pretty disappointing right?  This means no new Foo(), no Foo.Bar is allowed. This dramatically limits the range of scenarios where this feature can be used, as most of the time, you’ll want not null, but something else as your default value, in which case you’ll end up creating overloads anyway.&lt;/p&gt;  &lt;p&gt;There are some workarounds, like the one described in &lt;a href="http://kozmic.pl/archive/2009/08/27/book-review-c-in-depth-2nd-edition.aspx" target="_blank"&gt;this book&lt;/a&gt;, but they have their own downsides, and it’s not always possible to use them anyway.&lt;/p&gt;  &lt;p&gt;This all makes me think – why did the authors of the language decided to provide such crippled implementation of the feature? I’m not an expert in this matter, but I found a simple way in which the feature could be implemented allowing for far greater range of scenarios.&lt;/p&gt;  &lt;h3&gt;What if…&lt;/h3&gt;  &lt;p&gt;Let’s re-examine how this feature works.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The compiler puts the default values of the arguments in a special attribute type on the &lt;em&gt;called method&lt;/em&gt; signature. &lt;/li&gt;    &lt;li&gt;On the calling side, the compiler reads the values, and puts these of them that were not provided explicitly. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;All the work happens at compile time (let’s ignore the dynamic for a moment, we’ll get to that as well) so no additional work at runtime is required. Since the compiler is very powerful, why not go one step further.&lt;/p&gt;  &lt;p&gt;I think by simply extending this approach the following scenarios could be enabled.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;using value returned by static parameterless method (this includes static property getters) as default value. &lt;/li&gt;    &lt;li&gt;using value returned by instance parameterless method defined on the same type (or base type of the type) as &lt;em&gt;called method &lt;/em&gt;is defined on (this would only be applicable for instance methods). &lt;/li&gt;    &lt;li&gt;using default, parameterless constructors as default value. &lt;/li&gt;    &lt;li&gt;or if we wanted to extend it further: using value returning by any variant of the above that does take parameters that are allowed to be put in the attribute (including both constants, and values of other parameters). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Let us examine how this could be (I think) achieved. &lt;/p&gt;  &lt;h3&gt;The spoon does not exist&lt;/h3&gt;  &lt;p&gt;What we would need is a way to store information which method, or constructor of which type we want to invoke in case no default value is provided. Since tokens are legal in attributes, the existing approach could be extended with something like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultParameters_5.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="defaultParameters_5" border="0" alt="defaultParameters_5" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/ThoughtsonC4.0optionalparameters_10BF1/defaultParameters_5_thumb.png" width="757" height="109" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;We have a way of storing the method. Now, the compiler could easily retrieve the token and invoke the &lt;em&gt;called method&lt;/em&gt;.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;If &lt;em&gt;value provider &lt;/em&gt;is static there’s no problem – just call it, regardless of whether &lt;em&gt;called method &lt;/em&gt;is an instance or static method. &lt;/li&gt;    &lt;li&gt;If value provider is instance method, and called method is instance method as well, invoke the &lt;em&gt;value provider &lt;/em&gt;on the instance on which &lt;em&gt;called method &lt;/em&gt;is being invoked (hence the requirement that &lt;em&gt;value provider &lt;/em&gt;must be declared on the same type as &lt;em&gt;called method &lt;/em&gt;or its base type). &lt;/li&gt;    &lt;li&gt;If &lt;em&gt;value provider &lt;/em&gt;is instance method but &lt;em&gt;called method &lt;/em&gt;is static do not allow (at compile time!) this code to compile, since there’s no instance to call the &lt;em&gt;value provider &lt;/em&gt;on. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;When it comes to constructors it is even simpler – since we allow only default constructor, at compile time we would check if the type does indeed have default, accessible constructor, and disallow the code to compile otherwise. Then when the called method is being invoked, retrieve the type token and call the default constructor on that type.&lt;/p&gt;  &lt;h3&gt;What about dynamic code?&lt;/h3&gt;  &lt;p&gt;I don’t have very intimate knowledge of dynamic code yet, but I think this could work with &lt;em&gt;dynamic &lt;/em&gt;code as well. The compiler would put all the information on the call site (new concept introduced in .NET 4.0) and this would be all we need. In C# 1.0 having MethodInfo of a method you could invoke it. Same having System.Type it is easy to find and invoke it’s default constructor using little bit of reflection. I really see no reason why this would not work.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;What do you think – am I missing part of the picture – is it something terribly wrong with my idea that would restrain it from working? Or maybe C# team didn’t really want (as they used to) implement this feature so they provided only the minimal implementation they needed for other major features, COM interop specifically.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:b7e7628e-1ebf-4b1d-8b2c-6bbbdd9746fb" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;&lt;/div&gt;&lt;img src="http://kozmic.pl/aggbug/243.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/kozmic/~4/gARSXTOll30" height="1" width="1"/&gt;</content><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/243.aspx</wfw:comment><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/commentRss/243.aspx</wfw:commentRss><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://kozmic.pl/services/trackbacks/243.aspx</trackback:ping></entry><entry><title type="text">Book review &amp;ndash; Debug It!: Find, Repair, and Prevent Bugs in Your Code</title><link rel="alternate" type="text/html" href="http://kozmic.pl/archive/2009/08/30/book-review-ndash-debug-it-find-repair-and-prevent-bugs-again.aspx" /><updated>2009-08-30T02:00:00-07:00</updated><id>http://kozmic.pl/archive/2009/08/30/book-review-ndash-debug-it-find-repair-and-prevent-bugs-again.aspx</id><content type="html">&lt;p&gt;&lt;a href="http://www.amazon.com/Debug-Find-Repair-Prevent-Bugs/dp/193435628X%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dkrzkomicsblo-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D193435628X" target="_blank"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="41wUoTodBVL._SL160_[1]" border="0" alt="41wUoTodBVL._SL160_[1]" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/BookreviewDebugItFindRepairandPreventBug_10775/41wUoTodBVL._SL160_1.jpg" width="134" height="160" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I bought &lt;a href="http://www.amazon.com/Release-Production-Ready-Software-Pragmatic-Programmers/dp/0978739213%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dkrzkomicsblo-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0978739213" target="_blank"&gt;Release it!&lt;/a&gt; some time ago, and it was a really good book. Now Pragmatic Programmer released a new book in their ‘do it!’ series – &lt;a href="http://www.amazon.com/Debug-Find-Repair-Prevent-Bugs/dp/193435628X%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Dkrzkomicsblo-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D193435628X" target="_blank"&gt;Debug it!&lt;/a&gt;, so when I got a change to snap a copy, I did, and I’m here to let you know that it is a remarkable book.&lt;/p&gt;  &lt;p&gt;First of all, I think the title might be a little misleading for some people. It is about debugging in a broader sense than most people define it. If for you debugging equates dealing with debugger, you won’t find much of that in this book. The book talks about debugging as in getting the bugs out of your software (and doing your best to make sure they won’t come back).&lt;/p&gt;  &lt;p&gt;The book is divided into three parts:&lt;/p&gt;  &lt;p&gt;The first part, which accounts for almost half of the book, talks in great detail about four stages of dealing with bugs in your code – reproduction, diagnostics, actual fix and reflecting over reasons why the bug was there in the first place. There is not much earth shattering here, and when reading this part you’ll (hopefully) think to yourself – “&lt;em&gt;yeah, I knew this&lt;/em&gt;” and “&lt;em&gt;sure, it makes perfect sense&lt;/em&gt;”. The fact that this all common sense, and rules of thumb are gathered in one place though, and backed up by concrete discussion and good anecdotes helps you organize your workflow when dealing with misbehaving code. The fact that what you felt under your skin is said out loud makes you stop and realize there’s deeper meaning to all this.&lt;/p&gt;  &lt;p&gt;Second part is relatively small, but concentrates on very important topic – working with people. That includes both, working with QA department and users as well as working with bug tracking systems. It discusses what makes a good bug report, what it should contain, what it should not contain etc. This part was a revelation, and if I could I would make it mandatory to read for all my clients, and people submitting bugs to OSS projects I contribute to. Then it moves to pragmatic approach towards working with bugs (hey, it’s a Pragmatic Bookshelf book after all). It talks about perfectionist attitude of bug-free software, and working with software that is riddled with bugs, which was interesting to read in the context of &lt;a href="http://devlicio.us/blogs/tim_barcz/archive/2009/08/26/zero-defects-in-software-setting-a-higher-bar.aspx" target="_blank"&gt;Tim’s post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The final part, is a loosely connected group of topics, though it is as valuable as the previous ones. It talks about special cases, like service releases, about building the ideal debugging environment, which I found particularly interesting as this is topic close to my heart, although I would disagree with the author, who discourages extensive use of branching is source control. We also get a chapter on how to make software easy to debug. The book ends with list of anti-patterns along with discussion about how to remedy them.&lt;/p&gt;  &lt;p&gt;I know that this book influenced the way I work now, and there aren’t many books I could say something like this about. It discusses extremely important part of software engineer’s profession, and does it very well. If you call yourself a pragmatic programmer I think you should read this book.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:59fa8a96-f24e-4b13-8f51-3a47bd208a61" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Book" rel="tag"&gt;Book&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Review" rel="tag"&gt;Review&lt;/a&gt;&lt;/div&gt;&lt;img src="http://kozmic.pl/aggbug/249.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/kozmic/~4/_D2fNjdfTtQ" height="1" width="1"/&gt;</content><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/249.aspx</wfw:comment><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/commentRss/249.aspx</wfw:commentRss><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://kozmic.pl/services/trackbacks/249.aspx</trackback:ping></entry><entry><title type="text">Castle blogs &amp;ndash; RSS feed</title><link rel="alternate" type="text/html" href="http://kozmic.pl/archive/2009/08/29/castle-blogs-ndash-rss-feed.aspx" /><updated>2009-08-29T06:09:46-07:00</updated><id>http://kozmic.pl/archive/2009/08/29/castle-blogs-ndash-rss-feed.aspx</id><content type="html">&lt;p&gt;One thing I don’t think I articulated clearly enough when announcing &lt;a href="http://kozmic.pl/archive/2009/08/13/castle-blogs-aggregator.aspx" target="_blank"&gt;Castle Blog Aggregator&lt;/a&gt;, was that it comes with RSS feed.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/CastleblogsRSSfeed_B945/castle_blogs.png" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="castle_blogs" border="0" alt="castle_blogs" src="http://kozmic.pl/Images/kozmic_pl/WindowsLiveWriter/CastleblogsRSSfeed_B945/castle_blogs_thumb.png" width="436" height="95" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You may have noticed an RSS icon in your browser when browsing the aggregator site, but in case you didn’t &lt;a href="http://pipes.yahoo.com/pipes/pipe.run?_id=bGjr2c1s3hGi5qx20EypaA&amp;amp;_render=rss" target="_blank"&gt;here’s the direct link to the feed&lt;/a&gt; that you can use in your favorite feed reader.&lt;/p&gt;  &lt;p&gt;We’re also open to include other blogs in the feed, so if you blog about Castle, or know any good blogs about Castle project, let us know, either in the comments, or create a suggestion on &lt;a href="http://castle.uservoice.com/pages/16605-official-castle-project-feedback-forum" target="_blank"&gt;Castle UserVoice page&lt;/a&gt;.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e48e58b5-05c8-441b-9497-d1eb16465c9f" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Castle" rel="tag"&gt;Castle&lt;/a&gt;&lt;/div&gt;&lt;img src="http://kozmic.pl/aggbug/245.aspx" width="1" height="1" /&gt;&lt;img src="http://feeds.feedburner.com/~r/kozmic/~4/iwJUJDRv-nc" height="1" width="1"/&gt;</content><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/245.aspx</wfw:comment><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kozmic.pl/comments/commentRss/245.aspx</wfw:commentRss><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://kozmic.pl/services/trackbacks/245.aspx</trackback:ping></entry></feed>
