<?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>iansrobinson.com &#187; Linq</title>
	<atom:link href="http://iansrobinson.com/category/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://iansrobinson.com</link>
	<description>Ian Robinson&#039;s Blog</description>
	<lastBuildDate>Thu, 02 Sep 2010 14:22:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Linq to Sql (Some Puzzles)</title>
		<link>http://iansrobinson.com/2008/05/19/linq-to-sql-some-puzzles/</link>
		<comments>http://iansrobinson.com/2008/05/19/linq-to-sql-some-puzzles/#comments</comments>
		<pubDate>Mon, 19 May 2008 20:12:25 +0000</pubDate>
		<dc:creator>iansrobinson</dc:creator>
				<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://iansrobinson.com/?p=11</guid>
		<description><![CDATA[I’m currently trying to use Linq to SQL to model and persist some very simple domain objects. Not having used Linq to SQL before, I’m muddling through, but have quickly run up against a couple of things that have me puzzled:

Entity classes require a public default constructor
There&#8217;s no support for user-defined value types

My ignorance? Limitations [...]]]></description>
			<content:encoded><![CDATA[<p>I’m currently trying to use Linq to SQL to model and persist some very simple domain objects. Not having used Linq to SQL before, I’m muddling through, but have quickly run up against a couple of things that have me puzzled:</p>
<ul>
<li>Entity classes require a public default constructor</li>
<li>There&#8217;s no support for user-defined value types</li>
</ul>
<p>My ignorance? Limitations in the framework? You tell me.</p>
<h3>Default Public Constructors for Entity Classes</h3>
<p>I’m attributing my entity (domain) classes with <code>TableAttribute</code>, and their members with <code>ColumnAttribute</code>. <code>TableAttribute</code> maps a class to a table in the database; <code>ColumnAttribute</code> maps its members to the table’s columns.</p>
<p>To ensure that an object behaves as a good citizen from the moment it’s instantiated, I prefer to set up its state in its constructor. To further guarantee an object can’t be created in an ill-formed state, I tend not to include a default, parameterless constructor for such classes.</p>
<p>Now unless I’m mistaken, I can’t define a Linq to SQL entity class that lacks a public default constructor:</p>
<p><code>System.InvalidOperationException: The type &#039;xxx&#039; must declare a default (parameterless) constructor in order to be constructed during mapping.</code></p>
<p>Nor can I use a private parameterless constructor:</p>
<p><code>System.MissingMethodException: No parameterless constructor defined for this object.</code></p>
<p>I can make my members private, so that only Linq can call the setters during object initialisation, which is great; but I can’t make the constructor private or otherwise accessible only to Linq. Seemingly a small issue, but it means that some objects could end up being created in an invalid state.</p>
<h3>No Support for User-Defined Value Types</h3>
<p>As far as I’m aware, Linq to SQL doesn’t support user-defined value types. Value types are types that lack identity, much as an integer doesn’t have any identity: this 3 is as good as that 3. Value types serve as attributes of entities &#8211; which do possess identity. In a database, value types are often stored in the table of the entity that owns them; in an object model, they’re usually surfaced as a dependent object of the parent class they help describe.</p>
<p>To illustrate what I mean by value type, imagine we are modelling a <em>Picture</em> with <em>Description</em>, <em>Border Thickness</em> and <em>Border Style</em> attributes. We might store the <em>Picture</em> in the database like this:</p>
<p><img src="http://iansrobinson.com/wp-content/uploads/2008/05/picture-db.png" alt="Picture entity: database schema" title="Picture entity: database schema" width="341" height="142" class="alignnone size-full wp-image-13" /></p>
<p>- but represent it in code like this:</p>
<p><img src="http://iansrobinson.com/wp-content/uploads/2008/05/picture-oo.png" alt="Picture entity: OO model" title="Picture entity: object model" width="384" height="186" class="alignnone size-full wp-image-14" /></p>
<p>Here the <code>Border</code> class constitutes a user-defined value type.</p>
<p>One of the things I want to do with one of my domain objects is store a hash of a <code>string</code> member (a <code>Uri</code> in this instance) with that object’s corresponding database record. I can then index the hash, and lookup records based on the hash rather than a long (500) <code>nvarchar</code> value (which in any case can’t be indexed because it exceeds 900 bytes).</p>
<p>The hashing algorithm isn’t a concern of the entity class that exposes the hash value, so I’ve encapsulated the algorithm in a separate class, effectively making it a user-defined value object:</p>
<pre><code>public class UriHash
{
&nbsp;&nbsp;&nbsp;&nbsp;private readonly int value;

&nbsp;&nbsp;&nbsp;&nbsp;public UriHash(string uri)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = uri.GetHashCode();
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public UriHash(int value)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.value = value;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public int Value
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get { return value; }
&nbsp;&nbsp;&nbsp;&nbsp;}
}</code></pre>
<p>As you can see, I’m simply calling <code>GetHashCode()</code> on the relevant <code>string</code> member, but it may be that in the near future I’ll want to change this to use, say, an MD5 hash. When (if) that happens, I’ll have to make changes in several places: in the database schema (<code>int</code> to <code>nvarchar</code>), and in any code that gets or sets an <code>int</code> value. What I’m keen to do, even from the outset, is maintain my encapsulation of the hashing algorithm, but at the same time keep to a minimum the number of places where knowledge of the hash type (<code>int</code> versus <code>string/nvarchar</code>) is baked into the code.</p>
<p>Because Linq to SQL doesn&#8217;t currently support user-defined value types, my entity class exposes the hash as a plain old <code>int</code>, as follows:</p>
<pre><code>[Table(Name = &quot;MonitorableResource&quot;)]
public class MonitorableResource
{
&nbsp;&nbsp;&nbsp;&nbsp;// Constructors omitted

&nbsp;&nbsp;&nbsp;&nbsp;private UriHash uriHash;

&nbsp;&nbsp;&nbsp;&nbsp;[Column(DbType = &quot;Int NOT NULL&quot;, CanBeNull = false)]
&nbsp;&nbsp;&nbsp;&nbsp;internal int UriHash
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private set { uriHash = new UriHash(value); }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (uriHash == null)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uriHash = new UriHash(Uri);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return uriHash.Value;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;[Column(DbType = &quot;NVarChar(500) NOT NULL&quot;, CanBeNull = false)]
&nbsp;&nbsp;&nbsp;&nbsp;public string Uri { get; private set; }

&nbsp;&nbsp;&nbsp;&nbsp;// Other code omitted
}</code></pre>
<p>The problem here is that whilst the hashing algorithm is nicely encapsulated in the <code>UriHash</code> class, the hash type (an <code>int</code> in this instance) is smeared all over the code. The database schema knows it’s an <code>int</code>, and so does the <code>UriHash</code> class. That’s fine: there’s a one-to-one mapping between the OO and the relational worlds. But on top of that, <code>MonitorableResource</code> knows that the type is an <code>int</code>, and that its <code>UriHash</code> member maps to the <code>UriHash</code> column in the database. That’s one place too many for my liking.</p>
<p>Ideally I’d like to expose the <code>MonitorableResource</code>’s <code>UriHash</code> member as a <code>UriHash</code>, and push the <code>ColumnAttribute</code> mapping onto the <code>UriHash</code> type’s <code>Value</code> member &#8211; something like this (note, this code isn&#8217;t valid Linq to SQL):</p>
<code></code>
<p>Interestingly, the forthcoming Entity Framework does support value objects, which it calls <a href="http://msdn.microsoft.com/en-us/library/bb738472.aspx" title="Complex Type Objects (Entity Framework)" target="_blank">Complex Type Objects</a>. NHibernate, of course, has long supported <a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html#mapping-types-custom" title="Custom value types" target="_blank">custom value types</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://iansrobinson.com/2008/05/19/linq-to-sql-some-puzzles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
