<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Changing type, the state pattern and LINQ to SQL</title>
	<atom:link href="http://damieng.com/blog/2009/01/05/changing-type-the-state-pattern-and-linq-to-sql/feed" rel="self" type="application/rss+xml" />
	<link>http://damieng.com/blog/2009/01/05/changing-type-the-state-pattern-and-linq-to-sql?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=changing-type-the-state-pattern-and-linq-to-sql</link>
	<description>A .NET developer in silicon valley</description>
	<lastBuildDate>Sun, 25 Dec 2011 15:10:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Gareth Jones</title>
		<link>http://damieng.com/blog/2009/01/05/changing-type-the-state-pattern-and-linq-to-sql#comment-32574</link>
		<dc:creator>Gareth Jones</dc:creator>
		<pubDate>Thu, 17 Dec 2009 20:34:11 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=1126#comment-32574</guid>
		<description>While I think it&#039;s true that its a smell if entities in a LOB application change their type, I think it&#039;s actually pretty common in a knowledge manipulation/creative application.

I&#039;ve lost count of the times I&#039;ve wanted to change a geometry to an image or a struct to a class or similar choices.

The state pattern can work here, although it primarily concerns itself with behavior.  In these knowledge-based apps, data frequently changes as well, so you need to have some sort of exchange mechanism where you can move the subset of data that is applicable across. Also the object then needs to change its set of attributes for later manipulation.  That implies that you have a dynamic enough typing mechanism that the aggregated state object can appear to project its properties up to the parent object for anyone that cares - otherwise you&#039;re breaking your encapsulation. I&#039;ve never tried mapping that to a RDBMS - interesting topic.</description>
		<content:encoded><![CDATA[<p>While I think it&#8217;s true that its a smell if entities in a LOB application change their type, I think it&#8217;s actually pretty common in a knowledge manipulation/creative application.</p>
<p>I&#8217;ve lost count of the times I&#8217;ve wanted to change a geometry to an image or a struct to a class or similar choices.</p>
<p>The state pattern can work here, although it primarily concerns itself with behavior.  In these knowledge-based apps, data frequently changes as well, so you need to have some sort of exchange mechanism where you can move the subset of data that is applicable across. Also the object then needs to change its set of attributes for later manipulation.  That implies that you have a dynamic enough typing mechanism that the aggregated state object can appear to project its properties up to the parent object for anyone that cares &#8211; otherwise you&#8217;re breaking your encapsulation. I&#8217;ve never tried mapping that to a RDBMS &#8211; interesting topic.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damien Guard</title>
		<link>http://damieng.com/blog/2009/01/05/changing-type-the-state-pattern-and-linq-to-sql#comment-10814</link>
		<dc:creator>Damien Guard</dc:creator>
		<pubDate>Thu, 08 Jan 2009 23:01:30 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=1126#comment-10814</guid>
		<description>Unfortunately business rules these days are very complex and the database is unsuited to advanced programming environments/language/contructs that would be required not to mention the issues and costs involved in scaling those out compared to putting the logic in business layers.

It&#039;s the same server-v-client argument that has caused the swings back and forth over the last x number of years.

[)amien</description>
		<content:encoded><![CDATA[<p>Unfortunately business rules these days are very complex and the database is unsuited to advanced programming environments/language/contructs that would be required not to mention the issues and costs involved in scaling those out compared to putting the logic in business layers.</p>
<p>It&#8217;s the same server-v-client argument that has caused the swings back and forth over the last x number of years.</p>
<p>[)amien</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Young</title>
		<link>http://damieng.com/blog/2009/01/05/changing-type-the-state-pattern-and-linq-to-sql#comment-10812</link>
		<dc:creator>Robert Young</dc:creator>
		<pubDate>Thu, 08 Jan 2009 22:20:47 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=1126#comment-10812</guid>
		<description>It&#039;s still too bad that most ORM folk come from the Coding world rather than the Database world.  For example, 

Hitting the discriminator directly

has 3 cases which you worry about.  As one from the Database world, the answer is straightforward:  these are constraints, stored in the catalog as plain text, and can be read from said catalog either at compile time or run time or both and turned into local Edit Checks as desired.  There are products in existence which do so.  

The advantage:  by having data and its constraints of record stored together in the Database Engine leaves no room for execution error due to sync-ing among however many tiers one chooses to use.  All constraints which are applied in a tier are validated against the catalog.  This is the whole point of storing to a RDBMS; storing to bespoke files (xml or VSAM or ...) will always be faster.  Less power for coders, but too bad.

I just did some looking elsewhere, and it seems that LINQ is not designed to generate validation from the catalog, beyond simple stuff.  Really too bad; it would be killer if it did.

In the Database world, the Prime Directive:  a row is a business rule.  Date wrote a book about that, although it is obvious.</description>
		<content:encoded><![CDATA[<p>It&#8217;s still too bad that most ORM folk come from the Coding world rather than the Database world.  For example, </p>
<p>Hitting the discriminator directly</p>
<p>has 3 cases which you worry about.  As one from the Database world, the answer is straightforward:  these are constraints, stored in the catalog as plain text, and can be read from said catalog either at compile time or run time or both and turned into local Edit Checks as desired.  There are products in existence which do so.  </p>
<p>The advantage:  by having data and its constraints of record stored together in the Database Engine leaves no room for execution error due to sync-ing among however many tiers one chooses to use.  All constraints which are applied in a tier are validated against the catalog.  This is the whole point of storing to a RDBMS; storing to bespoke files (xml or VSAM or &#8230;) will always be faster.  Less power for coders, but too bad.</p>
<p>I just did some looking elsewhere, and it seems that LINQ is not designed to generate validation from the catalog, beyond simple stuff.  Really too bad; it would be killer if it did.</p>
<p>In the Database world, the Prime Directive:  a row is a business rule.  Date wrote a book about that, although it is obvious.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damien Guard</title>
		<link>http://damieng.com/blog/2009/01/05/changing-type-the-state-pattern-and-linq-to-sql#comment-10807</link>
		<dc:creator>Damien Guard</dc:creator>
		<pubDate>Wed, 07 Jan 2009 19:44:49 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=1126#comment-10807</guid>
		<description>The concept of making the state pattern a first-class citizen in an ORM is an interesting one and if type discriminators are supported then why not?

I assume by session you mean the data gateway and identity map?  If so, the problem there is when there are multiple references to a single entity object that is now taking part in a unit of work.  

It would be possible to create a copy of the entities as they participate to ensure changes are isolated and then apply those back to the original entity instance once commited.  Having to specifically enroll entities in the unit of work and know they won&#039;t propogate out would be a bit of a mind-shift for many but worth it in my opinion.

[)amien</description>
		<content:encoded><![CDATA[<p>The concept of making the state pattern a first-class citizen in an ORM is an interesting one and if type discriminators are supported then why not?</p>
<p>I assume by session you mean the data gateway and identity map?  If so, the problem there is when there are multiple references to a single entity object that is now taking part in a unit of work.  </p>
<p>It would be possible to create a copy of the entities as they participate to ensure changes are isolated and then apply those back to the original entity instance once commited.  Having to specifically enroll entities in the unit of work and know they won&#8217;t propogate out would be a bit of a mind-shift for many but worth it in my opinion.</p>
<p>[)amien</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Campbell</title>
		<link>http://damieng.com/blog/2009/01/05/changing-type-the-state-pattern-and-linq-to-sql#comment-10806</link>
		<dc:creator>Steve Campbell</dc:creator>
		<pubDate>Wed, 07 Jan 2009 18:30:20 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=1126#comment-10806</guid>
		<description>When I was writing a custom-ORM some time ago, I had an identical concept.  Boolean flags (like &quot;Active&quot;) are the uncommon, simple case.  The more general case in an ORM model is that you really have to carefully consider every discriminator value in your types, as to whether it is constant over the lifetime of an instance, or it can change.  If it is constant, then inheritance is the simpler pattern.  If it is not, then States are a better pattern.

In my case, I was able to focus considerable effort on making States into a first-class citizen in the ORM.  This enabled things like exposing optimized collections like &quot;Customer.ActiveAccounts&quot;, and also having the change of the discriminator automatically move the account from Customer.ActiveAccounts to Customer.NonActiveAccounts.  

A more realistic example is that you have a status-discriminator with multiple possible values, and that status is stored in a separate table than the main account data.  In addition, you have some other discriminator flags.  It can easily be that &quot;Active&quot; is a broad concept which must be derived from multiple discriminators.  For example, Active is true when &quot;open-flag=true and status!=suspended&quot;.  Consider the value of an automatically available (not hand-coded) Customer.ActiveAccounts in this context - it is a very low-friction way of working with data.  

My point is that the State pattern *should* be a first-class citizen in any ORM model.  Without it, you have a fair amount of friction (for example having to manually code the simple boolean logic for my &quot;Active&quot; example) and performance issues (because the ORM cannot auto-generate a query for &quot;Active&quot; accounts the way it could if it were a &quot;Type&quot; discriminator).  

Score -1 for every commercial and open-source ORM I&#039;ve encountered so far.  Does anyone know if there an ORM that even knows about States?  While I&#039;m here, does anyone know if there is one that does not couple the orthogonal concepts of Session and Unit of Work?  (You can probably tell that I have a very low opinion of the ORMs I have encountered).</description>
		<content:encoded><![CDATA[<p>When I was writing a custom-ORM some time ago, I had an identical concept.  Boolean flags (like &#8220;Active&#8221;) are the uncommon, simple case.  The more general case in an ORM model is that you really have to carefully consider every discriminator value in your types, as to whether it is constant over the lifetime of an instance, or it can change.  If it is constant, then inheritance is the simpler pattern.  If it is not, then States are a better pattern.</p>
<p>In my case, I was able to focus considerable effort on making States into a first-class citizen in the ORM.  This enabled things like exposing optimized collections like &#8220;Customer.ActiveAccounts&#8221;, and also having the change of the discriminator automatically move the account from Customer.ActiveAccounts to Customer.NonActiveAccounts.  </p>
<p>A more realistic example is that you have a status-discriminator with multiple possible values, and that status is stored in a separate table than the main account data.  In addition, you have some other discriminator flags.  It can easily be that &#8220;Active&#8221; is a broad concept which must be derived from multiple discriminators.  For example, Active is true when &#8220;open-flag=true and status!=suspended&#8221;.  Consider the value of an automatically available (not hand-coded) Customer.ActiveAccounts in this context &#8211; it is a very low-friction way of working with data.  </p>
<p>My point is that the State pattern *should* be a first-class citizen in any ORM model.  Without it, you have a fair amount of friction (for example having to manually code the simple boolean logic for my &#8220;Active&#8221; example) and performance issues (because the ORM cannot auto-generate a query for &#8220;Active&#8221; accounts the way it could if it were a &#8220;Type&#8221; discriminator).  </p>
<p>Score -1 for every commercial and open-source ORM I&#8217;ve encountered so far.  Does anyone know if there an ORM that even knows about States?  While I&#8217;m here, does anyone know if there is one that does not couple the orthogonal concepts of Session and Unit of Work?  (You can probably tell that I have a very low opinion of the ORMs I have encountered).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://damieng.com/blog/2009/01/05/changing-type-the-state-pattern-and-linq-to-sql#comment-10802</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Wed, 07 Jan 2009 09:53:38 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=1126#comment-10802</guid>
		<description>I&#039;d argue that account types are not universally fundamental. The distinction between &#039;checking&#039; and &#039;savings&#039; is an American thing, in that there are Federal laws that say you can&#039;t pay interest on a checking account. Not so in other countries. In the UK a &#039;Current&#039; account isn&#039;t anywhere near as restricted a definition as a &#039;Checking&#039; account, and the line between &#039;Current&#039; and &#039;Savings&#039; is regularly very blurry.</description>
		<content:encoded><![CDATA[<p>I&#8217;d argue that account types are not universally fundamental. The distinction between &#8216;checking&#8217; and &#8216;savings&#8217; is an American thing, in that there are Federal laws that say you can&#8217;t pay interest on a checking account. Not so in other countries. In the UK a &#8216;Current&#8217; account isn&#8217;t anywhere near as restricted a definition as a &#8216;Checking&#8217; account, and the line between &#8216;Current&#8217; and &#8216;Savings&#8217; is regularly very blurry.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damien Guard</title>
		<link>http://damieng.com/blog/2009/01/05/changing-type-the-state-pattern-and-linq-to-sql#comment-10799</link>
		<dc:creator>Damien Guard</dc:creator>
		<pubDate>Tue, 06 Jan 2009 16:31:46 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=1126#comment-10799</guid>
		<description>The terms of bank accounts tends to change from time to time, interest rates etc. but I&#039;ve never had a bank account change it&#039;s fundamental type...

[)amien</description>
		<content:encoded><![CDATA[<p>The terms of bank accounts tends to change from time to time, interest rates etc. but I&#8217;ve never had a bank account change it&#8217;s fundamental type&#8230;</p>
<p>[)amien</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://damieng.com/blog/2009/01/05/changing-type-the-state-pattern-and-linq-to-sql#comment-10798</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Tue, 06 Jan 2009 12:01:44 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=1126#comment-10798</guid>
		<description>Funnily enough I&#039;ve had my bank accounts change type many times, due to the bank running promotional new types of account, time-limited sign-ups that then revert to other types etc. But obviously your use of the State pattern could equally be used for that (or the Strategy pattern). The GoF is one book that just keeps on giving.

There&#039;s actually not a lot of practical difference between the State and Strategy patterns. The only real difference is that Strategy tends to hold no state of its own, it&#039;s a pure functional object and can therefore be shared between stateful instances, swapped in and out more freely, while the State pattern is strongly associated with parent stateful structures.</description>
		<content:encoded><![CDATA[<p>Funnily enough I&#8217;ve had my bank accounts change type many times, due to the bank running promotional new types of account, time-limited sign-ups that then revert to other types etc. But obviously your use of the State pattern could equally be used for that (or the Strategy pattern). The GoF is one book that just keeps on giving.</p>
<p>There&#8217;s actually not a lot of practical difference between the State and Strategy patterns. The only real difference is that Strategy tends to hold no state of its own, it&#8217;s a pure functional object and can therefore be shared between stateful instances, swapped in and out more freely, while the State pattern is strongly associated with parent stateful structures.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

