<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Musings&#60;Biefeld&#62; &#187; Presenter</title>
	<atom:link href="http://sbiefeld.com/tag/presenter/feed/" rel="self" type="application/rss+xml" />
	<link>http://sbiefeld.com</link>
	<description>- curiosities of development, life, the universe and everything -</description>
	<lastBuildDate>Thu, 17 Nov 2011 04:01:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Presenter Logic &#124;&#124; Domain Service Logic &#124;&#124; Repository Logic?</title>
		<link>http://sbiefeld.com/2009/02/presenter-logic-domain-service-logic-repository-logic/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=presenter-logic-domain-service-logic-repository-logic</link>
		<comments>http://sbiefeld.com/2009/02/presenter-logic-domain-service-logic-repository-logic/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 05:22:33 +0000</pubDate>
		<dc:creator>Sean Biefeld</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Domain Service]]></category>
		<category><![CDATA[Presenter]]></category>
		<category><![CDATA[View]]></category>

		<guid isPermaLink="false">http://sbiefeld.com/?p=186</guid>
		<description><![CDATA[Obviously the answer to the titular question is yes. I have recently found myself questioning whether the logic I am coding belongs in a domain service or in the presenter. I actually found the same logic in the presenter residing the base repository. Something definitely smells wrong, almost like the putrid smell of death, lol, [...]]]></description>
			<content:encoded><![CDATA[<p><br/>
<p>Obviously the answer to the titular question is yes.</p>
<p><br/></p>
<p>I have recently found myself questioning whether the logic I am coding belongs in a <a href="http://devlicio.us/blogs/casey/archive/2009/02/17/ddd-services.aspx" target="_blank">domain service</a> or in the presenter. I actually found the same logic in the presenter residing the base repository. Something definitely <a href="http://en.wikipedia.org/wiki/Code_smell" target="_blank">smells</a> wrong, almost like the putrid smell of death, lol, nah just a <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" target="_blank">DRY</a> smell and a hint of mixed responsibility odor. The presenter was calling the Repository directly which was kinda of an indicator, but it is valid to do so, depending on the scenario.</p>
<p><br/></p>
<p>The application that I am currently working in is a web application. That being said, I feel it is valid to consider the web limitations part of the current domain, not just a concern of the presentation. If we need to move to a windows app or something else, it will take a lot of refactoring, so why not just view the web&#8217;s issues as part of what affects the domain. Besides, is the purpose of the domain to be abstract enough to support multiple platforms or to dimish complexity? Anyway, that&#8217;s another discussion altogether, and I&#8217;m digressing.</p>
<p><br/></p>
<p>Here&#8217;s the skinny, that&#8217;s a valid colloquialism isn&#8217;t it. I found string to integer conversion happening in two different places. Once in the presenter, grabbing a string Id from the view, converting it, calling an overloaded GetById method from the repository or throwing an exception if the Id was invalid. The overloaded GetById method was in the base repository, it either accepted a string Id or integer Id, if the string Id was invalid it was throwing an exception. Yikes, this is scary, and to think I was the one that coded all this, frightening, I know. I am recovering so don&#8217;t you worry yourself. Now to the current code:</p>
<h3>Code to be Refactored:</h3>
<h4>Presenter</h4>
<pre style="background-color: #000;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #aaa; font-size: 10pt;">
<span style="color: #df8000;">public virtual void</span> InitializeView()
{
	<span style="color: #df8000;">if</span>(TreatmentIdIsValid())
		LoadTreatment();
	<span style="color: #df8000;">else</span>
		<span style="color: #df8000;">throw new</span> <span style="color: #2091af;">ApplicationException</span>(<span style="color: #df8000;">string</span>.Format(<span style="color: #800000;">"A Record of Waste cannot be completed because of the invalid treatment id: {0}"</span>, View.TreatmentId));
}

<span style="color: #df8000;">private bool</span> TreatmentIdIsValid()
{
	<span style="color: #df8000;">int</span> validTreatmentId; 

	<span style="color: #df8000;">bool</span> treatmentIdIsValid = <span style="color: #df8000;">int</span>.TryParse(View.TreatmentId, <span style="color: #df8000;">out</span> validTreatmentId); 

	<span style="color: #df8000;">if</span>(treatmentIdIsValid)
		CurrentTreatmentId = id; 

	<span style="color: #df8000;">return</span> treatmentIdIsValid;
} 

<span style="color: #df8000;">protected virtual void</span> LoadTreatment()
{
	<span style="color: #df8000;">try</span>
	{
		CurrentTreatment = <span style="color: #2091af;">Repository</span>&lt;<span style="color: #2091af;">ITreatmentRepository</span>&gt;.GetById(CurrentTreatmentId);
	}
	<span style="color: #df8000;">catch</span>
	{
		<span style="color: #df8000;">throw new</span> <span style="color: #2091af;">ApplicationException</span>(<span style="color: #800000;">"Could not retrieve the specified treatment"</span>);
	}
}
</pre>
<h4>Base Repository</h4>
<pre style="background-color: #000;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #aaa; font-size: 10pt;">
<span style="color: #df8000;">public virtual</span> Entity GetById(<span style="color: #df8000;">string</span> id)
{
	<span style="color: #df8000;">int</span> parsedId;

	<span style="color: #df8000;">if</span> (!<span style="color: #df8000;">int</span>.TryParse(id, <span style="color: #df8000;">out</span> parsedId))
		<span style="color: #df8000;">throw new</span> <span style="color: #2091af;">ApplicationException</span>(<span style="color: #800000;">"Could not convert the given id: "</span> + id + <span style="color: #800000;">" into an integer"</span>);

	<span style="color: #df8000;">return</span> GetById(parsedId);
}

<span style="color: #df8000;">public virtual</span> Entity GetById(<span style="color: #df8000;">int</span> id)
{
	<span style="color: #df8000;">return</span> Session.Get&lt;Entity&gt;(id);
}
</pre>
<p>I think that there is no place for logic in the repository it should be left to the domain service. You could even argue that this functionality is common and can be moved to a domain utility. For ease I am going to move it to a domain service. Now, lettuce see the refactoring to the code above:</p>
<h3>Refactored Code:</h3>
<h4>Presenter</h4>
<pre style="background-color: #000;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #aaa; font-size: 10pt;">
<span style="color: #df8000;">public virtual void</span> InitializeView()
{
	LoadTreatment();
}

<span style="color: #df8000;">protected virtual void</span> LoadTreatment()
{
	CurrentTreatment = <span style="color: #2091af;">RecordOfWasteService</span>.GetParentTreatmentById(CurrentTreatmentId);
}
</pre>
<h4>Domain Service</h4>
<pre style="background-color: #000;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #aaa; font-size: 10pt;">
<span style="color: #df8000;">public virtual</span> <span style="color: #2091af;">Treatment</span> GetParentTreatmentById(<span style="color: #df8000;">string</span> id)
{
	<span style="color: #df8000;">int</span> validTreatmentId;

	<span style="color: #df8000;">if</span> (!<span style="color: #df8000;">int</span>.TryParse(id, <span style="color: #df8000;">out</span> validTreatmentId))
		<span style="color: #df8000;">throw new</span> <span style="color: #2091af;">ApplicationException</span>(<span style="color: #800000;">"Could not convert the given treatment id: "</span> + id + <span style="color: #800000;">" into an integer"</span>);

	<span style="color: #df8000;">return</span> GetParentTreatmentById(validTreatmentId);
}

<span style="color: #df8000;">protected virtual </span><span style="color: #2091af;">Treatment</span> GetParentTreatmentById(<span style="color: #df8000;">int</span> treatmentId)
{
	<span style="color: #df8000;">try</span>
	{
		CurrentTreatment = <span style="color: #2091af;">Repository</span>&lt;<span style="color: #2091af;">ITreatmentRepository</span>&gt;.GetById(treatmentId);
	}
	<span style="color: #df8000;">catch</span>
	{
		<span style="color: #df8000;">throw new</span> <span style="color: #2091af;">ApplicationException</span>(<span style="color: #800000;">"Could not retrieve the specified treatment"</span>);
	}
}
</pre>
<h4>Base Repository</h4>
<pre style="background-color: #000;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #aaa; font-size: 10pt;">
<span style="color: #df8000;">public virtual</span> Entity GetById(<span style="color: #df8000;">int</span> id)
{
	<span style="color: #df8000;">return</span> Session.Get&lt;Entity&gt;(id);
}
</pre>
<p>Alrighty then, we got any logic out of the repository, I&#8217;m feeling better already, my face has gone from grimace to grin, and no not the McDonlad&#8217;s character Grimace. <a href="http://www.youtube.com/watch?v=xf69EEL3WBk" target="_blank">Super serial</a>, a la Al Gore about ManBearPig, what was <a href="http://sbiefeld.com/Stuff/grimace.jpg" target="_blank" rel="lightbox[186]">Grimace</a>, was he what you turn in to if you only eat McDonalds and nothing else?</p>
<p><br/></p>
<p>The responsibility of the repository should be to read and write to persistence/web services/messages etc. The string validation logic is in the domain service, I may pull it out to a base service or utility service. Our presenter is so much simpler now, and not worried about logic that it shouldn&#8217;t have to worry about. Hmm, the cleanliness is delightful. There is no more duplication of logic in the presenter and repository, w00t! Now let me know your thoughts, comments, opinions etc. of dissent or agreement, it will help me and hopefully others learn and grow. I&#8217;m off to watch the some <a href="http://en.wikipedia.org/wiki/Teenage_Mutant_Ninja_Turtles_(1987_TV_series)" target="_blank">Teenage Mutant Ninja Turtle original series</a>, wow, I&#8217;m a nerd.</p>
]]></content:encoded>
			<wfw:commentRss>http://sbiefeld.com/2009/02/presenter-logic-domain-service-logic-repository-logic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

