<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8151663570574264569</id><updated>2024-11-08T07:42:36.218-08:00</updated><category term="C#"/><category term=".NET"/><category term="Design Patterns"/><category term="General programming"/><category term="ASP.NET MVC"/><category term="WCF"/><category term="ADO.NET"/><category term="Castle DynamicProxy"/><category term="DDD"/><category term="Fluent interface"/><category term="JavaScript"/><category term="Silverlight"/><title type='text'>Reducing Complexity</title><subtitle type='html'>It Depends...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>19</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-3563249880206467106</id><published>2010-03-03T07:47:00.001-08:00</published><updated>2011-03-22T16:17:08.067-07:00</updated><title type='text'>Use before Reuse</title><content type='html'>&lt;p&gt;I have been doing a lot of software design work in past months. As you know design is not easy. It’s almost always about making (hard) decisions. It’s about considering alternatives and weighting options. In that sense, I’ve also tried to learn more and expand my knowledge about software architecture and software design. As the main source of usable knowledge, I find the “Domain-driven design” book. You’ve probably heard of the Repositories, Aggregates, Value object etc. But, what I value the most in this book is the part called “Strategic design”. It’s the part that talks about Bounded Contexts, Context Maps, Core domain etc. Really, this is the most valuable part of the book. Eric Evans, himself, in his &lt;a href=&quot;http://www.infoq.com/presentations/ddd-eric-evans&quot; target=&quot;_blank&quot;&gt;What I’ve learned about DDD since the book talk&lt;/a&gt; kind of admits that putting that part at the end of the book was a mistake. If you do software architecture and design I strongly advise you to explore that part of the book. However there is one advice-practice, that changed the my thinking from ground up. I have read &lt;a href=&quot;http://blogs.stickyminds.com/Blogs/tabid/91/EntryId/170/Simplicity-Before-Generality-Use-Before-Reuse.aspx&quot; target=&quot;_blank&quot;&gt;this article&lt;/a&gt; by Kevlin Henney. The title of the article is “&lt;em&gt;Simplicity Before Generality, Use Before Reuse&lt;/em&gt;”. The article is about how to value concrete and specific solutions over the more general solutions. Business people mostly have problems that are specific to them and thus need specific solutions. Software people, on the other hand, like to generalize. They (we) like to see abstractions everywhere and try to come up with general solutions that can solve all the problems. Of course, this will not happen. This usually end by making things more complex without any benefit. Go and read this article. &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/3563249880206467106/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/03/use-before-reuse.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/3563249880206467106'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/3563249880206467106'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/03/use-before-reuse.html' title='Use before Reuse'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-8040620171792929279</id><published>2010-02-16T11:03:00.001-08:00</published><updated>2010-03-23T11:10:42.684-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="WCF"/><title type='text'>How to use Custom RoleProvider in WCF?</title><content type='html'>&lt;p&gt;First thing is to add reference to the &lt;em&gt;System.Web.dll&lt;/em&gt; assembly to your WCF project. Then create a class which inherits from &lt;em&gt;RoleProvider. &lt;/em&gt;You’ll also need to add the System.Web.Security namespace.&lt;em&gt;&amp;#160;&lt;/em&gt;Then you can use the CTRL+dot trick so that VS generates all the methods for you. You’ll end up with something like this: &lt;/p&gt;  &lt;div class=&quot;csharpcode&quot;&gt;   &lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MyRoleProvider : RoleProvider&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; AddUsersToRoles(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] usernames, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] roleNames)&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; ApplicationName&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        get&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;            &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        set&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;            &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; CreateRole(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; roleName)&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; DeleteRole(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; roleName, &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; throwOnPopulatedRole)&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] FindUsersInRole(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; roleName, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; usernameToMatch)&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] GetAllRoles()&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] GetRolesForUser(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; username)&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] GetUsersInRole(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; roleName)&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; IsUserInRole(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; username, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; roleName)&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; RemoveUsersFromRoles(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] usernames, &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] roleNames)&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; RoleExists(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; roleName)&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;Now, you’ll need to provide actual implementation for all of these methods. That’s up to you. &lt;/p&gt;

&lt;p&gt;Next, you need to configure your config file. Add following to the system.web section of your configuration file:&lt;/p&gt;

&lt;div class=&quot;csharpcode&quot;&gt;
  &lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;system.web&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;roleManager&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;providers&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;            &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;add&lt;/span&gt;  &lt;span class=&quot;attr&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&amp;quot;MyRoleProvider&amp;quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&amp;quot;ReducingComplexity.MyRoleProvider, ReducingComplexityRoleProvider&amp;quot;&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;providers&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;roleManager&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;system.web&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;Now you’ll need to link this role provider to your services services. You will use &lt;em&gt;serviceBehavior &lt;/em&gt;for this. For example, if you have service behavior defined like this: &lt;/p&gt;

&lt;div class=&quot;csharpcode&quot;&gt;
  &lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;behaviors&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;serviceBehaviors&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;behavior&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&amp;quot;MyServis.MyStandardBehavior&amp;quot;&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;serviceAuthorization&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;roleProviderName&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&amp;quot;MyRoleProvider&amp;quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;principalPermissionMode&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&amp;quot;UseAspNetRoles&amp;quot;&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;serviceAuthorization&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;behavior&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;serviceBehaviors&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;behaviors&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;Using the &amp;lt;serviceAuthorization&amp;gt;, I set &lt;em&gt;roleProviderName&lt;/em&gt; with the name of my role provider as defined under &amp;lt;roleManager&amp;gt; node. Also, &lt;em&gt;principalPermissionMode &lt;/em&gt;need to be set to &lt;strong&gt;&lt;em&gt;UseAspNetRoles.&lt;/em&gt;&lt;/strong&gt; That’s pretty much it. It’s that simple.&lt;/p&gt;

&lt;p&gt;Now it’s possible to use both declarative security framework: &lt;/p&gt;

&lt;div class=&quot;csharpcode&quot;&gt;
  &lt;pre class=&quot;alt&quot;&gt;[PrincipalPermission(SecurityAction.Demand,Role = &lt;span class=&quot;str&quot;&gt;&amp;quot;Customer&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;List&amp;lt;Order&amp;gt;&lt;/span&gt; GetOrders(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; customerId)&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;{&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;or for example &lt;em&gt;Thread.CurrentPrincipal.IsInRole()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div class=&quot;csharpcode&quot;&gt;
  &lt;pre class=&quot;alt&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; List&amp;lt;Order&amp;gt; GetOrders(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; customerId)&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (Thread.CurrentPrincipal.IsInRole(&lt;span class=&quot;str&quot;&gt;&amp;quot;Customer&amp;quot;&lt;/span&gt;)) &lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        &lt;span class=&quot;rem&quot;&gt;// get customer orders &lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;        &lt;span class=&quot;rem&quot;&gt;// permission denied&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class=&quot;alt&quot;&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;WCF rocks!!!&lt;/p&gt;

&lt;p&gt;Still, there is something I really don’t like. It is the fact that RoleProvider is considered to belong to ASP.NET(hence the need to add reference to System.Web.dll assembly). This is kind of unnatural, and I would like to see the role providers moved to some other assembly, for example System.Security.dll. &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/8040620171792929279/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/how-to-use-custom-roleprovider-in-wcf.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/8040620171792929279'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/8040620171792929279'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/how-to-use-custom-roleprovider-in-wcf.html' title='How to use Custom RoleProvider in WCF?'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-8874206288733259770</id><published>2010-02-15T07:10:00.001-08:00</published><updated>2010-02-15T12:41:03.861-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Castle DynamicProxy"/><category scheme="http://www.blogger.com/atom/ns#" term="WCF"/><title type='text'>WCF and AOP with Castle.DynamicProxy</title><content type='html'>&lt;p&gt;Managing logging and exception handling in WCF applications can be very time-consuming. Cross-cutting concerns can become a great burden and generate a lot of work for us. Today, I played a little bit with AOP and Castle Dynamic Proxy (DP) library. If you’re not familiar with AOP please take a look at &lt;a href=&quot;http://en.wikipedia.org/wiki/Aspect-oriented_programming&quot; target=&quot;_blank&quot;&gt;this&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;Before I even started with DP, I also considered &lt;a href=&quot;http://www.postsharp.org/&quot; target=&quot;_blank&quot;&gt;PostSharp&lt;/a&gt;, but eventually I decided to go with DP. Reason: I just don’t like things integrated into VS and doing stuff I cannot fully control. It can be that this is not a legitimate reason, but still:).&lt;/p&gt;  &lt;p&gt;I started by adding references to Castle.Core.dll and Castle.DynamicProxy2.dll files. Then I defined my &lt;em&gt;Logging Aspect&lt;/em&gt; class&lt;/p&gt; &lt;style type=&quot;text/css&quot;&gt;








.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;  &lt;p&gt;&lt;/p&gt;  &lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; LoggingAspect : IInterceptor
{
    &lt;span class=&quot;preproc&quot;&gt;#region&lt;/span&gt; IInterceptor Members

    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Intercept(IInvocation invocation)
    {
        Debug.WriteLine(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;.Format(&lt;span class=&quot;str&quot;&gt;&amp;quot;The user {0} is calling method {1}.&amp;quot;&lt;/span&gt;, OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name, invocation.Method.Name));
        Debug.WriteLine(&lt;span class=&quot;str&quot;&gt;&amp;quot;Parameters are : &amp;quot;&lt;/span&gt;);
        &lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; argument &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; invocation.Arguments)
        {
            Debug.WriteLine(&lt;span class=&quot;str&quot;&gt;&amp;quot;Parameter value : &amp;quot;&lt;/span&gt; + Convert.ToString(argument));
        }
        invocation.Proceed();
        Debug.WriteLine(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;.Format(&lt;span class=&quot;str&quot;&gt;&amp;quot;The calling is complete!&amp;quot;&lt;/span&gt;));
    }

    &lt;span class=&quot;preproc&quot;&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;

&lt;p&gt;This code is pretty straightforward. By implementing &lt;em&gt;IInterceptor&lt;/em&gt; interface, I print out username of the calling user, the method called, and parameter values. You’ll probably want to write these info into a database or a file in your project. However, this will stands good as an example. Then, to create a proxy for a type: &lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;ProxyGenerator pg = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ProxyGenerator();
var loggingAspect = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; LoggingAspect();
var proxy = pg.CreateClassProxy(&lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(MyType), loggingAspect);&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;





.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;That is all I need to do regarding DP itself. Now, I need to integrate this into WCF. Luckily, WCF is very extensible framework. First, I’ll implement &lt;em&gt;IInstanceProvider &lt;/em&gt;interface, so that I can control object instances creation. The interface is rather simple, consisting of three methods. &lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MyInstanceProvider : IInstanceProvider
{
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; Type Type { get; &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; set; }
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; MyInstanceProvider(Type type)
    {
        &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.Type = type;
    }

    &lt;span class=&quot;preproc&quot;&gt;#region&lt;/span&gt; IInstanceProvider Members

    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; GetInstance(System.ServiceModel.InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
    {
        ProxyGenerator pg = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ProxyGenerator();
        var loggingAspect = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; LoggingAspect();
        var proxy = pg.CreateClassProxy(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.Type, loggingAspect);
        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; proxy;
    }

    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; GetInstance(System.ServiceModel.InstanceContext instanceContext)
    {
        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; GetInstance(instanceContext, &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;);
    }
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; ReleaseInstance(System.ServiceModel.InstanceContext instanceContext, &lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; instance)
    {
        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (instance &lt;span class=&quot;kwrd&quot;&gt;is&lt;/span&gt; IDisposable)
            ((IDisposable)instance).Dispose();
    }

    &lt;span class=&quot;preproc&quot;&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;&lt;style type=&quot;text/css&quot;&gt;




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;Note that I’m using &lt;em&gt;CreateClassProxy&lt;/em&gt; method of ProxyGenerator to create proxies. This requires that methods we want to intercept need to be marked as &lt;em&gt;virtual&lt;/em&gt;. Now that I have my &lt;em&gt;IInstanceProvider&lt;/em&gt; implemented, I need to hook it up to a service. There are several options for this, but I like using attributes for this. I want to be able to mark my service implementation with some attribute so that all methods on that are intercepted in the same way. To do this I’ll need an attribute that implements &lt;em&gt;IServiceBehavior &lt;/em&gt;interface. &lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MyAspectsAttribute : Attribute, IServiceBehavior
{

    &lt;span class=&quot;preproc&quot;&gt;#region&lt;/span&gt; IServiceBehavior Members

    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection&amp;lt;ServiceEndpoint&amp;gt; endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; ApplyDispatchBehavior(ServiceDescription serviceDescription,
        System.ServiceModel.ServiceHostBase serviceHostBase)
    {
        &lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (var dispatcher &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; serviceHostBase.ChannelDispatchers)
        {
            var cd = dispatcher &lt;span class=&quot;kwrd&quot;&gt;as&lt;/span&gt; ChannelDispatcher;

            &lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (var endpoint &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; cd.Endpoints)
            {
                endpoint.DispatchRuntime.InstanceProvider = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; MyInstanceProvider(serviceDescription.ServiceType);
            }
        }
    }

    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    {

    }
    &lt;span class=&quot;preproc&quot;&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;The most important thing here is the &lt;em&gt;ApplyDispatchBehavior &lt;/em&gt;method where I set up the instance provider for each endpoint. Next, the only thing left is to mark my service with &lt;em&gt;MyAspects &lt;/em&gt;attribute. &lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;[MyAspects]
&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MyGreatService : IMyGreatService 
{
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; SayHello()
    {
        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;str&quot;&gt;&amp;quot;Hello&amp;quot;&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;This way, &lt;em&gt;SayHello&lt;/em&gt;() method (and all others marked as virtual) will be intercepted by my &lt;em&gt;LoggingAspect&lt;/em&gt; class thus enabling me to centralize my logging code. &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/8874206288733259770/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/wcf-and-aop-with-castledynamicproxy.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/8874206288733259770'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/8874206288733259770'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/wcf-and-aop-with-castledynamicproxy.html' title='WCF and AOP with Castle.DynamicProxy'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-6654756925147468010</id><published>2010-02-08T11:10:00.001-08:00</published><updated>2010-02-08T11:32:21.750-08:00</updated><title type='text'>Convention over Configuration – the Microsoft way!</title><content type='html'>&lt;p&gt;I like the concept of Convention over Configuration. It really makes sense. I only need to change parts of the configuration that are specific for my case. Here’s the Wikipedia definition of the concept: &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;b&gt;“Convention over Configuration&lt;/b&gt; (also known as &lt;b&gt;Coding by convention&lt;/b&gt;) is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility.” &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;It looks like that at Microsoft, they also liked the Convention over Configuration concept. Take for example the resource files in Visual Studio. Every form has it’s its’s resource file named in format &lt;em&gt;[FormName]&lt;/em&gt;.resx. This is great, but how do I change the .resx file for a form? I only want to change .resx file name, not the form name. I’m missing the &lt;em&gt;Rename &lt;/em&gt;command on the context menu: &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUylC_KAcxaLy6FOUjSzpV43DnxlvnUsroCw2mId_wL99NQPc4uxbABnJc5Nl0U729Hr8plqVGtRRy1eiXZslM-PPfF4dP8N_mKeqWdDoS92U4wcyuNdwXAjue9DDaJ0Alh0VInV4cfpo/s1600-h/image%5B9%5D.png&quot;&gt;&lt;img style=&quot;border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px&quot; title=&quot;.resx file rename&quot; border=&quot;0&quot; alt=&quot;.resx file rename&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7gSqidk0VripED_h0iZ1yu5tGsLXHg6eOXM28ERIWW-2H_aOjJqROfWJZOifk0KZHaNQVmGVusdb9oR0EhOzeCvNNvoWbnzA_gSMHxIiXKfxEMDA3i978ynnLUc9hkkv5XyU6k23f1mI/?imgmax=800&quot; width=&quot;301&quot; height=&quot;374&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;No rename here. OK, let’s try by clicking &lt;em&gt;Properties &lt;/em&gt;on the Form1.resx file, and changing the name there. &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCHtOZg3CdghqtDZA_39LtgTGfQkjaA69hYkT-j8a69L6Y9Om7cIHRaEMUlfkEGdivGbdDWufYLBWBbo3L6tVI6I9D9LsejqO6NMwn4iUuYzeHIHDsjYPVh591nyR2lbqwwQrM1fimKJA/s1600-h/image%5B23%5D.png&quot;&gt;&lt;img style=&quot;border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px&quot; title=&quot;.resx file rename&quot; border=&quot;0&quot; alt=&quot;.resx file rename&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsmmyTY4bMLn79FxlsqWtTQZ6nJrpCGsNwADhxaUT372l0WLZvEZrGlF3KkGLBWC9pqBZ6iVgPVOurmOBNOmk5fQ1AS-gbQk8Jw-ZHOCH_wLkKpNLouClD41JgeVPCbyMeH7zLnMzW5uk/?imgmax=800&quot; width=&quot;286&quot; height=&quot;403&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The filename field is &lt;strong&gt;read-only&lt;/strong&gt;. So there is no way to change the file whatsoever. It seems that Microsoft didn’t quite get the bolded and colored part of the Convention over Configuration definition: &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;b&gt;“Convention over Configuration&lt;/b&gt; (also known as &lt;b&gt;Coding by convention&lt;/b&gt;) is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, &lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;but not necessarily losing flexibility&lt;/font&gt;&lt;/strong&gt;.” &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I don’t have &lt;strong&gt;flexibility &lt;/strong&gt;to change the name of the .resx file for a form. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Why am I telling all of this? &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;If you have ever written something like this: &lt;/p&gt;  &lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MyForm&amp;lt;T&amp;gt;: Form 
{
     ...
}&lt;/pre&gt;

&lt;p&gt;&lt;style type=&quot;text/css&quot;&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;and tried to use resources on your Form, you’ll get whole kind of interesting problems. But there is not much you can do about it. Thanks for reading!&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/6654756925147468010/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/convention-over-configuration-microsoft.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/6654756925147468010'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/6654756925147468010'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/convention-over-configuration-microsoft.html' title='Convention over Configuration – the Microsoft way!'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7gSqidk0VripED_h0iZ1yu5tGsLXHg6eOXM28ERIWW-2H_aOjJqROfWJZOifk0KZHaNQVmGVusdb9oR0EhOzeCvNNvoWbnzA_gSMHxIiXKfxEMDA3i978ynnLUc9hkkv5XyU6k23f1mI/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-7854816345296844491</id><published>2010-02-07T15:32:00.001-08:00</published><updated>2010-02-07T15:37:40.104-08:00</updated><title type='text'>I’m having some hard time with Mono.Cecil</title><content type='html'>&lt;p&gt;Mono.Cecil is great library, no doubt about that. But there are some things that are bugging me. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;No documentation&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This one is serious. A framework without documentation is very hard to use. Usually time is being wasted just to figure out how to do some of the very simple things. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;API is hard to use&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Generally, the API is OK, but it requires a lot of writing. Also, it’s easy to get lost in all of the MemberReferences, MethodReferences, MethodDefinitions, FieldReferences, FieldDefinitions, TypeReferences stuff. Writing IL is not easy at all. Especially when things get a bit complicated. Wouldn’t it be nice if Mono.Cecil had a interpreter/compiler in it. So, we could write C# code that would be translated to IL. Add to that a nice fluent interface:&lt;/p&gt;  &lt;pre class=&quot;csharpcode&quot;&gt;  AssemblyDefinition assembly = AssemblyFactory.GetAssembly(&lt;span class=&quot;str&quot;&gt;&amp;quot;SomeAssembly.dll&amp;quot;&lt;/span&gt;);

            assembly.WithType(&lt;span class=&quot;str&quot;&gt;&amp;quot;MyClass&amp;quot;&lt;/span&gt;)
                    .WithMethod(&lt;span class=&quot;str&quot;&gt;&amp;quot;SomeMethod&amp;quot;&lt;/span&gt;)
                    .AppendCode(&lt;span class=&quot;str&quot;&gt;@&amp;quot;
                                    int x = 5; 
                                    Console.WriteLine(x); 
                                &amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;I guess that I’m asking for too much with the interpreter/complier stuff, but a Mono.Cecil fluent interface would be very convenient.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Testability – unit testing&amp;#160; &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How do you test code that works with Mono.Cecil? Here is my Mono.Cecil development cycle: &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Write code that uses Mono.Cecil &lt;/li&gt;

  &lt;li&gt;Write the dummy assembly to the disk &lt;/li&gt;

  &lt;li&gt;Open the assembly in the Reflector to see if everything is fine. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you can see there is no test automation here whatsoever. The whole process is manual and very time consuming. &lt;/p&gt;

&lt;p&gt;As I said, Mono.Cecil is great but more documentation and an Fluent Interface would make it much more better. If you have any advices on how to overcome these difficulties please let me know. &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/7854816345296844491/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/im-having-some-hard-times-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/7854816345296844491'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/7854816345296844491'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/im-having-some-hard-times-with.html' title='I’m having some hard time with Mono.Cecil'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-628364173992578541</id><published>2010-02-06T16:54:00.001-08:00</published><updated>2010-02-07T05:27:57.014-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><title type='text'>Exploring the MVP Pattern</title><content type='html'>&lt;p&gt;Windows Forms are still number one choice when it comes to development for Windows platform. Windows Forms are easy and proven. In most cases you use things out-of-the-box. If you need something special, you have the ability to make custom controls work the way you want. For most enterprise applications I would dare to call Windows Forms near to perfect. So what is this post about then? Imagine that you’re building a typical enterprise application with layered architecture. You’ve organized your layers perfectly, looking like this: &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvAZ56att2C8SEW8UM5oJZJMX6VqAIiCrj4_tG-iMSDlQ9ov0XcFGiODBJtlZyiZ6tFY4xZ-MXc38_d6c6_mja4pqdL6XzbgqvyLmgQhYIuglD4vzUyuYibhSeJTrJ_yoAoKKmc9sgynQ/s1600-h/3tier_27.jpg&quot;&gt;&lt;img style=&quot;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&quot; title=&quot;Perfect layered architecture&quot; border=&quot;0&quot; alt=&quot;Perfect layered architecture&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFhTA95qXHNlQVOaLQaZobf8sqWxeeVH7O_HWb8_SxhyphenhyphenHpbK4sz5bIVtDZNnNTP3DP3mTZmHzZiwhJR5yT4cAiVF5umPYqc55pzpwgg6mJRRqaQb-UXE15qcRsR-JZdqRbB6KC9R5H17A/?imgmax=800&quot; width=&quot;286&quot; height=&quot;369&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Next imagine that you’re done with developing your data and business layers. You’re happy since you have a nice, clean and loosely coupled design so far. Everything seems perfect as it can be. The next step is to work on your presentation layer. You are all excided! Of course, you choose to use Windows Forms for that. Shortly, after starting the development of presentation layer, you start to feel weird. Your feeling of nice, clean codebase starts to disappear. You start to see your BLL classes mixed in various event handlers. A lot of &lt;em&gt;(object sender, EventArgs e) &lt;/em&gt;stuff makes you feel sad. You see code like txtName.Text and listViewOrders.Items all over the place. Little by little, it gets harder to gain control of the complexity. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Reason behind this post&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;First off, if you already have experience in using MVP pattern in Windows Forms, then it may be possible that you will not benefit that much from reading this post. &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;There are a lot of sources about implementation of the MVP pattern in Windows Forms applications. However, while exploring the pattern on my own, I didn’t find a lot of applications with full source code provided. Also, example applications were very simple, far from what can be seen in real-world applications. This made my understanding of MVP pattern harder. In that sense, I have set following goals for this post: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Build an working application by using MVP pattern. &lt;/li&gt;    &lt;li&gt;Provide full source for it. &lt;/li&gt;    &lt;li&gt;The application needs to be more complex than a simple “Hello World” example &lt;/li&gt;    &lt;li&gt;Explain benefits of using MVP in Windows Forms applications&amp;#160; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;The problem &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;My application consists of a single form. I’ll call the form &lt;em&gt;Money Transfer Form&lt;/em&gt;. The form is used to initiate money transfer from one account to another. Each account has an ID and the name of the account owner. An account can be either of type &lt;em&gt;Personal &lt;/em&gt;or &lt;em&gt;Company&lt;/em&gt;. The actual transfer can be either &lt;em&gt;Standard&lt;/em&gt;, &lt;em&gt;International&lt;/em&gt;, &lt;em&gt;Extended &lt;/em&gt;(no idea what that means). When transferring money, one needs to provide amount which needs to be of specific currency. Here are the other requirements/rules: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;When transfer type is &lt;em&gt;Standard&lt;/em&gt; the amount that can be transferred is limited to 500 &lt;/li&gt;    &lt;li&gt;When transfer type is &lt;em&gt;International &lt;/em&gt;the amount cannot be lower then 200, and the currency needs to be in dollars. However, if dollar exchange rate at the moment of transfer is below 1.3, then the minimum amount that’s allowed needs to be 700. User of application needs to be notified when this happens. Also, in such case, another currency can be used for transfer. &lt;/li&gt;    &lt;li&gt;When transfer type is &lt;em&gt;Extended&lt;/em&gt; and both accounts are &lt;em&gt;Company&lt;/em&gt;, an additional description of the transfer needs to be specified. Normally, users cannot enter any kind of description unless these conditions are not satisfied. The description cannot exceed 20 characters. If it does, the user will be asked a if he wants to reset the description (clear it). Also, as long as description is larger then 20 characters, this will be indicated with different color in UI.&amp;#160; &lt;/li&gt;    &lt;li&gt;When user enters account number, the name of the account owner and account type has to be displayed on the form. &lt;/li&gt;    &lt;li&gt;Before actual money transfer is initiated, application needs to ask the user to confirm such action. Also, when transfer has been done, the user needs to be informed. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Note that these rules really don’t make any sense, but can provide us with the complexity that often is found in real-world applications.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Domain design&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Here is my domain model:&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXclp3mbJTzeeDe_YGikRGxLYGV6WxJlaozh63MS84jqo6OPPfoH0oFh42VtPt7OcrSdOC9-nnjfNQHJRfIpzdupidPl0fHxnUoDDUK3Z8V5iQi-BVP8q0T3VRTyJgUWhAwMR7sEDqU5k/s1600-h/image15.png&quot;&gt;&lt;img style=&quot;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&quot; title=&quot;Domain Model&quot; border=&quot;0&quot; alt=&quot;Domain Model&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWbOl7IdnBdeXnJAW7Ma6FjqOgHJZ3RLaL7TPAkGoGlPnk3j9khB9anmjr4J0Yrdh84TtIFmv3NIljyyu-Hpba0EcBiGxCio_hDXokB8UDJO0AnR4LrKma0j8gfthPhiH35VF6bcd26PM/?imgmax=800&quot; width=&quot;585&quot; height=&quot;478&quot; /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The core domain model is very simple. To most interesting part is &lt;em&gt;MoneyTransfer &lt;/em&gt;class.&amp;#160; &lt;/p&gt; &lt;style type=&quot;text/css&quot;&gt;














































.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;&lt;style type=&quot;text/css&quot;&gt;


















































.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;  &lt;p&gt;&lt;strong&gt;Validation&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;Since I have a lot of business rules, I have to think about validation. There has been a lot of discussions about validation in domain model. There are generally 2 different approaches&lt;/p&gt;  &lt;p&gt;1. Let the entity validate itself by using some kind of IsValid() method.    &lt;br /&gt;2. Use external services to perform validation on entities. &lt;/p&gt;  &lt;p&gt;The first method is fine, but lacks some flexibility. The main argument against it is that the same entity can be valid in one context and not valid in some other context. Personally, I prefer to keep my validation away from entities. Validation services can also be reused. This way, in one context a one set of rules can be used, while in another context a different set of rules can be used. There are different approaches to implementing the validation services, for example introducing &lt;em&gt;IValidator&amp;lt;T&amp;gt;&lt;/em&gt; interface, but I will not use that approach just to keep the things simpler. Here is my rule interface: &lt;/p&gt;  &lt;pre class=&quot;csharpcode&quot;&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; ValidationResult
    {
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Message { get; set; }
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; IsValid { get; set; }
    }
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;interface&lt;/span&gt; IMoneyTransferRule
    {
        ValidationResult Validate(MoneyTransfer moneyTransfer);
    }&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;The interface is very simple since we only have &lt;em&gt;Validate &lt;/em&gt;method on it. Every rule must implement this interface. I’ll show some rules implementing &lt;em&gt;IMoneyTransferRule &lt;/em&gt;interface: &lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“When transfer type is Standard the amount that can be transferred is limited to 500”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; StandardLimitRule : IMoneyTransferRule
{
    &lt;span class=&quot;preproc&quot;&gt;#region&lt;/span&gt; IMoneyTransferRule Members

    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; ValidationResult Validate(MoneyTransfer moneyTransfer)
    {
        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (moneyTransfer.Type == TransferType.Standard)
        {
            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (moneyTransfer.Amount &amp;gt; 500)
                &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ValidationResult()
                           {
                               IsValid = &lt;span class=&quot;kwrd&quot;&gt;false&lt;/span&gt;,
                               Message = &lt;span class=&quot;str&quot;&gt;&amp;quot;When transfer type is &#39;Standard&#39;, amount is limited to 500.&amp;quot;&lt;/span&gt;
                           };
        }
        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ValidationResult {IsValid = &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;};
    }

    &lt;span class=&quot;preproc&quot;&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;
  &lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

  &lt;p&gt;“When transfer type is Extended and both accounts are Company, an additional description of the transfer needs to be specified. Normally, users cannot enter any kind of description unless these conditions are not satisfied. The description cannot exceed 20 characters.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;div style=&quot;font-size: 0.8em&quot;&gt;
    &lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; ExtendedTransferRule : IMoneyTransferRule
{
    &lt;span class=&quot;preproc&quot;&gt;#region&lt;/span&gt; IMoneyTransferRule Members

    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; ValidationResult Validate(MoneyTransfer moneyTransfer)
    {
        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (moneyTransfer.Type == TransferType.Extended)
        {
            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (moneyTransfer.From.Type == AccountType.Company &amp;amp;&amp;amp; moneyTransfer.To.Type == AccountType.Company)
            {
                &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;.IsNullOrEmpty(moneyTransfer.Description) || moneyTransfer.Description.Length &amp;gt; 20)
                {
                    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ValidationResult
                               {
                                   IsValid = &lt;span class=&quot;kwrd&quot;&gt;false&lt;/span&gt;,
                                   Message = &lt;span class=&quot;str&quot;&gt;&amp;quot;Extended Company-To-Company transfers need to have description. The description needs to be below 20 characters.&amp;quot;&lt;/span&gt;
                               };
                }
            }
        }
        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ValidationResult { IsValid = &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt; };
    }

    &lt;span class=&quot;preproc&quot;&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;
    &lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;This was my validation strategy. Now, I’ll go to repositories. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Repositories&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are two repositories in my application: &lt;em&gt;ICurrencyRepository &lt;/em&gt;and &lt;em&gt;IAccountRepository. &lt;/em&gt;I’ll provide two in-memory implementations of both.&lt;/p&gt;

&lt;p&gt;AccountRepository&lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt; &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; AccountRepository : IAccountRepository
    {
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; List&amp;lt;Account&amp;gt; _accounts = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; List&amp;lt;Account&amp;gt;(); 
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; AccountRepository()
        {
            _accounts.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Account { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Account 1&amp;quot;&lt;/span&gt;, Type = AccountType.Personal  });
            _accounts.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Account { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;2&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Account 2&amp;quot;&lt;/span&gt;, Type = AccountType.Company });
            _accounts.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Account { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;3&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Account 3&amp;quot;&lt;/span&gt;, Type = AccountType.Personal });
            _accounts.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Account { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;4&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Account 4&amp;quot;&lt;/span&gt;, Type = AccountType.Company });
            _accounts.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Account { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;5&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Account 5&amp;quot;&lt;/span&gt;, Type = AccountType.Personal });
            _accounts.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Account { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;6&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Account 6&amp;quot;&lt;/span&gt;, Type = AccountType.Company  });
            _accounts.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Account { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;7&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Account 7&amp;quot;&lt;/span&gt;, Type = AccountType.Company  });
            _accounts.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Account { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;8&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Account 8&amp;quot;&lt;/span&gt;, Type = AccountType.Company  });
            _accounts.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Account { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;9&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Account 9&amp;quot;&lt;/span&gt;, Type = AccountType.Personal });
            _accounts.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Account { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;10&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Account 10&amp;quot;&lt;/span&gt;, Type = AccountType.Personal });
        }
        &lt;span class=&quot;preproc&quot;&gt;#region&lt;/span&gt; IAccountRepository Members

        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; Account Load(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; ID)
        {
            &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; _accounts.Where(m =&amp;gt; m.ID == ID).FirstOrDefault();
        }
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Withdraw(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; ID, &lt;span class=&quot;kwrd&quot;&gt;double&lt;/span&gt; amount)
        {
            &lt;span class=&quot;rem&quot;&gt;// we don&#39;t actually do anything here&lt;/span&gt;
        }
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Deposit (&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; id, &lt;span class=&quot;kwrd&quot;&gt;double&lt;/span&gt; amount)
        {
            &lt;span class=&quot;rem&quot;&gt;// we don&#39;t actually do anything here&lt;/span&gt;
        }
        &lt;span class=&quot;preproc&quot;&gt;#endregion&lt;/span&gt;
    }&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;CurrencyRepository&lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;  &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; CurrencyRepository : ICurrencyRepository
    {
        &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; List&amp;lt;Currency&amp;gt; _currencies = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; List&amp;lt;Currency&amp;gt;(); 
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; CurrencyRepository()
        {
            _currencies.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Currency { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;USD&amp;quot;&lt;/span&gt;, Country = &lt;span class=&quot;str&quot;&gt;&amp;quot;US&amp;quot;&lt;/span&gt;, ExchangeRate = 1.6 });
            _currencies.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Currency { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;2&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;GBP&amp;quot;&lt;/span&gt;, Country = &lt;span class=&quot;str&quot;&gt;&amp;quot;United Kingdom&amp;quot;&lt;/span&gt;, ExchangeRate = 1.0 });
            _currencies.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Currency { ID = &lt;span class=&quot;str&quot;&gt;&amp;quot;3&amp;quot;&lt;/span&gt;, Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;EUR&amp;quot;&lt;/span&gt;, Country = &lt;span class=&quot;str&quot;&gt;&amp;quot;EU&amp;quot;&lt;/span&gt;, ExchangeRate = 1.95 });
        }

        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; Currency Load(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; id)
        {
            &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; _currencies.Where(c =&amp;gt; c.ID == id).FirstOrDefault(); 
        }

        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; List&amp;lt;Currency&amp;gt; GetAll()
        {
            &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; _currencies.ToList(); 
        }
    }&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;Another thing worth mentioning is &lt;em&gt;TransferFailedException &lt;/em&gt;class:&lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; TransferFailedException : Exception 
{
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; List&amp;lt;ValidationResult&amp;gt; FailedValidations { get; set; } 
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; TransferFailedException(ValidationResult[] failedValidations)
    {
        &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.FailedValidations = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; List&amp;lt;ValidationResult&amp;gt;(); 
        &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.FailedValidations.AddRange(failedValidations); 
    }
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;&lt;strong&gt;MVP Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MVP stands for Model-View-Presenter. The relationships between M and V and P are: &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj19mqdEOE6QvUX6eiaSOWwKaB0mFOUQw0ghB65GJu0z2fXFru5X2kOXyS8K5-_Y7y0hjmKHsjub05N6lyyJhMYnSMA4IxDb5Xdqt5jf5KdiWgN7tRJ9H-EVfy6yZBzMgULN1wrljJmoEU/s1600-h/ModelViewPresenter11.gif&quot;&gt;&lt;img style=&quot;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&quot; title=&quot;Model-View-Presenter&quot; border=&quot;0&quot; alt=&quot;Model-View-Presenter&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimD43wFh1zaW7rcjVpfFsbLrEC8wcktWc4exFsg8y7FPC3jSqO4IDAW8wyuHFQKdSMfJI1iodBRnBdMyzHz7-SRf0w5xgHaqJS23gGO8JM-kW42L_ShWEEkXT_BCIY7OJ3npS_eY9TMvU/?imgmax=800&quot; width=&quot;366&quot; height=&quot;205&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Controller is mediating between &lt;strong&gt;Model&lt;/strong&gt; and &lt;strong&gt;View&lt;/strong&gt;&lt;em&gt;.&lt;/em&gt; Unlike in MVC here we don’t have direct connection between the &lt;strong&gt;Model &lt;/strong&gt;and the &lt;strong&gt;View&lt;/strong&gt;&lt;em&gt;. &lt;/em&gt;The &lt;strong&gt;Presenter &lt;/strong&gt;is responsible for updating both the &lt;strong&gt;Model &lt;/strong&gt;and the &lt;strong&gt;View&lt;/strong&gt;&lt;em&gt;. &lt;/em&gt;This variant of MVP is called &lt;strong&gt;Passive View&lt;/strong&gt;&lt;em&gt;, &lt;/em&gt;since &lt;strong&gt;View &lt;/strong&gt;acting is kind of dumb. The whole process goes like this: &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Events are received on &lt;strong&gt;View&lt;/strong&gt;&lt;em&gt;, &lt;/em&gt;the &lt;strong&gt;View &lt;/strong&gt;passes them to the &lt;strong&gt;Presenter&lt;/strong&gt;. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Presenter &lt;/strong&gt;decides how and if the &lt;strong&gt;Model &lt;/strong&gt;needs to be updated. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Presenter &lt;/strong&gt;initiates any needed changes on the &lt;strong&gt;View&lt;/strong&gt;&lt;em&gt;. &lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this approach &lt;strong&gt;View&lt;/strong&gt; is very simplified and worries only about the very presentation. There are no decisions being made in the &lt;strong&gt;View&lt;/strong&gt;&lt;em&gt;.&lt;/em&gt; The &lt;strong&gt;Presenter &lt;/strong&gt;actually controls the logic flow. This is how &lt;strong&gt;Passive View &lt;/strong&gt;variant of MVP works. There are also two other variants of MVP: &lt;strong&gt;Presentation Model &lt;/strong&gt;and &lt;strong&gt;Supervising Controller&lt;/strong&gt;&lt;em&gt;. &lt;/em&gt;I will not talk about them now. This picture of &lt;strong&gt;Passive View &lt;/strong&gt;may be more expressive: &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqMVEERorryOsk57gvlzJuqZiJ2-i3z6ENKLzC7voEX73dPHTWrSTO2s_wgXo_nRy1KDE-u2UD3WmC3nOSGI50ljoEVjVuLQ-eA0pCI3H0xx6KiXqRdpOiMm3ijvouKHLTEar5vd6fQJ4/s1600-h/ModelViewPresenter26.png&quot;&gt;&lt;img style=&quot;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&quot; title=&quot;Model-View-Presenter &quot; border=&quot;0&quot; alt=&quot;Model-View-Presenter &quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQAk_QTuYB-Tb4In-5qjzr-oKHOxCgC6EFbMFa3diL71s-oJQIDWfoBDwxh_7x3eY8Aad6-fO32C8mWcxw-lID2R0vGblPs01d5d6rsQhh4T8dgycqajnaHSqT9DTL9YRjj6qwMPZzsQU/?imgmax=800&quot; width=&quot;295&quot; height=&quot;262&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;That was theory. Here’s the practice:&lt;/p&gt;

&lt;p&gt;Here it is how my form looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizBkPaV4r7OK_nEVqVX3qy5ZqdiD_R4xvZ9QTa8t6BhjsaCxDgKlWigBarReu-Lz7fCsLl18jrc3GiX1pMtmeBQYgIDBcfPepNXfOg4N8Cb6Tw37b6FLQeM4edvfg1RwrwCXEnECFzXtw/s1600-h/image6.png&quot;&gt;&lt;img style=&quot;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&quot; title=&quot;Example MVP Application&quot; border=&quot;0&quot; alt=&quot;Example MVP Application&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd7aQstLLDACne5CMlZgJnQ7EVz_6vxljKBuQZtFh1uKUG0VJ_853Y2KisNgTrOP6vCeEOlut9w1vhYbLfg_g6shdkvPNjPfPAIWnkVvWmDXqNUL88Kxw9bS6Yk4uy6LmEpnwl6mc_XQY/?imgmax=800&quot; width=&quot;472&quot; height=&quot;334&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, I’ll abstract my frmMoneyTransfer with an interface: &lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;interface&lt;/span&gt; IMoneyTransferView
{
    &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Account1ID { get; set; }
    &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Account1Name { get; set; }
    &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Account1Type { get; set; }
    &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Account2ID { get; set; }
    &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Account2Name { get; set; }
    &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Account2Type { get; set; }
    &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Description { get; set; }
    Currency Currency { get; }
    &lt;span class=&quot;kwrd&quot;&gt;double&lt;/span&gt; Amount { get; set; }
    TransferType Type { get; set; }

    &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; AskIfSure();
    &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; ShowMessage(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; message);
    &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; DisplayDescriptionField();
    &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; HideDescriptionField();
    &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; PopulateCurrencyList(List&amp;lt;Currency&amp;gt; currencyList);
    &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; AskQuestion(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; message);
    &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; ExpandCurrencyBox();
    &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; SetDescriptionDifferentColor();
    &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; SetDescriptionNormalColor();
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;The &lt;em&gt;frmMoneyTransfer &lt;/em&gt;implements &lt;em&gt;IMoneyTransferView&lt;/em&gt; interface. &lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;partial&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; frmMoneyTransfer : Form , IMoneyTransferView
{
    ...
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;I’ll also need my &lt;em&gt;MoneyTransferPresenter &lt;/em&gt;class&lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MoneyTransferPresenter
{
    ...
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;&lt;strong&gt;Connecting View and Presenter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The connection between &lt;strong&gt;View&lt;/strong&gt; and &lt;strong&gt;Presenter &lt;/strong&gt;is simple one, they hold references to each other. Here is the &lt;strong&gt;Presenter&lt;/strong&gt; constructor:&lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MoneyTransferPresenter
{
    ...
    &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; IMoneyTransferView View { get; set; }
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; MoneyTransferPresenter(IMoneyTransferView view)
    {
        ...
        &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.View = view;
        ...

    }
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;The &lt;strong&gt;View&lt;/strong&gt; implementation also holds reference to &lt;strong&gt;Presenter&lt;/strong&gt;&lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;partial&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; frmMoneyTransferFrom : Form , IMoneyTransferView
{    ...    
    MoneyTransferPresenter Presenter { get; set; }     
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; frmMoneyTransferFrom()    
    {        
        InitializeComponent();        
        &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.Presenter = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; MoneyTransferPresenter(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;);     
    }    
    ...
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;&lt;strong&gt;Connection between Model and Presenter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This one is even simpler. &lt;strong&gt;Presenter&lt;/strong&gt; holds reference to &lt;strong&gt;Model&lt;/strong&gt;&lt;em&gt;. &lt;/em&gt;The &lt;strong&gt;Model &lt;/strong&gt;does not even need to know about that. &lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MoneyTransferPresenter
{
    ...
    &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; MoneyTransfer MoneyTransfer { get; set; }

    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; MoneyTransferPresenter(IMoneyTransferView view)
    {
        ...
        &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.MoneyTransfer = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; MoneyTransfer();
        ...
    }
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;I have said earlier that:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“Events are received on &lt;strong&gt;View&lt;/strong&gt;&lt;em&gt;,&lt;/em&gt; the &lt;strong&gt;View &lt;/strong&gt;passes them to &lt;strong&gt;Presenter&lt;/strong&gt;.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here’s how it looks in practice when user presses &lt;em&gt;Process Transfer &lt;/em&gt;button.&lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; btnProcessTransfer_Click(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)
{
    Presenter.ProcessTransfer();
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;The view only called &lt;em&gt;Presenter.ProcessTransfer()&lt;/em&gt; method. Here’s another example: &lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; txtAmount_TextChanged(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)
{
    Presenter.AmountChanged(); 
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;I also said this: &lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;“&lt;/em&gt;&lt;strong&gt;Presenter &lt;/strong&gt;decides how and if the &lt;strong&gt;Model&lt;/strong&gt; needs to be updated.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MoneyTransferPresenter
{
    ...
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; AmountChanged()
    {
        &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.MoneyTransfer.Amount = View.Amount;
    }
    ...
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;Then again, I’ve said this as well: &lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;“&lt;/em&gt;&lt;strong&gt;Presenter&lt;/strong&gt; initiates any needed changes on the &lt;strong&gt;View&lt;/strong&gt;&lt;em&gt;”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Below, you can see how &lt;strong&gt;Presenter&lt;/strong&gt; tells the &lt;strong&gt;View &lt;/strong&gt;what to do and updates it appropriately:&lt;/p&gt;

&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; DescriptionChanged()
{
    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (View.Description.Length &amp;gt; 20)
    {
        &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; answer =
            View.AskQuestion(
                &lt;span class=&quot;str&quot;&gt;&amp;quot;Warning : Your description is larger then 20 characters. Your transfer may fail. Do you want to clear it?&amp;quot;&lt;/span&gt;);
        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (answer == &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;)
        {
            View.Description = &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;.Empty;
        }
        &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt;
        {
            View.SetDescriptionDifferentColor();
        }
    }
    &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt;
    {
        View.SetDescriptionNormalColor();
    }
    MoneyTransfer.Description = View.Description;
}&lt;/pre&gt;
&lt;style type=&quot;text/css&quot;&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, 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; }&lt;/style&gt;

&lt;p&gt;&lt;strong&gt;What is the benefit of using MVP?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;We’ve managed to control the complexity by separating concerns. &lt;/li&gt;

  &lt;li&gt;Code in &lt;em&gt;frmMoneyTransfer&lt;/em&gt; form is simplified and easy to understand. &lt;/li&gt;

  &lt;li&gt;Code in &lt;strong&gt;Presenter &lt;/strong&gt;is clean and simple because it accesses the &lt;strong&gt;View &lt;/strong&gt;using well defined interface – &lt;em&gt;IMoneyTransferView&lt;/em&gt; &lt;/li&gt;

  &lt;li&gt;We have better structure of the code which improves understandability, maintainability, readability. &lt;/li&gt;

  &lt;li&gt;We have got a more testable code. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note on source code provided&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I did not use any IoC container just to keep the code simpler and more focused on MVP pattern. That’s why I have things like &lt;em&gt;new MoneyTransferPresenter() &lt;/em&gt;and &lt;em&gt;new AccountRepository() &lt;/em&gt;all over the place.&lt;/p&gt;

&lt;p&gt;I also strongly suggest you to go through the &lt;a href=&quot;http://www.sendspace.com/file/a5oc2n&quot; target=&quot;_blank&quot;&gt;source code&lt;/a&gt; and examine the various parts of the application. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My conclusion is that, in absolutely all, but the most simplest situations, should MVP pattern be used when building presentation layers in Windows Forms. The pattern is easy to learn and the benefit of using it is great. &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/628364173992578541/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/exploring-mvp-pattern.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/628364173992578541'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/628364173992578541'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/exploring-mvp-pattern.html' title='Exploring the MVP Pattern'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFhTA95qXHNlQVOaLQaZobf8sqWxeeVH7O_HWb8_SxhyphenhyphenHpbK4sz5bIVtDZNnNTP3DP3mTZmHzZiwhJR5yT4cAiVF5umPYqc55pzpwgg6mJRRqaQb-UXE15qcRsR-JZdqRbB6KC9R5H17A/s72-c?imgmax=800" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-3327267427561706535</id><published>2010-02-04T07:16:00.001-08:00</published><updated>2010-02-04T11:41:27.005-08:00</updated><title type='text'>A good reading list</title><content type='html'>&lt;p&gt;Today, I found an interesting blog post that kind of represents a reading guide to becoming a better developer. I found it on &lt;a href=&quot;http://davybrion.com&quot;&gt;The Inquisitive Coder&lt;/a&gt; blog, which I highly recommend. You can look at the original post &lt;a href=&quot;http://davybrion.com/blog/2009/11/a-reading-guide-to-becoming-a-better-developer/&quot;&gt;here&lt;/a&gt;. The author separates reading list in three sections: Introduction, Design &amp;amp; Architecture, Team Environment. The author also thinks that it’s best to read the books exactly in given order. Anyway, here is the list: &lt;/p&gt;  &lt;p&gt;Introduction&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;     &lt;div align=&quot;justify&quot;&gt;&lt;a href=&quot;http://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530&quot;&gt;Test-Driven Development (Kent Beck)&lt;/a&gt;&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672&quot;&gt;Refactoring (Martin Fowler)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Implementation-Patterns-Kent-Beck/dp/0321413091&quot;&gt;Implementation Patterns (Kent Beck)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670&quot;&gt;Code Complete: 2nd Edition (Steve McConnell)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052&quot;&gt;Working Effectively With Legacy Code (Michael Feathers)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882&quot;&gt;Clean Code (Robert C. Martin)&lt;/a&gt; &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Design &amp;amp; Architecture&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612&quot;&gt;Design Patterns (Gang Of Four)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420&quot;&gt;Patterns Of Enterprise Application Architecture (Martin Fowler)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_sim_b_2&quot;&gt;Domain-Driven Design (Eric Evans)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Enterprise-Integration-Patterns-Designing-Deploying/dp/0321200683&quot;&gt;Enterprise Integration Patterns (Gregor Hohpe, Bobby Woolf)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Release-Production-Ready-Software-Pragmatic-Programmers/dp/0978739213&quot;&gt;Release It! Design and deploy production-ready software (Michael T. Nygard)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Things-Every-Software-Architect-Should/dp/059652269X&quot;&gt;97 Things Every Software Architect Should Know (edited by Richard Monson-Haefel)&lt;/a&gt; &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Team Environment&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Extreme-Programming-Explained-Embrace-Change/dp/0321278658&quot;&gt;Extreme Programming Explained, Second Edition (Kent Beck)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Art-Agile-Development-James-Shore/dp/0596527675&quot;&gt;The Art Of Agile Development (James Shore &amp;amp; Shane Warden)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Mythical-Man-Month-Software-Engineering-Anniversary/dp/0201835959&quot;&gt;The Mythical Man-Month, 20th Anniversary Edition (Frederick P. Brooks)&lt;/a&gt; &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;I already own following books from this list:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670&quot;&gt;Code Complete: 2nd Edition (Steve McConnell)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_sim_b_2&quot;&gt;Domain-Driven Design (Eric Evans)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Enterprise-Integration-Patterns-Designing-Deploying/dp/0321200683&quot;&gt;Enterprise Integration Patterns (Gregor Hohpe, Bobby Woolf)&lt;/a&gt; – Still reading this one, now at chapter 5 &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Even though I’m not absolutely sure that this is the best order to read these books, I have decided to start reading Test-Driven Development (Kent Beck) first. It shouldn’t take long since it has around 240 pages. Currently, I’m at chapter 3, and I have to say I like the style of the book. It’s easy to follow and the author, Kent Beck, knows how to make a point. I also plan to introduce a new practice of reading one book each month. Two if time permits. This way, it will take me a year to read the all the books from the list. I’m pretty sure it will be worth the effort. &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/3327267427561706535/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/good-reading-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/3327267427561706535'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/3327267427561706535'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/good-reading-list.html' title='A good reading list'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-1080508580448987313</id><published>2010-02-02T14:23:00.001-08:00</published><updated>2010-02-02T14:23:54.287-08:00</updated><title type='text'>Digging deeper into Messaging</title><content type='html'>&lt;p&gt;Recently, I got interested into messaging. Messaging is simply one of the ways of building enterprise applications. When you live in .NET world, you can use one of the following frameworks to ease your way of building a messaging application:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href=&quot;http://www.nservicebus.com/&quot; target=&quot;_blank&quot;&gt;NServiceBus&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://code.google.com/p/masstransit/&quot; target=&quot;_blank&quot;&gt;MassTransit&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://ayende.com/Blog/archive/2008/12/17/rhino-service-bus.aspx&quot; target=&quot;_blank&quot;&gt;Rhino Service Bus&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I decided to go with NServiceBus and write an simple application just to get more sense about the overall approach. I liked what I saw. Messaging gives a whole new meaning to building robust, fault-tolerant, fast performing, loosely coupled enterprise applications. The framework itself is designed in a such way so that actual development is simplified to it’s maximum. My next step was to download NServiceBus source code to take a peek of how the things were done internally. This helped me to grasp core concepts better and faster. Since I liked the whole idea, I decided to take things even further and read the &lt;a href=&quot;http://www.amazon.com/Enterprise-Integration-Patterns-Designing-Deploying/dp/0321200683&quot; target=&quot;_blank&quot;&gt;Enterprise Integration Pattern&lt;/a&gt; book which stands for one of the best on the subject. I would also like to look more into the NServiceBus source code. However, reading the book has a higher priority for now. If you’re new to messaging, I strongly recommend checking out following links: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href=&quot;http://nservicebus.com/Documentation.aspx&quot; target=&quot;_blank&quot;&gt;NServiceBus Overview documentation&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://gojko.net/2008/12/02/asynchronous-net-applications-with-nservicebus/&quot; target=&quot;_blank&quot;&gt;Asynchronous .NET applications with NServiceBus&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Anyway, I’ll keep you posted on my progress and thoughts on this interesting subject. &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/1080508580448987313/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/digging-deeper-into-messaging.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/1080508580448987313'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/1080508580448987313'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/02/digging-deeper-into-messaging.html' title='Digging deeper into Messaging'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-3851059697962347069</id><published>2010-01-31T12:35:00.001-08:00</published><updated>2010-01-31T12:36:21.988-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><title type='text'>Thoughts on OO design</title><content type='html'>&lt;p&gt;OO design is a creative activity. To be successful at it, you’ll need a mixture of both talent and learned skills. There is no such thing as &lt;em&gt;correct &lt;/em&gt;design. There is only good, bad and somewhere-in-between design. OO program is simply a collection of objects that send messages to each other. But how do you design these objects? What objects to create? Which objects should interact? In my work, I usually begin OO design by identifying abstractions. I look at the problem from higher level. I try to identify abstractions that model true nature of the problem. An common mistake is to completely model real-world entities. In software development we have possibility of using abstractions to make our job/life easier. You should use this to your advantage. I assign each object a single responsibility. Note that assigning responsibilities is not easy at all. If you are good at assigning responsibilities then chances are, that you’re are good at OO designs too.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;“&lt;strong&gt;Understanding responsibilities is key to good object-oriented design&lt;/strong&gt;”—Martin Fowler&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Once you’re done with the design, how do you know if it’s good or bad? There are two metrics/principles that you can evaluate your designs against. These are coupling and cohesion. These principles are very powerful in assisting you to make your OO designs good. Funnily, the principles don’t even come from OO, but from Structured design. Coupling is simply a degree to which each object is dependent on each one of the other objects (Wikipedia definition). Coupling quantifies the dependencies among objects. It can be loose or tight. Loose coupling is desired, since it’s more &lt;em&gt;change-friendly&lt;/em&gt;. Fewer dependencies make introducing changes easier. Cohesion tells us how focused a class is in fulfilling its responsibility. Does a class have well defined responsibility? Are all parts of class (methods etc) focused on fulfilling that, and only that responsibility? High-cohesion is desired, since it promotes robustness, reusability, and understandability.&lt;/p&gt;  &lt;p&gt;To sum up, here are the key concepts I use in my day-to-day OO design: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Abstractions &lt;/li&gt;    &lt;li&gt;Responsibilities &lt;/li&gt;    &lt;li&gt;Loose-coupling &lt;/li&gt;    &lt;li&gt;High-cohesion &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Following books helped me to gain more understanding of OO design:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Applying-UML-Patterns-Introduction-Object-Oriented/dp/0131489062/ref=dp_ob_title_bk&quot;&gt;Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development&lt;/a&gt; by Craig Larman. This book is a great place to start. It will teach you UML as well, and how to use it effectively. &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Object-Design-Roles-Responsibilities-Collaborations/dp/0201379430&quot;&gt;Object Design: Roles, Responsibilities, and Collaborations&lt;/a&gt; by Rebecca Wirfs-Brock. I haven’t read it completely ,gets kind of boring at some parts, but there are some great insights on OO design. &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Object-Oriented-Design-Heuristics-Arthur-Riel/dp/020163385X&quot;&gt;Object-Oriented Design Heuristics&lt;/a&gt; by Arthur J. Riel – a bit old, but great book. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Final advice:&lt;/strong&gt; The best way to become better at OO design is to do it a lot. A huge amount of it.&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/3851059697962347069/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/01/thoughts-on-oo-design.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/3851059697962347069'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/3851059697962347069'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/01/thoughts-on-oo-design.html' title='Thoughts on OO design'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-7897134501379053882</id><published>2010-01-30T05:48:00.001-08:00</published><updated>2010-01-30T08:55:16.451-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="General programming"/><title type='text'>DRY – The first step</title><content type='html'>&lt;p&gt;It has been said that a thousand miles journey starts with a single step. So in case journey = becoming a better developer, then first step = DRY. DRY stands for Don’t Repeat Yourself. I see way too much copy-paste development around. This needs to stop. If you constantly copy-paste code around, you’re limiting yourself of becoming a better developer. Code duplication is evil, and it’s simply against common sense. So why do we do it? We do it probably because it’s easier, it doesn’t require us to think. When we stop thinking, our journey of becoming a better developer ends. Anyway, does anybody know how do you disable CTRL+C in VS :)?&amp;#160; &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/7897134501379053882/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/01/dry-first-step.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/7897134501379053882'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/7897134501379053882'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/01/dry-first-step.html' title='DRY – The first step'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-416189222027027227</id><published>2010-01-27T12:47:00.001-08:00</published><updated>2010-01-27T14:36:52.187-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term=".NET"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Silverlight"/><title type='text'>Is anybody interested in open-source tool like Silverlight Spy?</title><content type='html'>&lt;p&gt;A few months ago I started to work on a Silverlight project. It was a business-line application, pretty much standard of that type. It was my first project in Silverlight. Anyway, soon I found this great tool called &lt;a href=&quot;http://silverlightspy.com/silverlightspy/&quot; target=&quot;_blank&quot;&gt;Silverlight Spy&lt;/a&gt;. It’s a tool for runtime inspection of Silverlight applications, something like Firebug but only for Silverlight. I was impressed with the software since it was very powerful. The tool costs around 99 EUR, which is not much for the value you get. However, as a passionate developer, I like to develop my own tools (during my free time of course:)). It was then the idea of similar open-source tool was born. I decided to develop a proof-of-concept application called Silverlight Buddy(now thinking of renaming it to Silverlight Inspector). Here’s the what I have done by now:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Intercepting Silverlight application before it’s displayed in browser &lt;/li&gt;    &lt;li&gt;Injection of custom code logic into an Silverlight application&amp;#160;&amp;#160; &lt;/li&gt;    &lt;li&gt;Solved communication between Silverlight application and Silverlight Buddy tool. (both using COM and JavaScript) &lt;/li&gt;    &lt;li&gt;Support for hosting DLR inside Silverlight applications (Just like in Silverlight Spy) &lt;/li&gt;    &lt;li&gt;Very basic “Inspect Element”-like functionality &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The app is not stable since it is intended only to be a proof-of-concept, written in a quick-and-dirty way. But as you can see, all the major issues/parts have been solved. There is still a huge amount of work to be done in order to make it a usable application. Now,I have never run open-source project, and to be honest, I don’t have time to. That’s why I thought there was somebody out there willing to continue to work on the project. I will probably be a committer myself, but don’t have time to devote fully to the project. If somebody is interested please write a comment. &lt;/p&gt;  &lt;p&gt;Here are some screenshots of the app so far. &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhD420sNB9v4XifpgA8cfFToUD0-YvxT9lCEHcfGj52CCqjvvzq42zGW1BV-MynYSEGvYU5K4aKQA2z4iTgfUhlGvNz8KYUeNHaPrzmjUxrxocLfgetCoB0EVac1Bvk7Lm-zHFrBt7_uBo/s1600-h/image%5B18%5D.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgt1CI9lEeIRAbVigEVBv7_Xw644LgmYOcUqWjG4TzECWg8jXY9VUnSTKRP15Ay6u_2aPrpmK0QvsG334eWfdh8nSfwkSDav_4HyedO7WmU1KJqX7qkivL0gX6gr_lkqK_bkUcrEIfDtT0/?imgmax=800&quot; width=&quot;764&quot; height=&quot;593&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Notice the red border around “Search” textbox, it represents “Inspect Element” functionality as you move your mouse over the elements. &lt;/p&gt;  &lt;p&gt;I hope someone will be willing to continue the work on this project so the effort I have made so far will not be wasted. &lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/416189222027027227/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/01/is-anybody-interested-in-open-source.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/416189222027027227'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/416189222027027227'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2010/01/is-anybody-interested-in-open-source.html' title='Is anybody interested in open-source tool like Silverlight Spy?'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgt1CI9lEeIRAbVigEVBv7_Xw644LgmYOcUqWjG4TzECWg8jXY9VUnSTKRP15Ay6u_2aPrpmK0QvsG334eWfdh8nSfwkSDav_4HyedO7WmU1KJqX7qkivL0gX6gr_lkqK_bkUcrEIfDtT0/s72-c?imgmax=800" height="72" width="72"/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-7763921532526864813</id><published>2009-08-15T10:34:00.001-07:00</published><updated>2010-02-06T15:11:14.723-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC"/><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript"/><title type='text'>ASP.NET : Using ClientID in external JavaScript files</title><content type='html'>&lt;p&gt;In the past year I have been writing a lots of JavaScript code. Mostly because I have been working on same ASP.NET WebForms application for more then 10 months by now. We use JavaScript for things like validation, async HTTP requests etc. To keep our code clean,we try to keep all of our JavaScript in external files. The problem with external JavaScript files is that you cannot use server tags in them, so you cannot obtain ClientID of ASP.NET controls by using &amp;lt;%= control.ClientID %&amp;gt; syntax. My first workaround for this problem was to add JavaScript variables manually on every page by using ClientScript.RegisterClientScriptBlock() method. Every page needed to have a collection of controls which was called JSControls. In the Page_Load event of the page I would add to JSControl collection all controls for which I need access from JavaScript. Code would look like this: &lt;/p&gt;  &lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; List&amp;lt;Control&amp;gt; JSControls = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; List&amp;lt;Control&amp;gt;();
&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Page_Load(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)
{
     JSControls.Add(txtName);
     JSControls.Add(txtLastname);
}&lt;/pre&gt;&lt;p&gt;Then I had a function that would generate JavaScript code for each control in JSControl collection:&lt;/p&gt;&lt;pre class=&quot;brush: csharp&quot;&gt;public static string GetClientScriptBlock()
{
StringBuilder sb = new StringBuilder();
sb.Append(&quot;&amp;lt;script type=\&quot;text/javascript\&quot;&amp;gt;&quot;);
foreach (Control c in JSControls)
{
    if (c != null)
    {
        sb.Append(string.Format(&quot;var {0} = &#39;{1}&#39;;&quot;, c.ID + &quot;ClientID&quot;, c.ClientID));
    }
}
sb.Append(&quot;&amp;lt;/script&amp;gt;&quot;);
return sb.ToString();
}&lt;/pre&gt;&lt;p&gt;Then, again in the Page_Load event I needed to call RegisterClientScriptBlock with JavaScript code block:&lt;/p&gt;&lt;pre class=&quot;brush: csharp&quot;&gt;protected void Page_Load(object sender, EventArgs e)
{
JSControls.Add(txtName);
JSControls.Add(txtLastname);                         
Page.ClientScript.RegisterStartupScript(this.GetType(), &quot;myClientBlock&quot;, GetClientScriptBlock(), false);
}&lt;/pre&gt;&lt;p&gt;This works fine, so if I need to access for example ClientID of TextBox control called txtName I would just refer to txtNameClientID variable, like this:&lt;/p&gt;&lt;pre class=&quot;js&quot; name=&quot;code&quot;&gt;document.getElementById(txtNameClientID); &lt;/pre&gt;

&lt;p&gt;While this approach is fine it has some disadvantages. One problem is that there could be controls with same ID but in different parent controls. Another disadvantage is that this approach is not very reusable, it would force me to violate DRY principle, since the same code had to be added to every page. &lt;/p&gt;

&lt;h4&gt;A better solution&lt;/h4&gt;

&lt;p&gt;There is a better solution. A reusable custom control can be created to automate JavaScript variable creation for us. The idea is to put the control on a Page, define for which controls from the page you need ClientID’s and the control would do the rest of the work. The custom control would also have some kind of Namespace property, that would solve the problem with same ID on different controls. Design-Time support would also be nice to have here, since we would not need to write control id&#39;s manually. Let’s make our idea to realization!&lt;/p&gt;

&lt;h5&gt;Control syntax &lt;/h5&gt;

&lt;p&gt;Goal is to have control with syntax as simple as possible. Suppose that we call our control &lt;strong&gt;JSClientIDList&lt;/strong&gt;, we want to have syntax like this:&lt;/p&gt;

&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;ReducingComplexity:JSClientIDList runat=&quot;server&quot; ID=&quot;JSClientIDList1&quot; Namespace=&quot;namespace1&quot;&amp;gt;
&amp;lt;JSControls&amp;gt;
    &amp;lt;ReducingComplexity:ControlItem ControlID=&quot;btnOK&quot; /&amp;gt;
&amp;lt;/JSControls&amp;gt;
&amp;lt;/ReducingComplexity:JSClientIDList&amp;gt;&lt;/pre&gt;
We see that this syntax is very straightforward. JSClientIDList control has two important properties. The first one is Namespace property, which defines some kind of prefix for each of the controls. The second important property is JSControls which defines list of ASP.NET controls for which JavaScript variables will be created. We will need another class for representing a control in our JSControls list. We will call that class ControlItem. The whole code would look like this:

&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.UI.WebControls;
using System.Text;

namespace ReducingComplexity.Web.Controls
{
[PersistChildren(false)]
[ParseChildren(true)]
public class JSClientIDList : Control
{
    private string m_Namespace;
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string Namespace
    {
        get
        {
            return m_Namespace;
        }
        set
        {
            m_Namespace = value;
        }
    }

    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public List&amp;lt;ControlItem&amp;gt; JSControls { get; set; }

    public JSClientIDList()
    {
        JSControls = new List&amp;lt;ControlItem&amp;gt;();
    }
    protected override void OnPreRender(EventArgs e)
    {
        Page.ClientScript.RegisterClientScriptBlock(this.Parent.GetType(),this.ClientID, GetClientScriptBlock(), true);
        base.OnPreRender(e);
    }
    protected override void AddParsedSubObject(object obj)
    {
        if (obj is ControlItem)
        {
            this.JSControls.Add((ControlItem)obj);
            return;
        }
    }
    private string GetClientScriptBlock()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat(&quot;var {0}=new Object();&quot;, this.Namespace);
        foreach (ControlItem ci in this.JSControls)
        {
            Control c = this.FindControl(ci.ControlID);
            string clientId = c != null ? c.ClientID : &quot;&quot;;
            sb.AppendFormat(&quot;{0}.{1}=&#39;{2}&#39;;&quot;, this.Namespace, ci.ControlID, clientId);
        }
        Debug.WriteLine(sb.ToString());
        return sb.ToString();
    }
}
public class ControlItem
{
    [TypeConverter(typeof(ControlIDConverter))]
    public string ControlID { get; set; }
}
}&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;
I’ll try to explain most important parts of the code. To enable clean syntax we needed to use PersistChildren and ParseChildren attributes. These attributes define how nested content of the control will be interpreted. More details can be found &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.web.ui.persistchildrenattribute.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. Next we also needed to override AddParsedSubObject method, which will add nested controls to JSControls collection.




&lt;pre class=&quot;js&quot; name=&quot;code&quot;&gt;protected override void AddParsedSubObject(object obj)
{
if (obj is ControlItem)
{
      this.JSControls.Add((ControlItem)obj);
      return;
}
}&lt;/pre&gt;
Another interesting part is usage of ControlIDConverter as TypeConverter. This will enable us to use Design-Time support for ControlID property, or to be more precise it will provide dropdown list of all controls available for addition, so that we can choose from the list.







&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjedTyDW2_J0Q5xDAqtXJx32X-g5HGMULfXnYd9dzAy64bfQqcUHO_u_utRgXNdr3txPv43e52eA57jw76gex5C9AOD9vsx4D8vuuZGwgkqc7FuF1RqUCnHa1Od_vIVxMqRlaTuiSr4KrM/s1600-h/image%5B9%5D.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGRFhuPxh-_l4I20cMhAc2W39_DRKJmJURHJdnNwUxKbKOWqLsyVn1qrgzot-3iCr7mzIW274_Zaa8uZ1wu4HiLtO6Wokou-KidV8Q8ecxyElo-iisuGQwnk2tRy4bw1piCTgSo54VQFs/?imgmax=800&quot; width=&quot;591&quot; height=&quot;425&quot; /&gt;&lt;/a&gt;


&lt;/p&gt;

&lt;p&gt;When I first started implementing this functionality I didn’t really know about ControlIDConverter class. My plan was to write my own Type Converter which would provide such functionality, but I failed. To cut the long story short,the reason I failed was I didn’t know I could use GetService() function of ITypeDescriptorContext interface to get the instance of IDesignerHost. Anyway, Reflector reveals all the secrets:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPD8uQG3Gw3D3-hcgStYrmPuesK-TutrbpT-bEb6S1NPz5ZMg60Y17rqSmIujCadtYna8TZwloMs9UNL62XfeH6uhA3W8OsIW8rmDQkjs5_CDC0KuBjTBU4hidOLxJ2PXmzGlbhBnwaAs/s1600-h/image%5B5%5D.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirLuYDLPpSd0MbIILRmXMdSvft9avLSa9WzXAikhEUWhcPaIPurKCzhfwSKubekRDOtPT3hnGNdiK7UXTYCcSk7yhXFjLsYL2iuErSwOPzT5s8iXpSAoi1U9Q2BqL0h7eBwQQxy-9Z3XE/?imgmax=800&quot; width=&quot;859&quot; height=&quot;446&quot; /&gt;&lt;/a&gt; &lt;/p&gt;
We can also see that the actual registration of JavaScript code block is done in OnPreRender event since in this event all controls in Control collection are available.




&lt;p&gt;&lt;/p&gt;

&lt;p&gt;For example if we define Namespace property to “Namespace1” and have a ASP.NET button control with ID &quot;btnOK&quot; then to get ClientID of the btnOK button from external JavaScript file we would use following syntax:&lt;/p&gt;

&lt;pre class=&quot;js&quot; name=&quot;code&quot;&gt;document.getElementById(Namespace1.btnOK);&lt;/pre&gt;

&lt;p&gt;And of course JSClientIDList control would have to be declared like this:&lt;/p&gt;

&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;&amp;lt;ReducingComplexity:JSClientIDList runat=&quot;server&quot; ID=&quot;JSClientIDList1&quot; Namespace=&quot;namespace1&quot;&amp;gt;
      &amp;lt;JSControls&amp;gt;
          &amp;lt;ReducingComplexity:ControlItem ControlID=&quot;btnOK&quot; /&amp;gt;
      &amp;lt;/JSControls&amp;gt;
&amp;lt;/ReducingComplexity:JSClientIDList&amp;gt;&lt;/pre&gt;

&lt;p&gt;By using this simple control our goal of using external files for JavaScript code has been achieved. &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/7763921532526864813/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/aspnet-and-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/7763921532526864813'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/7763921532526864813'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/aspnet-and-javascript.html' title='ASP.NET : Using ClientID in external JavaScript files'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGRFhuPxh-_l4I20cMhAc2W39_DRKJmJURHJdnNwUxKbKOWqLsyVn1qrgzot-3iCr7mzIW274_Zaa8uZ1wu4HiLtO6Wokou-KidV8Q8ecxyElo-iisuGQwnk2tRy4bw1piCTgSo54VQFs/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-5584317534254141625</id><published>2009-08-14T05:58:00.001-07:00</published><updated>2009-08-14T05:58:41.193-07:00</updated><title type='text'>Good design comes over time</title><content type='html'>&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj04Y-_MyzaxM4POln4MnBUDc5jr8TwgTzyBOqAzw2jbaN2dkA7ljnmI9_aQTfL_NeTZsYhgveJsx_PccFk0zvkSrOYsCxa4YKqbVMDE8xImyLP_wDPtHdOWmOlw9gQxsNwCCvok5tywmo/s1600-h/image%5B4%5D.png&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border: 0px none ; display: inline; margin-left: 0px; margin-right: 0px;&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA-r4OubeQ2Znhq2bz57NzPwppZ7TFvzw1cDXfRobamLyF5-nebMha86yZ-tFxo_xDkUEpP5iJsUBjDDEqQwcufnTi_YvlrqE_iQ8MXn8Tk1DKg6D0EKcFmv8XhWo3E7qN9zx1puIJy1k/?imgmax=800&quot; align=&quot;right&quot; border=&quot;0&quot; height=&quot;228&quot; width=&quot;240&quot; /&gt;&lt;/a&gt; Have you ever tried to provide design for &lt;strong&gt;Mark IV coffee maker problem &lt;/strong&gt;that Robert C. Martin presented in his &quot;Designing Object Oriented C++ Applications using the   
Booch Method&quot; book. Well I have and I was not successful to come up with any kind of elegant solution. But I was quite impressed with the solution that Uncle Bob presented for this design problem. You can find the solution &lt;a href=&quot;http://www.objectmentor.com/resources/articles/CoffeeMaker.pdf&quot;&gt;here&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;What I find interesting in the above document is section titled &quot;&lt;strong&gt;How did I really come up with this design?&quot;&lt;/strong&gt;. In that section Uncle Bob says: &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;    
&lt;em&gt;I did not just sit down one day and develop this design in a nice straightfoward manner. Indeed, my very first design for the coffee maker looked much more like Figure 11–1. However,I have written about this problem many times, and have used it as an exercise while teaching class after class. So this design has been refined over time. &lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This paragraph was very encouraging to me, because as you see even Robert C. Martin himself did not get it right the first time. We are not going to get it right the first time, no matter how much we try. Good designs come over time, they are not obvious immediately. Our initial design will change over time, and we need to ensure that changes that happen will lead to better design. I see great power in refactoring here. By doing refactoring we change design of our software to something better and still preserve clean and maintainable code. Unit testing and TDD in general is of great help here as well. It is the immediate feedback that makes us less fearful of making changes to our code. &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/5584317534254141625/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/good-design-comes-over-time.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/5584317534254141625'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/5584317534254141625'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/good-design-comes-over-time.html' title='Good design comes over time'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA-r4OubeQ2Znhq2bz57NzPwppZ7TFvzw1cDXfRobamLyF5-nebMha86yZ-tFxo_xDkUEpP5iJsUBjDDEqQwcufnTi_YvlrqE_iQ8MXn8Tk1DKg6D0EKcFmv8XhWo3E7qN9zx1puIJy1k/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-7826006154960846715</id><published>2009-08-11T11:06:00.001-07:00</published><updated>2009-08-11T11:08:03.796-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="General programming"/><title type='text'>Pseudocode Programming Process</title><content type='html'>&lt;p&gt;Today I had to implement a feature on project I&#39;m working on. The feature was not a trivial one. It was rather complex. I had a general idea how it could be implemented. The implementation I was having in mind involved several complex data structures and some recursion function calls as well.   
After short thinking about the problem I started to do the actual implementation in C#. To be more precise     I tried several implementations, but I kept failing, and found myself rewriting the code over and over. I was     completely lost in complexity of the problem and all the details (complex data structures and recursion calls). I kept doing so for about 1.5 hours (maybe two), and I still had no working solution. Then I realized that I have to change my problem solving approach. &lt;/p&gt;  &lt;p&gt;If you have read &quot;Code complete&quot; book you probably remember Pseudocode Programming Process (PPP) that Steve described in the book. Pseudocode Programming Process is a way of designing algorithms in pseudo code. Basically, this means that an algorithm is described in high-level English-like way. You can find more about the PPP on following links:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href=&quot;http://codebetter.com/blogs/darrell.norton/archive/2004/10/14/28596.aspx&quot; target=&quot;_blank&quot;&gt;Does anyone use the Pseudocode Programming Process?&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Pseudocode&quot; target=&quot;_blank&quot;&gt;Pseudocode&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href=&quot;http://www.coderookie.com/2006/tutorial/the-pseudocode-programming-process/&quot; target=&quot;_blank&quot;&gt;The Pseudocode Programming Process&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Anyway, after writing down the algorithm in high-level English and then making every line of comment into a line or fewof code (applying PPP practices), I had working solution in about 20 minutes. &lt;strong&gt;YES, 20 minutes including pseudocode and C# implementation&lt;/strong&gt;. How that compares to 1.5 hours spent for literally nothing? I can only but recommend PPP as way of handling complex algorithms.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/7826006154960846715/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/pseudocode-programming-process.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/7826006154960846715'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/7826006154960846715'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/pseudocode-programming-process.html' title='Pseudocode Programming Process'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-6142412245325016147</id><published>2009-08-06T14:22:00.001-07:00</published><updated>2009-08-06T14:28:00.838-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term=".NET"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="General programming"/><title type='text'>Bugs and bytes</title><content type='html'>&lt;p&gt;Yesterday, at work we had a bug in application we’re developing. Nothing critical but rather inconvenient. Our application has a very common functionality of file download. Some users can upload files to the system and some other users can download those files. Pretty straightforward functionality, right? It’s an ASP.NET application so it should be very easy to implement this. Here&#39;s the code (not the real code but the relevant part of it):&lt;/p&gt; &lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt; protected void ViewFile(int fileId)
       {
           byte[] fileData = DAL.GetFile(fileId);
           Response.ContentType = &amp;quot;application/octet-stream&amp;quot;;
           Response.AddHeader(&amp;quot;Content-Disposition&amp;quot;, &amp;quot;attachment; filename=test.txt&amp;quot;);
           Response.OutputStream.Write(fileData, 0, fileData.Length);
           Response.End();
       } &lt;/pre&gt; &lt;p&gt;So what could possibly go wrong here? Not much? Except…, files can be EMPTY too! An empty file is a file that is 0 bytes in size. So if you try to write an empty file with the code above an exception would be throw at Response.OutputStream.Write() line. How do we write an empty file to HttpResponse then? Very simple: you DONT write anything. Just skip Response.OutputStream.Write line and call the Response.End(). Anyway, what am I trying to prove here with this simple example? My point is that we need to be more thoughtful when writing code. We must carefully think about code we write. What could go wrong? How is API we’re using behaving? What exceptions can be thrown? What assumptions are being made? One approach I find useful with dealing this kind of issues is &lt;a href=&quot;http://en.wikipedia.org/wiki/Defensive_programming&quot; target=&quot;_blank&quot;&gt;Defensive programming&lt;/a&gt;. Here’s quote from wiki about Defensive programming: &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;A difference between defensive programming and normal practices is that few assumptions are made by the programmer, who attempts to handle all possible error states. In short, the programmer never assumes a particular function call or library will work as advertised, and so handles it in the code.&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If the code had been written in Defensive programming mind-set this bug would have never been made. I must also note that Defensive programming is not only choice for solving these kind of issues. Another approach is &lt;a href=&quot;http://en.wikipedia.org/wiki/Design_by_contract&quot; target=&quot;_blank&quot;&gt;Design by contract&lt;/a&gt;. In the end it really does not matter which approach you choose, the goal is to create more robust and quality software. &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/6142412245325016147/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/bugs-and-bytes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/6142412245325016147'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/6142412245325016147'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/bugs-and-bytes.html' title='Bugs and bytes'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-8664203976933326460</id><published>2009-08-05T07:18:00.001-07:00</published><updated>2009-08-05T10:16:48.809-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><title type='text'>Strings and performance</title><content type='html'>&lt;p&gt;I really cannot stress enough importance of using StringBuilder class for string concatenation. The reason is obvious: &lt;strong&gt;PERFORMANCE&lt;/strong&gt;!  
I know that most programmers are aware of the possible string concatenations performance problems but somehow this issue is still being overlooked and many make the mistake.The difference in performance between using and not using StringBuilder is HUGE as I&#39;ll show in simple demo application. I&#39;m not saying that you should use    StringBuilder for every string concatenation, but be very alert when you have loops that do string concatenation. Stop for a minute and think about possible performance issues. How much iteration do you expect your loop to have? If you loop through collection that you expect to get bigger     and bigger over time, or a collection that you know nothing about (such as one coming from external system) then using StringBuilder has no alternative. Let&#39;s take a look how long does it take to do for example  30 000 string concatenations. &lt;em&gt;&lt;/em&gt;by using StringBuilder. Code looks as follows : &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;class Program
  { 
      static void Main(string[] args) 
      { 
          StringBuilder sb = new StringBuilder(); 
          DateTime start = DateTime.Now;  
          for (int i = 0; i &amp;lt;30000; i++) 
          { 
              sb.Append(&quot;some string&quot;); 
          } 
          TimeSpan ts = DateTime.Now - start;  
          Console.WriteLine(&quot;Finished!&quot;); 
          Console.WriteLine(ts.TotalSeconds); 
          Console.ReadLine(); 
      } 
}&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;  &lt;p&gt;On my computer it takes exactly &lt;strong&gt;0.15625&lt;/strong&gt; seconds which is very very fast. &lt;/p&gt;  &lt;div style=&quot;text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYwariyEmlysAPy2GnE2swluZDM7YwwjIY-bgpA2rIfKJbRfBdZtJWnGpcaLFqv4LPMik-_D6vclbPyDXGkc9sYIt4H09n9wkJpojjP_x-cOobD0c6KMQJXNKyv85pcU66SOvH50DCSL8/s1600-h/image%5B5%5D.png&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border: 0px none ; display: block; float: none; &quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj7RXXDSWElZddtiGZc6selOw7OiTsIeJD9dQi5icERXgzEpJcs-js7hIDs-eYVggDMYYJqZIJLq6FflE8uM2Pc50nbRrR1RXKnD5PMP8Ozha6tmvZQsi9oBM7aST_GcO2Jj33loqIOJs/?imgmax=800&quot; width=&quot;244&quot; border=&quot;0&quot; height=&quot;132&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt; &lt;/p&gt;  &lt;p&gt;Now let&#39;s take a look at same code but without using StringBuilder &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt; class Program
  { 
      static void Main(string[] args) 
      { 
          string str= string.Empty; 
          DateTime start = DateTime.Now;  
          for (int i = 0; i &amp;lt;30000; i++) 
          { 
              str+= &quot;some string&quot;; 
          } 
          TimeSpan ts = DateTime.Now - start;  
          Console.WriteLine(&quot;Finished!&quot;); 
          Console.WriteLine(ts.TotalSeconds); 
          Console.ReadLine(); 
      } 
  }&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;  &lt;p&gt;Without StringBuilder it takes &lt;strong&gt;21.396625&lt;/strong&gt; seconds.&lt;/p&gt;  &lt;p&gt; &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCcdU0AdbLEAyX0CMEY7A8OnKESJSs8YzugjPEBslSkhF-QuDYiRc2pCgjvSqmJZfTKu59IynuEUeui73t23Uq9cDGOfz8aPGV9n2PSCHZDexF4uQxYQHu2gq361qiUPWVvEYAxV2Ds50/s1600-h/image%5B2%5D.png&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border: 0px none ; display: block; float: none;  &quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhB81TvS6VG2Eh8QFHS6hG60-SWfMBXyPJKyw6hqBjCU__FgJoJXmyMf4YJv6MrJZVb4VCL9iaUrLO07u3g5R9pr4v6aS_MXqP7grKcAzQybhIM4e3plHFY2E-Pt3xtj2h_TyBA0NLErQE/?imgmax=800&quot; width=&quot;244&quot; border=&quot;0&quot; height=&quot;106&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Difference in performance is very noticeable and benefit of using StringBuilder is obvious. It&#39;s worth noting that   performance issues are usually not discovered until system has been deployed and used in production, but by that time damage can already be done. That&#39;s why string concatenation must be taken with care. &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/8664203976933326460/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/strings-and-performance.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/8664203976933326460'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/8664203976933326460'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/strings-and-performance.html' title='Strings and performance'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj7RXXDSWElZddtiGZc6selOw7OiTsIeJD9dQi5icERXgzEpJcs-js7hIDs-eYVggDMYYJqZIJLq6FflE8uM2Pc50nbRrR1RXKnD5PMP8Ozha6tmvZQsi9oBM7aST_GcO2Jj33loqIOJs/s72-c?imgmax=800" height="72" width="72"/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-2497085478614616138</id><published>2009-08-03T02:21:00.001-07:00</published><updated>2009-08-03T07:35:05.007-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DDD"/><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><title type='text'>Tip on employing the domain model pattern</title><content type='html'>&lt;p&gt;Today, I read &lt;a href=&quot;http://msdn.microsoft.com/en-us/magazine/ee236415.aspx&quot;&gt;Udi&#39;s latest article&lt;/a&gt; about domain model pattern published in MSDN magazine. I want to comment on following part of the article: &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;When designing a domain model, spend more time looking at the specifics found in various use cases rather than jumping directly into modeling entity relationships—especially be careful of setting up these relationships for the purposes of showing the user data. That is better served with simple and straightforward database querying, with possibly a thin layer of facade on top of it for some database-provider independence.&lt;/em&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Udi couldn&#39;t be more right here. I had similar dilemma few weeks ago,and my reasoning happened to be same as Udi&#39;s. I had an association that seemed so natural and correct. Association was as follows: &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_yFxZU1YL4ESF9j_DpKOqtLdgEqRu-OueFslDDp6qY1G3PnsAu9-aWQ80R_swMsGrJLVHvow7xlAxb3hY3SJ9Ru9uyYWjcu37JzicCVfpBJSfv34XwtfZSSZTLPzPreF6iPnLcMmr-qU/s1600-h/image%5B6%5D.png&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border: 0px none ; display: block; float: none; margin-left: auto; margin-right: auto;&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvlbuys7eis4TBQ5hOkYqb6QMuIfI8l03f1zqCHF9yj7p9E_UuZZ9B816oL1DdvnPaH7bbG4ngh5oio9NAAauQar85KgVaxi8kAvmG5VbE4R93MSIAGIgSQv-YKJWI-NG1qwVssVywV5k/?imgmax=800&quot; border=&quot;0&quot; height=&quot;286&quot; width=&quot;465&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;But after looking deeper at problem I was trying to solve it turned out that this association was only needed for presentation purposes. So I dropped the association altogether from the domain model. Presentation issue was solved by using a simple query (using NHibernate). This shows that domain model should be used for capturing core business behavior only. In the article Udi also wrote about Domain Events pattern that can help solve complex problems quite elegantly.  I strongly recommend reading this article.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/2497085478614616138/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/tip-on-employing-domain-model-pattern.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/2497085478614616138'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/2497085478614616138'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/08/tip-on-employing-domain-model-pattern.html' title='Tip on employing the domain model pattern'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvlbuys7eis4TBQ5hOkYqb6QMuIfI8l03f1zqCHF9yj7p9E_UuZZ9B816oL1DdvnPaH7bbG4ngh5oio9NAAauQar85KgVaxi8kAvmG5VbE4R93MSIAGIgSQv-YKJWI-NG1qwVssVywV5k/s72-c?imgmax=800" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-6703473430933812343</id><published>2009-07-16T14:16:00.001-07:00</published><updated>2009-07-18T15:33:38.386-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term=".NET"/><category scheme="http://www.blogger.com/atom/ns#" term="ADO.NET"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Fluent interface"/><title type='text'>Fluent ADO.NET. An attempt!</title><content type='html'>&lt;p&gt;Yes, I know there is NHibernate and other decent ORM solutions but, there are times when using an ORM is not an option. Still the ADO.NET API is VERY boring thing to use. Yes, you can make some SqlHelper classes that can help you but still many of the SqlHelpers lack the needed flexibility. So, I’ll try to make a Fluent wrapper for ADO.NET API. &lt;/p&gt;  &lt;h4&gt;What are we trying to achieve?&lt;/h4&gt;  &lt;p&gt;Our goal is to make usage of ADO.NET API more friendly and easier. So let’s start with some examples of our desired code usage. &lt;/p&gt;  &lt;p&gt;Wouldn’t it be nice if we could use SqlCommand object in following way?&lt;/p&gt;  &lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;SqlCommand command = new SqlCommand();
SqlConnection connection = new SqlConnection();
command.UsingConnection(connection)
.ExecuteQuery(&quot;SELECT * FROM USERS WHERE ID=@id&quot;)
.AddParameter(&quot;@id&quot;,    1)
.AsDataTable();&lt;/pre&gt;or..
&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;SqlCommand command = new SqlCommand();
SqlConnection connection = new SqlConnection();
command.UsingConnection(connection)
.ExecuteQuery(&quot;UPDATE Users SET Active=1 WHERE ID=@id&quot;)
.AddParameter(&quot;@id&quot;,1)
.ExecuteNonQuery();&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Before we go further with this fluent ADO.NET I need to say that there is already an open source project related to ADO.NET fluent interface. As a matter of fact I had a short discussion with coordinator of the project about some design aspects of the API. The design differences between the two approaches are related to supporting different database engines. You can read about this short discussion &lt;a href=&quot;http://fluentado.codeplex.com/Thread/View.aspx?ThreadId=62643&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;h4&gt;How to achieve our goal?&lt;/h4&gt;&lt;p&gt;To achieve usage of SqlCommand as shown above, we can go and write wrapper around SqlCommand object and that would definitely work. But what if I need to use Access as data source? In that case I wouldn’t be using the SqlCommand class, I would be using OleDbCommand instead. To avoid writing wrapper around all possible (Oracle,Postgre,MySql) databases, we can instead write wrapper around IDbCommand interface which all of the specific commands implement. This approach is rather good, but has some major drawback. Problem is that we would not be able to take advantage of specific features that are available on some of the IDbCommand implementations. One such property on SqlCommand object is &quot;Notification&quot;, which is not present in IDbCommand interface. &lt;/p&gt;&lt;p&gt;To get around this problem we could make use of extension methods, which could be written in fluent approach and thus enable that specific features of IDbCommand implementations can be configured fluently. So let’s start the implementation.&lt;/p&gt;&lt;h4&gt;The implementation&lt;/h4&gt;&lt;p&gt;Please note that I will not be implementing whole API, but I’ll only show the concept. &lt;/p&gt;&lt;p&gt;Our wrapper class will be called FluentDbCommand, it is generic class that receives a IDbCommand as the type parameter. Here’s the code listing: &lt;/p&gt;&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;public class FluentDbCommand&amp;lt;T&amp;gt;
where T : IDbCommand
{
    T command = default(T);
    Func&amp;lt;T&amp;gt; commandConstructor = null;

    public T InnerCommand
    {
        get
        {
            return command;
        }
    }
    public FluentDbCommand()
    {
        // In case we don&#39;t provide any factory for creating command objects
        // try to build the object using reflection
        command = Activator.CreateInstance&amp;lt;T&amp;gt;();
    }
    public FluentDbCommand(Func&amp;lt;T&amp;gt; commandConstructor)
    {
        this.commandConstructor = commandConstructor;
        command = this.commandConstructor.Invoke();
    }

    public FluentDbCommand&amp;lt;T&amp;gt; AddParameter(string name, object value)
    {
        IDbDataParameter parameter = command.CreateParameter();
        parameter.ParameterName = name;
        parameter.Value = value;
        command.Parameters.Add(parameter);
        return this;
    }
    public FluentDbCommand&amp;lt;T&amp;gt; QueryToRun(string query)
    {
        command.CommandType = CommandType.Text;
        command.CommandText = query;
        return this;
    }
    public DataTable AsDatatable()
    {
        DataTable tblResults = new DataTable();
        tblResults.Load(command.ExecuteReader());
        return tblResults;
    }
    public FluentDbCommand&amp;lt;T&amp;gt; UseConnection(IDbConnection connection)
    {
        this.command.Connection = connection;
        return this;
    }
    public int ExecuteNonQuery()
    {
        return this.InnerCommand.ExecuteNonQuery();
    }
    public object ExecuteScalar()
    {
        return this.InnerCommand.ExecuteScalar();
    }
    public IDataReader ExecuteReader()
    {
        return this.InnerCommand.ExecuteReader();
    }
    public FluentDbCommand&amp;lt;T&amp;gt; RunStoredProcedure(string strProcedureName)
    {
        this.InnerCommand.CommandType = CommandType.StoredProcedure;
        this.InnerCommand.CommandText = strProcedureName;
        return this;
    }
}&lt;/pre&gt;&lt;p&gt;And usage of the FluentDbCommand would be:&lt;/p&gt;&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;FluentDbCommand&amp;lt;OleDbCommand&amp;gt; oleCmd = new FluentDbCommand&amp;lt;OleDbCommand&amp;gt;();

DataTable result = oleCmd.UseConnection(connection)
                 .QueryToRun(&quot;SELECT * FROM USERS&quot;)
                 .AddParameter(&quot;@id&quot;, 1)
                 .AsDatatable();&lt;/pre&gt;&lt;p&gt;We see that it’s rather simple to implement the fluent interface around IDbCommand, but as I said earlier, to support the specific features of concrete database engine we need to use extension methods. &lt;/p&gt;&lt;p&gt;For example to support NotificationAutoEnlist property of SqlCommand class we would need to write following extension method&lt;/p&gt;&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;public static class SQLServerSpecificExtensions
{
public static FluentDbCommand&amp;lt;SqlCommand&amp;gt; NotificationAutoEnlist(this FluentDbCommand&amp;lt;SqlCommand&amp;gt; command, bool value)
{
command.InnerCommand.NotificationAutoEnlist = true;
return command;
}
}&lt;/pre&gt;&lt;p&gt;And that would enable us to use the FluentDbCommand in following way: &lt;/p&gt;&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;FluentDbCommand&amp;lt;SqlCommand&amp;gt; sqlCmd = new FluentDbCommand&amp;lt;SqlCommand&amp;gt;();

sqlCmd.UseConnection(connection)
            .QueryToRun(&quot;SELECT * FROM USERS WHERE ID=@id&quot;)
            .AddParameter(&quot;@id&quot;, 1)
            .NotificationAutoEnlist(true)
            .AsDatatable();&lt;/pre&gt;&lt;p&gt;That would be it. This implementations is just a beginning of the full-blown wrapper, but it’s shows the concept very well. &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/6703473430933812343/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/07/fluent-adonet-attempt.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/6703473430933812343'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/6703473430933812343'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/07/fluent-adonet-attempt.html' title='Fluent ADO.NET. An attempt!'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8151663570574264569.post-5352449892880764634</id><published>2009-07-11T15:35:00.001-07:00</published><updated>2009-07-16T14:02:07.593-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC"/><title type='text'>Book review – Pro ASP.NET MVC (Steven Sanderson)</title><content type='html'>&lt;p&gt;&amp;#160;&lt;img style=&quot;border-bottom: 0px; border-left: 0px; margin: 10px 25px 5px 0px; display: inline; border-top: 0px; border-right: 0px&quot; title=&quot;asp.net-mvc-pro&quot; border=&quot;0&quot; alt=&quot;asp.net-mvc-pro&quot; align=&quot;left&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzNzPKnrsJoNB-qfwITed9A1B7ECahkDRuNlyve7P-pelAvlvB4-lYmKUcPhj0IJkmX-ozFAB8oZ9n1xKBVbJTFioilnoPSqRWKZEl4bqpBF-DHfcDO2RpYJ_DL6puE1krQjQf1oIzab4/?imgmax=800&quot; width=&quot;129&quot; height=&quot;168&quot; /&gt;I decided to purchase this book after seeing good reviews of this book at &lt;a href=&quot;http://www.amazon.com/Pro-ASP-NET-Framework-Steven-Sanderson/dp/1430210079/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1247351444&amp;amp;sr=8-1&quot;&gt;Amazon&lt;/a&gt;. I must say that reviews at Amazon can sometimes misguide you but this time I was lucky. This book is outstanding, it is full of details about ASP.NET MVC and the author knows his stuff. Another good thing about this book is author’s writing style, which is entertaining and easy to follow. The book is divided in two parts, first part is about building small e-commerce book with ASP.NET MVC framework and second part is about detailed explanations of ASP.NET MVC Framework inner details. As a matter of fact this book contains so much valuable technical information about MVC framework that I’ll surly need to read some parts again. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;: If you plan to do any serious development with ASP.NET MVC then this book is must-have for you. &lt;/p&gt;  &lt;p&gt;The author of this book also has a &lt;a href=&quot;http://blog.codeville.net/&quot;&gt;blog&lt;/a&gt; that is worth checking out.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://reducingcomplexity.blogspot.com/feeds/5352449892880764634/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/07/book-review-pro-aspnet-mvc-steven.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/5352449892880764634'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8151663570574264569/posts/default/5352449892880764634'/><link rel='alternate' type='text/html' href='http://reducingcomplexity.blogspot.com/2009/07/book-review-pro-aspnet-mvc-steven.html' title='Book review – Pro ASP.NET MVC (Steven Sanderson)'/><author><name>Edin</name><uri>http://www.blogger.com/profile/01552119735073632831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6z6XMXij5B114vmFIsWonGXTSYUq_8IXOJQ_VrddTWGAanVOQdyClQR_Wsu_kaAqoGpwoKVN4HdnSqxWnhxOBHWVpJR_QDzt74kpmbQKliOQxAdoFY8v5gJZ_K3didw/s220/me.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzNzPKnrsJoNB-qfwITed9A1B7ECahkDRuNlyve7P-pelAvlvB4-lYmKUcPhj0IJkmX-ozFAB8oZ9n1xKBVbJTFioilnoPSqRWKZEl4bqpBF-DHfcDO2RpYJ_DL6puE1krQjQf1oIzab4/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry></feed>