<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>Blog</title>
	<link>http://blog.roeppischer.com</link>
	<description></description>
	<pubDate>Tue, 30 Jun 2009 13:47:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>
	<language>en</language>
			<item>
		<title>Using Proxies: Proxies using Castle.DynamicProxy2</title>
		<link>http://blog.roeppischer.com/2009/06/30/using-proxies-proxies-using-castledynamicproxy2/</link>
		<comments>http://blog.roeppischer.com/2009/06/30/using-proxies-proxies-using-castledynamicproxy2/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 10:35:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[DynamicProxy]]></category>

		<category><![CDATA[Castle]]></category>

		<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://blog.roeppischer.com/2009/06/30/using-proxies-proxies-using-castledynamicproxy2/</guid>
		<description><![CDATA[At the end of the last blog I mentioned, that it is not useful to write proxies by hand. The reason
for this is very clear&#8230;many unnecessary work, error-prone, not easy to maintain if it comes to
changes in the interface etc. Therefore it would be nice, if we could automate that process.
The Castle project contains a [...]]]></description>
			<content:encoded><![CDATA[<p align="left">At the end of the last blog I mentioned, that it is not useful to write proxies by hand. The reason<br />
for this is very clear&#8230;many unnecessary work, error-prone, not easy to maintain if it comes to<br />
changes in the interface etc. Therefore it would be nice, if we could automate that process.</p>
<p align="left"><a href="http://www.castleproject.org" target="_blank">The Castle project</a> contains a library called DynamicProxy. As the name says, the aim of this<br />
library is to provide functionality to create dynamic proxies at runtime. The problem is that the<br />
documentation for this is not very complete and  examples are rare or do not fit the needs<br />
for beginners. Therefore I want to write here what I&#8217;ve learned so far. For  this we&#8217;ll stick to the<br />
simple<strong> ITask</strong> example from the last blog.</p>
<p align="left">So, let&#8217;s have a look, how this could be helpful, to avoid hand written proxies. The first question<br />
that arises is, how can a proxy be constructed at runtime?  This is done by using the classes<br />
provided by the System.CodeDom and System.Reflection namespaces of the .net framework.<br />
We&#8217;ll leave it to this, since the details of the implementation aren&#8217;t really interesting, when using<br />
the library. The only interesting thing right now is, that we can build an proxy object for any<br />
existing class. This is done with the</p>
<p align="left"><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_generator.jpg" title="proxy_generator.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_generator.jpg" alt="proxy_generator.jpg" /></a></p>
<p align="left">class. But before we go further, there is one big thing to think about. In the <strong>TaskProxy</strong> from<br />
the last blog, we have implemented our own version of the <em>doExecute()</em> method, where we<br />
checked, if a user is allowed to execute a task or not. If we now have a dynamicly constructed<br />
proxy, generated by the <strong>ProxyGenerator</strong>, how can we provide our own implementation for it?<br />
There must be a way to step in the execution process of the method. Without this possibility<br />
everything else makes no sense. So thats why the methods from the <strong>ProxyGenerator</strong> want<br />
some additional parameters:</p>
<p align="left"><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_creatinterface_param2.jpg" title="proxy_creatinterface_param2.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_creatinterface_param2.jpg" alt="proxy_creatinterface_param2.jpg" /></a></p>
<p align="left">We have to provide one (or more) objects that implements the <strong>IInterceptor </strong>interface.  This<br />
interface is defined in the <strong>Castle.Core.Interceptor</strong> namespace and is exactly what we are<br />
looking for, because this is the place, where our own proxy implementation lifes. To achieve<br />
this, we only have to implement one single method:</p>
<p align="left"> <a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_interceptor.jpg" title="proxy_interceptor.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_interceptor.jpg" alt="proxy_interceptor.jpg" /></a></p>
<p align="left">As parameter an <strong>IInvocation</strong> - object is provided. This gives us some importand informations<br />
about the current execution. Since we don&#8217;t override a specific method, we have to know wich<br />
method is currently executed and what parameters are stated to the call. Furthermore we need<br />
access to the real <strong>TaskImpl</strong> object and maybe have to set return values etc. All this could be<br />
done using the <em>invocation</em> properties. The definition looks like this:</p>
<p align="left"><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_invocation.jpg" title="proxy_invocation.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_invocation.jpg" alt="proxy_invocation.jpg" /></a></p>
<p align="left">Beside this we can see a call to a <strong>Proceed()</strong> method. With this call we simply allow the proxy to<br />
continue the actual called method.</p>
<p align="left">We can run our test from the last blog now. Let&#8217;s stop after the proxy was generated. We can see<br />
the following result:</p>
<p align="left"><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_class_proxy1.jpg" title="proxy_class_proxy1.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_class_proxy1.jpg" alt="proxy_class_proxy1.jpg" /></a></p>
<p align="left">As we can notice, the returned object is a randomly generated typename for the proxy.<br />
Let&#8217;s make another breakpoint at the <em>Intercept</em> method. What happens?<br />
To our disappointment nothing really happens <img src='http://blog.roeppischer.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> So what&#8217;s wrong here? The answer to this is<br />
simple. As mentioned above, the proxy is generated using System.CodeDom. That means a new<br />
type is build in memory with implementations for all methods and properties of the base class,<br />
wich is <strong>TaskImpl</strong> in our case. The so created proxy class inherits from <strong>TaskImpl</strong> and tries<br />
to override the methods to allow interception. Doing this, the same rules are effective as when<br />
we would do this by hand.  This means to override a method in an inherited class, the method<br />
has to be <strong>virtual </strong>in the base class. So let&#8217;s change our <strong>TaskImpl</strong> class and try it again:</p>
<p align="left"> <a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_interceptor2.jpg" title="proxy_interceptor2.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_interceptor2.jpg" alt="proxy_interceptor2.jpg" /></a></p>
<p align="left">That&#8217;s better. The call to <em>invocation.Proceed()</em> now simply continues the <em>doExecute()</em><br />
method of our <strong>TaskImpl</strong> class. Putting it all together, we can now implement our proxy<br />
behaviour, as we did in the last blog:</p>
<p align="left"><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_interceptor_final1.jpg" title="proxy_interceptor_final1.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_interceptor_final1.jpg" alt="proxy_interceptor_final1.jpg" /></a></p>
<p align="left">The <strong>TaskProxy</strong> is no longer needed and our tests run succesfully:</p>
<p align="left"> <a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_interceptor_test1.jpg" title="proxy_interceptor_test1.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_interceptor_test1.jpg" alt="proxy_interceptor_test1.jpg" /></a></p>
<p align="left">In the end this is a quick overview of how to use DynamicProxies. There is a lot more to<br />
talk about, but I think it is agood starting point for using the library and to understand, how<br />
proxies are generated and what one can do with it.</p>
<p align="left"> To see exactly what happens behind the scene, you can make the proxy persistent and have<br />
a look at it using .NET Reflector. To do this, simply call:</p>
<p align="left"> <a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_persistent.jpg" title="proxy_persistent.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_persistent.jpg" alt="proxy_persistent.jpg" /></a></p>
<p align="left">Now open the resulting assembly in the .NET Reflector. Here you can see the generated class<br />
for the proxy and what is done to the <em>doExecute()</em> method.</p>
<p align="left"><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_reflector.jpg" title="proxy_reflector.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_reflector.jpg" alt="proxy_reflector.jpg" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roeppischer.com/2009/06/30/using-proxies-proxies-using-castledynamicproxy2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using Proxies: What is a proxy and why use it?</title>
		<link>http://blog.roeppischer.com/2009/06/29/using-proxies-1-what-is-a-proxy-an-why-use-it/</link>
		<comments>http://blog.roeppischer.com/2009/06/29/using-proxies-1-what-is-a-proxy-an-why-use-it/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 20:21:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[DynamicProxy]]></category>

		<category><![CDATA[Castle]]></category>

		<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://blog.roeppischer.com/2009/06/29/using-proxies-1-what-is-a-proxy-an-why-use-it/</guid>
		<description><![CDATA[As I started dealing with NHibernate and other OS projects in the .net world, I wondered how
often the use of proxies where mentioned. So I started out reading a bit around and in the end it
becomes clear. I think everybody has used the pattern behind it sometimes, even if one didn&#8217;t
know about it. The following [...]]]></description>
			<content:encoded><![CDATA[<p>As I started dealing with NHibernate and other OS projects in the .net world, I wondered how<br />
often the use of proxies where mentioned. So I started out reading a bit around and in the end it<br />
becomes clear. I think everybody has used the pattern behind it sometimes, even if one didn&#8217;t<br />
know about it. The following example shows the principle usage of the proxy pattern and what<br />
it is good for.</p>
<p>&#8220;&#8230;A proxy, in its most general form, is a class functioning as an interface to something else&#8230;&#8221;</p>
<p>In practice this short definition means, a proxy encloses a real object in its entirety but presents<br />
the same signature to the client. So a client must not know if he works with the proxy or<br />
the real object. This is very usefull in a lot of cases, since you build an interface to a class and<br />
can extend the functionallity and  behaviour of it, without changing it&#8217;s original implementation.</p>
<p><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy12.jpg" title="proxy12.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy12.jpg" alt="proxy12.jpg" /></a></p>
<p>The example above shows an proxy that implements the <strong>ITask</strong> interface and delegates all calls<br />
to one of the interfaces methods to an internal instance of a <strong>TaskImpl</strong> - object.</p>
<p>The factory:<br />
<a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_factory.jpg" title="proxy_factory.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_factory.jpg" alt="proxy_factory.jpg" /></a></p>
<p>The client:<br />
<a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_create_and_execute.jpg" title="proxy_create_and_execute.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_create_and_execute.jpg" alt="proxy_create_and_execute.jpg" /></a></p>
<p>Ok, the proxy hides the real implementation, but furthermore it does nothing real useful. If we start<br />
a task, it is simply executed. But what about for instance permissions and dependencies? Not every<br />
user can execute every task, I think this is common for every application. So where can we validate<br />
if the user is allowed to execute a specific task? The <strong>TaskImpl</strong> is not a good place for this I think,<br />
since there only the execution logic should be implemented.  I would prefer implementing this in the<br />
proxy. Since every <strong>ITask</strong> - object is created by a factory, it is easy to interfere the process of creating<br />
an <strong>ITask</strong> instance and put the proxy on job.</p>
<p>We use a simple <strong>User</strong> class to emulate the permission validation:</p>
<p><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_user.jpg" title="proxy_user.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_user.jpg" alt="proxy_user.jpg" /></a></p>
<p>The <strong>TaskImpl</strong> stays untouched, but the proxy implementation of <em>doExecute</em> now looks like this:</p>
<p><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_impl_user.jpg" title="proxy_impl_user.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_impl_user.jpg" alt="proxy_impl_user.jpg" /></a></p>
<p>The task will be only executed, if the User has the permission to do this. Instead of throwing an<br />
exception it also could simply return <strong>false</strong>.</p>
<p>Now we can run two simple tests with it:</p>
<p><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_test1.jpg" title="proxy_test1.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_test1.jpg" alt="proxy_test1.jpg" /></a></p>
<p>As we can see, all test are successful:</p>
<p><a href="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_test_result1.jpg" title="proxy_test_result1.jpg"><img src="http://blog.roeppischer.com/__oneclick_uploads/2009/06/proxy_test_result1.jpg" alt="proxy_test_result1.jpg" /></a></p>
<p>So finally this simple example shows, what problems can be solved using proxies. As we can see,<br />
the client stays completly untouched and the <strong>TaskImpl</strong> too.</p>
<p>But thinking of real world applications, interfaces and classes are useally complex an have lots of<br />
properties and methods. It would be worse to code proxies for it by hand. In the end there is no need<br />
to do this, because there a libraries out there, doing the job very good. But thats for the next part&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roeppischer.com/2009/06/29/using-proxies-1-what-is-a-proxy-an-why-use-it/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Let&#8217;s start&#8230;</title>
		<link>http://blog.roeppischer.com/2009/06/29/lets-start/</link>
		<comments>http://blog.roeppischer.com/2009/06/29/lets-start/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 13:49:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.roeppischer.com/2009/06/29/lets-start/</guid>
		<description><![CDATA[Over the last couple of week&#8217;s I started to dig a little deeper in the .NET world then before.
Until now I had no opportunity to do a bigger project using .net and C#. So i only looked
around from time to time to see whats new and to try out some things for myself. Now at
last [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last couple of week&#8217;s I started to dig a little deeper in the .NET world then before.<br />
Until now I had no opportunity to do a bigger project using .net and C#. So i only looked<br />
around from time to time to see whats new and to try out some things for myself. Now at<br />
last our company decided to do all applications completly new, using C#. In this blog<br />
I want to write about my experiences doing this. One aim is to eventually provide some<br />
useful informations to others. On the other hand I&#8217;m looking forward to learn something<br />
from comments, if one of my ideas or thoughts aren&#8217;t that good or right <img src='http://blog.roeppischer.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roeppischer.com/2009/06/29/lets-start/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

