<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><!-- generator="wordpress/2.3.1" --><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Lost In Tangent</title>
	<link>http://lostintangent.com</link>
	<description>Code is never finished, only abandoned...</description>
	<pubDate>Sat, 20 Jun 2009 06:13:57 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/LostInTangent" type="application/rss+xml" /><item>
		<title>How the DataContext can change your data and your life (well, sort of, but not really)…</title>
		<link>http://feedproxy.google.com/~r/LostInTangent/~3/d25QvBlbLd8/</link>
		<comments>http://lostintangent.com/2009/06/20/how-the-datacontext-can-change-your-data-and-your-life-well-sort-of-but-not-really/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 05:32:09 +0000</pubDate>
		<dc:creator>Jonathan Carter</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[ASP.NET AJAX]]></category>

		<guid isPermaLink="false">http://lostintangent.com/2009/06/20/how-the-datacontext-can-change-your-data-and-your-life-well-sort-of-but-not-really/</guid>
		<description><![CDATA[In the previous post I explained how the DataContext can be used for read-only scenarios, both in a standalone fashion and in conjunction with a DataView. I explained how the additional level of abstraction over an underlying service proxy can be beneficial, but ultimately alluded to the fact that the DataContext’s true power lies in [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous post I explained how the DataContext can be used for read-only scenarios, both in a standalone fashion and in conjunction with a DataView. I explained how the additional level of abstraction over an underlying service proxy can be beneficial, but ultimately alluded to the fact that the DataContext’s true power lies in its unit of work behavior that can be achieved pretty trivially. In this post I want to dig into how the change tracking and data persistence functionality of the DataContext works.</p>
<p>Let’s pick up where we left off in the previous post. We had an ASMX service already in place for returning customer data. Using ASP.NET AJAX 4 we could create a DataContext that pointed at the service and associate it with a DataView to dynamically display all customers returned from the service. One thing that we didn’t mention is the DataContext’s saveOperation property. We could modify our previous example like so…</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> dataContext = $create<span style="color: #66cc66;">&#40;</span>Sys.<span style="color: #006600;">Data</span>.<span style="color: #006600;">DataContext</span>, <span style="color: #66cc66;">&#123;</span> 
                               serviceUri: <span style="color: #3366CC;">&quot;Customers.asmx&quot;</span>, 
                               saveOperation: <span style="color: #3366CC;">&quot;SaveCustomers&quot;</span> 
                          <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;       
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> customersTemplate = $create<span style="color: #66cc66;">&#40;</span>Sys.<span style="color: #006600;">UI</span>.<span style="color: #006600;">DataView</span>, <span style="color: #66cc66;">&#123;</span> 
                            autoFetch: <span style="color: #003366; font-weight: bold;">true</span>, 
                            dataProvider: dataContext, 
                            fetchOperation: <span style="color: #3366CC;">&quot;GetCustomers&quot;</span>, 
                            fetchParameters: <span style="color: #66cc66;">&#123;</span> count: <span style="color: #CC0000;">10</span> <span style="color: #66cc66;">&#125;</span> 
                        <span style="color: #66cc66;">&#125;</span>, 
                        <span style="color: #003366; font-weight: bold;">null</span>, 
                        <span style="color: #003366; font-weight: bold;">null</span>, 
                        $get<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;customers-template&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Notice that nothing has changed from the perspective of the DataView declaration. The only difference between the above sample and the read-only scenario we created in the previous post is the existence of the saveOperation property on the DataContext. Here we’re telling the DataContext that there is a method called “SaveCustomers” on our ASMX service that is responsible for handling the persistence of changes. When we call the saveChanges method on the DataContext, it will trigger a request to the save operation method, passing it a change set that represents all of the changes made to the local data since the last time it was saved.</p>
<p>Two questions arise here that need to be addressed before moving on:</p>
<ol>
<li>How is it that changes are happening to the data coming back from the DataContext?</li>
<li>How is it that the DataContext can know which exact changes have happened when trying to persist them back to the server?</li>
</ol>
<p>Remember in the previous post that I mentioned that the DataContext was basically a class that can connect to a server-side resource, as long as that resource serves JSON data? That technically isn’t true, since the DataContext could work just fine with services that return XML or other payload formats. The reason I limited the DataContext’s abilities is because the only type of data that it can provide change tracking on is JSON data, and since change tracking is the key feature of the DataContext, it should almost be viewed as a limitation.</p>
<p>The main value of services that return JSON data is that once that data is returned to the JavaScript client it can immediately begin using it since that data is represented by instances of objects, as opposed to XML that would need to be parsed. To answer the first question above, since the DataContext is retrieving JSON data from services, and JSON data is simply object instances in memory, changes can happen to the data as easily as modifying property values on the returned objects. But, if your using a DataContext simply to associate it with a DataView (or some other control), you’re not necessarily handling the returned data yourself, but rather, the DataView is. Let’s take a brief detour into client templates…</p>
<p>In all of the samples that I’ve been using thus far I’ve instantiated a DataView around an element called “customers-template”. What exactly is “customers-template”? It is an instance of a client template (another new feature in ASP.NET AJAX 4) that is responsible for displaying instances of customers. The customers client template, in our example, looks like this…</p>

<div class="wp_syntax"><div class="code"><pre>&lt;table cellspacing=&quot;0&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;Title&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Middle Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Suffix&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody id=&quot;customers-template&quot; class=&quot;sys-template&quot;&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;{binding Title}&quot; /&gt;&lt;/td&gt;
            &lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;{binding FirstName}&quot; /&gt;&lt;/td&gt;
            &lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;{binding MiddleName}&quot; /&gt;&lt;/td&gt;
            &lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;{binding LastName}&quot; /&gt;&lt;/td&gt;
            &lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;{binding Suffix}&quot; /&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;</pre></div></div>

<p>What we have here is a template that represents a tabular display of customers, including the full name, title, and suffix. The part that represents the actual client template is the &lt;tbody&gt; element. Notice that the ID of the &lt;tbody&gt; corresponds to the ID of the element that is represented by the DataView control we created. What the DataView will do is ask its corresponding DataContext for data (which will call its respective service), instantiate an instance of its associated client template, and create an instance of the template for every instance of data that is retrieved from the DataContext. Hence, in our example, we will get a table row for every customer.</p>
<p>In order to actually interpolate data into the client template you use what are called bindings. ASP.NET AJAX’s bindings look very similar to those in WPF/SL, and are represented by the contents of the value attributes of the &lt;input&gt; elements in the example above. Bindings are a fairly complex topic that I won’t get into it this post, but for the sake of our sample you can think of bindings as being able to synchronize two property’s values together. In this case, we’re synchronizing the value property of the textboxes to individual properties of the customer objects (remember that we’re getting back JSON data, which includes properties for all of the customer’s assets).</p>
<p>If we were to run this sample as is right now, we’d get something like this (I have some basic CSS in place as well)…</p>
<p><img border="0" src="http://lostintangent.com/wp-content/uploads/2009/06/image.png" alt="image" style="display: inline; border-width: 0px" title="image" height="270" width="639" /></p>
<p>As expected, we have a table with a row for each customer, and the customer’s name, title, and suffix are bound to individual textboxes. This is great for read-only scenarios so far, but since we’re using textboxes for this display, it would be great to be able to edit the customer data and push it back to the server. Because we used bindings to attach the textbox values to customer properties, when you modify the value of any of the textboxes in this table you’re also modifying the value of its respective customer object. This is a very subtle but very powerful feature of client templates and bindings. This also answers our question of how you can go about changing your data when using it in conjunction with a DataContext and DataView.</p>
<p>Let’s move on to the second question of how it is the DataContext can know about the changes. The act of binding data to controls is by no means anything new. Binding a customer to a row, and then each of its properties to textboxes is a pretty basic act. But, what we really need, is for each of the individual customer instances to be able to report back to their “parent” DataContext when they’ve changed. If I go up into the form above and enter “Sr.” into the Suffix textbox for Orlando Gee, I need that customer instance that represents Orlando Gee to notify the DataContext that he has changed. That way, whenever I call the DataContext’s saveChanges method, it would already know what changes have happened and can send them to the service.</p>
<p>If you were using WPF or WinForms or Silverlight, you would have to make your Customer class implement an interface called INotifyPropertyChanged. The whole purpose of this interface is to make objects capable of notifying some interested party when it has changed. This approach works alright but it is also painful and is frankly a gleaming example of the constraints of statically typed languages. JavaScript is a dynamic language and as such we shouldn’t be held down by such limitations. But the need still exists to have our customer objects report their changes back to the DataContext. How will that happen?</p>
<p><strong><font color="#ff8000">Attention: this entire paragraph is very key</font></strong>…whenever the DataContext is used to retrieve data from a service, it will automatically inject every JSON instance with the necessary logic so that it can report any changes back to the DataContext. The above example would flow something like this…</p>
<ol>
<li>The DataView asks its associated DataContext for data</li>
<li>The DataContext requests its associated service for the data, and once retrieved injects the objects with change tracking behavior</li>
<li>The DataView then creates an instance of its associated client template for every instance of data it received from its DataContext and binds the data instance to the client template instance</li>
<li>If you modify data through the client template UI (i.e. a textbox), since the JSON object instance is bound to the UI, and the JSON objects contain change tracking logic, those changes will trigger a notification to the DataContext letting it know all of the changes</li>
<li>When you call DataContext.saveChanges, it already knows about every change that has happened (by virtue of the binding notifications), and can simply send that change set to the service</li>
<li>The DataContext will then call the service method specified in the “saveOperation” property, passing the change set</li>
</ol>
<p>What this means is that you don’t have to worry about change tracking or data synchronization at all,  which is great because those are two of the most painful parts of developing data-driven applications (in my opinion). On the service-side you simply need to create the corresponding method to receive the save operation. For our ASMX service it could look like this…</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>WebMethod<span style="color: #000000;">&#93;</span> 
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SaveCustomers<span style="color: #000000;">&#40;</span>List&lt;Change&lt;Customer&gt;&gt;&gt; changeSet<span style="color: #000000;">&#41;</span> 
<span style="color: #000000;">&#123;</span> 
    <span style="color: #008080; font-style: italic;">// Save the customer data </span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The Change class is a simple container class that wraps the data instance that was changed and the change that occured (e.g. Insert, Update, Delete). Within that service method we can now take the appropriate actions to persist the changes based on whatever our data store is.</p>
<p>While ASMX and WCF provide a great option for general-purpose services, if what you need is a service for the sole purpose of retrieving and persisting data then using either ASMX or WCF is a roundabout way to go. ADO.NET Data Services should be seen as the defacto option for data-centric services since it does most of the heavy lifting for you. In fact, if you’re using a DataContext with a DataView and a Data Service (that’s a lot of data stuff) then you don’t have to do anything extra in order to persist your changes. Data Services automatically includes the logic to receive a change set and push it back to your data store, which alleviates the need to create a save operation.</p>
<p>So what is the moral of the story? The DataContext class can be used for read-only service interaction, but truly shines when you need automatic change tracking and unit of work behavior. You can use the base DataContext class when working with an RPC-style service (i.e. ASMX/WCF), but in most cases, if you’re developing a data-centric application, using the AdoNetDataContext along with an ADO.NET Data Service is going to provide you with a much easier solution. The DataContext works beautifully with a DataView and allows you to create dynamic data-driven UI, that when used with live bindings, can provide a really compelling user experience.</p>
<p>There is though still an outstanding question here: what if you’re using ASP.NET MVC and you want to leverage controllers to handle all incoming requests? There are plenty of scenarios where you don’t want to have to create an ASMX/WCF/Data service just to provide data to an AJAX client, when your controllers are perfectly good for that (in fact you could argue that in a “true” MVC application, the whole point of the controller role is to intercept user interaction, so using another service type is slightly awkward). Remember in the last post how I mentioned that the DataContext was also a great foundation for creating more specific context’s for additional scenarios? In the next post I’ll show how we created a DataContext that provides full change tracking and persistence when using ASP.NET MVC.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostintangent.com/2009/06/20/how-the-datacontext-can-change-your-data-and-your-life-well-sort-of-but-not-really/feed/</wfw:commentRss>
		<feedburner:origLink>http://lostintangent.com/2009/06/20/how-the-datacontext-can-change-your-data-and-your-life-well-sort-of-but-not-really/</feedburner:origLink></item>
		<item>
		<title>Gaining some context into ASP.NET AJAX 4’s DataContext…</title>
		<link>http://feedproxy.google.com/~r/LostInTangent/~3/3d_mM6oIcH8/</link>
		<comments>http://lostintangent.com/2009/06/19/gaining-some-context-into-aspnet-ajax-4s-datacontext/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 05:17:00 +0000</pubDate>
		<dc:creator>Jonathan Carter</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[ASP.NET AJAX]]></category>

		<guid isPermaLink="false">http://lostintangent.com/2009/06/19/gaining-some-context-into-aspnet-ajax-4s-datacontext/</guid>
		<description><![CDATA[The ASP.NET AJAX 4 release has some really cool features in it that can help lower the barrier of entry into developing client-side web applications (jQuery doesn’t hurt either). One of the more compelling new classes is the DataContext. Basically, the DataContext is an object that is capable of consuming a server-side resource that serves [...]]]></description>
			<content:encoded><![CDATA[<p>The ASP.NET AJAX 4 release has some really cool features in it that can help lower the barrier of entry into developing client-side web applications (<a href="http://www.jquery.com">jQuery</a> doesn’t hurt either). One of the more compelling new classes is the DataContext. Basically, the DataContext is an object that is capable of consuming a server-side resource that serves JSON data. In its most basic form, you simply give it the URI of a service and the operation name to execute and it handles making the underlying request. If you had an AJAX-enabled ASMX service like so (note: I’m using the Entity Framework)…</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>ScriptService<span style="color: #000000;">&#93;</span> 
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomersAsmx : WebService <span style="color: #000000;">&#123;</span> 
    <span style="color: #0600FF;">private</span> CustomersContext context = <span style="color: #008000;">new</span> CustomersContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; 
&nbsp;
    <span style="color: #000000;">&#91;</span>WebMethod<span style="color: #000000;">&#93;</span> 
    <span style="color: #0600FF;">public</span> Customer<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> GetCustomers<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> count<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> 
        var customers = <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">from</span> cus <span style="color: #0600FF;">in</span> context.<span style="color: #0000FF;">Customers</span> 
                         <span style="color: #0600FF;">orderby</span> cus.<span style="color: #0000FF;">Id</span> 
                         <span style="color: #0600FF;">select</span> cus<span style="color: #000000;">&#41;</span> 
                         .<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span>count<span style="color: #000000;">&#41;</span> 
                         .<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; 
&nbsp;
        <span style="color: #0600FF;">return</span> customers; 
    <span style="color: #000000;">&#125;</span> 
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Calling that service using a DataContext might looking something like this…</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> context = $create<span style="color: #66cc66;">&#40;</span>Sys.<span style="color: #006600;">Data</span>.<span style="color: #006600;">DataContext</span>, <span style="color: #66cc66;">&#123;</span> serviceUri: <span style="color: #3366CC;">&quot;Customers.svc&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; 
context.<span style="color: #006600;">fetchData</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;GetCustomers&quot;</span>, <span style="color: #009900; font-style: italic;">// Operation name </span>
                  <span style="color: #66cc66;">&#123;</span> count: <span style="color: #CC0000;">10</span> <span style="color: #66cc66;">&#125;</span>, <span style="color: #009900; font-style: italic;">// Operation parameters </span>
                  Sys.<span style="color: #006600;">Data</span>.<span style="color: #006600;">MergeOption</span>.<span style="color: #006600;">overwriteChanges</span>, <span style="color: #009900; font-style: italic;">// Merge-option (optional) </span>
                  <span style="color: #3366CC;">&quot;POST&quot;</span>, <span style="color: #009900; font-style: italic;">// HTTP verb (optional) </span>
                  <span style="color: #003366; font-weight: bold;">function</span> customerFetchSuccess<span style="color: #66cc66;">&#40;</span>customers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
                      <span style="color: #009900; font-style: italic;">// Do something with customers... </span>
                  <span style="color: #66cc66;">&#125;</span>, 
                  <span style="color: #003366; font-weight: bold;">function</span> customerFetchFailure<span style="color: #66cc66;">&#40;</span>error<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
                      <span style="color: #009900; font-style: italic;">// Handle the error somehow... </span>
                  <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>As you can see, there are all kinds of options that can be passed to the fetchData method, many of which are optional. The above scenario makes use of an ASMX service, but you could just as easily be targeting an <a href="http://www.techbubbles.com/aspnet/ajax-enabled-wcf-service/">AJAX-enabled WCF service</a>, which both have pretty similar semantics in this scenario.</p>
<p>What if you’re using ADO.NET Data Services though? In theory, since the DataContext class can make requests against any service that responds with JSON data (which Data Services can do), it should be able to consume a Data Service with a few modifications to the above code sample…</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> context = $create<span style="color: #66cc66;">&#40;</span>Sys.<span style="color: #006600;">Data</span>.<span style="color: #006600;">DataContext</span>, <span style="color: #66cc66;">&#123;</span> serviceUri: <span style="color: #3366CC;">&quot;Customers.svc&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; 
context.<span style="color: #006600;">fetchData</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Customers&quot;</span>, 
                 <span style="color: #66cc66;">&#123;</span> $top: <span style="color: #CC0000;">10</span> <span style="color: #66cc66;">&#125;</span>, 
                  <span style="color: #003366; font-weight: bold;">null</span>, <span style="color: #009900; font-style: italic;">// Defaults to overwrite changes </span>
                  <span style="color: #3366CC;">&quot;GET&quot;</span>, 
                  <span style="color: #003366; font-weight: bold;">function</span> customerFetchSuccess<span style="color: #66cc66;">&#40;</span>customers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
                      <span style="color: #009900; font-style: italic;">// Do something with customers... </span>
                  <span style="color: #66cc66;">&#125;</span>, 
                  <span style="color: #003366; font-weight: bold;">function</span> customerFetchFailure<span style="color: #66cc66;">&#40;</span>error<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
                      <span style="color: #009900; font-style: italic;">// Handle the error somehow... </span>
                  <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Obviously the serviceUri property now points at a Data Service instead of an ASMX/WCF service, but the real points of interest lie in the operation/parameters. While ASMX and WCF are traditionally operation-centric services (see note below), ADO.NET Data Services is resource-centric, and doesn’t have methods that we can target (unless of course you’re using <a href="http://msdn.microsoft.com/en-us/library/cc668788.aspx">service operations</a>). Hence, to consume the Data Service from our DataContext, we set the operation method to the name of the resource set (i.e. Customers) that we want to retrieve. Operation parameters work the same as with ASMX/WCF, but since Data Services has its own predefined set of query options, we simply need to comply with them (i.e. using $top). Finally, Data Services uses the GET HTTP verb for all data read requests (as opposed to traditional RPC style services that use POST for everything), so we need to set that.</p>
<p class="note">By default, WCF leans towards the edge of RPC-style/SOAP services, but is capable of providing REST-style services that are resource-oriented. An ADO.NET Data Service is itself a WCF service that exposes a RESTful interface and makes data-centric services really easy to implement and consume.</p>
<p>If you ran the above sample, it would work, but not exactly as you might like. The customers data that is passed into the success callback would be a string of XML, representing the <a href="http://tools.ietf.org/html/rfc5023">AtomPub</a> feed sent back from the Data Service. The reason for this is because ADO.NET Data Services will by default serve its data as AtomPub. If a request comes in that includes the Accept HTTP header set to “application/json” then the Data Service will return JSON data. Unfortunately the DataContext class doesn’t set the Accept header when making service calls, which makes consuming a Data Services from it pretty sub-optimal.</p>
<p>You might be thinking to yourself: “Hey that’s cool, I’ll just grab the request that is associated with the DataContext and add the Accept header myself!”. Unfortunately that isn’t possible since the DataContext doesn’t expose its underlying request. If you’re a seriously hardcore ASP.NET AJAX dev, you might be thinking: “Alright fine, I’ll just use the WebRequestManager to tap into the request before it’s sent”. That is totally possible, and would look something like this…</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">Sys.<span style="color: #006600;">Net</span>.<span style="color: #006600;">WebRequestManager</span>.<span style="color: #006600;">add_invokingRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>sender, args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> request = args.<span style="color: #006600;">get_webRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
    request.<span style="color: #006600;">get_headers</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">&quot;Accept&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #3366CC;">&quot;application/json&quot;</span>; 
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Unfortunately that solution is ridiculously unintuitive and is now intercepting all requests just to accommodate a single case. Unless you have a really good reason you should almost never do this. How then can we use a DataContext to easily consume a Data Service? Well, you can’t use the DataContext per se, but you can use a subclass of it, like so…</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> context = $create<span style="color: #66cc66;">&#40;</span>Sys.<span style="color: #006600;">Data</span>.<span style="color: #006600;">AdoNetDataContext</span>, <span style="color: #66cc66;">&#123;</span> serviceUri: <span style="color: #3366CC;">&quot;Customers.svc&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; 
context.<span style="color: #006600;">fetchData</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Customers&quot;</span>, 
                 <span style="color: #66cc66;">&#123;</span> $top: <span style="color: #CC0000;">10</span> <span style="color: #66cc66;">&#125;</span>, 
                  <span style="color: #003366; font-weight: bold;">null</span>, <span style="color: #009900; font-style: italic;">// Defaults to overwrite changes </span>
                  <span style="color: #003366; font-weight: bold;">null</span>, <span style="color: #009900; font-style: italic;">// Defaults to GET </span>
                  <span style="color: #003366; font-weight: bold;">function</span> customerFetchSuccess<span style="color: #66cc66;">&#40;</span>customers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
                      <span style="color: #009900; font-style: italic;">// Do something with customers... </span>
                  <span style="color: #66cc66;">&#125;</span>, 
                  <span style="color: #003366; font-weight: bold;">function</span> customerFetchFailure<span style="color: #66cc66;">&#40;</span>error<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
                      <span style="color: #009900; font-style: italic;">// Handle the error somehow... </span>
                  <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Notice that we’re now using an AdoNetDataContext instead of the vanilla DataContext. This immediately does two interesting things for us: defaults the HTTP verb to GET (as opposed to POST), and sets the Accept header to “application/json” for us (so the Data Service will return JSON data instead of XML). Now if we ran the above code, we would successfully get back the top ten customers as JSON objects.</p>
<p>The DataContext class should be viewed as two things:</p>
<ol>
<li>An easy solution for consuming RPC-style services (i.e. ASMX or WCF)</li>
<li>A great foundation for other higher-level service clients</li>
</ol>
<p>If your server-side resource is an ASMX or WCF service then the basic DataContext is exactly what you want and will work seamlessly. If however you’re using an ADO.NET Data Service, then the DataContext lacks the semantics you need to work successfully, and you should use the AdoNetDataContext instead. If you want to consume some other service type that requires some special attention, you could also choose to subclass DataContext and add on the extra behavior you need (it honestly is pretty simple to do).</p>
<p>Hopefully some of you are reading this and thinking that I’m absolutely crazy to think you would use a DataContext just to retrieve data. The above code sample for consuming an ASMX service could be achieved using a class that has been in ASP.NET AJAX since its conception (WebServiceProxy), and doesn’t require us to create an instance (um, because it’s “static”)…</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">Sys.<span style="color: #006600;">Net</span>.<span style="color: #006600;">WebServiceProxy</span>.<span style="color: #006600;">invoke</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Customers.asmx&quot;</span>, <span style="color: #009900; font-style: italic;">// Service URI </span>
                               <span style="color: #3366CC;">&quot;GetCustomers&quot;</span>, <span style="color: #009900; font-style: italic;">// Operation name </span>
                               <span style="color: #003366; font-weight: bold;">false</span>, <span style="color: #009900; font-style: italic;">// Use GET </span>
                               <span style="color: #66cc66;">&#123;</span> count: <span style="color: #CC0000;">10</span> <span style="color: #66cc66;">&#125;</span>, <span style="color: #009900; font-style: italic;">// Parameters </span>
                               <span style="color: #003366; font-weight: bold;">function</span> customerFetchSuccess<span style="color: #66cc66;">&#40;</span>customers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
                                  <span style="color: #009900; font-style: italic;">// Do something with customers... </span>
                               <span style="color: #66cc66;">&#125;</span>, 
                               <span style="color: #003366; font-weight: bold;">function</span> customerFetchFailure<span style="color: #66cc66;">&#40;</span>error<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
                                  <span style="color: #009900; font-style: italic;">// Handle the error somehow... </span>
                               <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Notice that this code is nearly identical to what we wrote when using the DataContext, but we’ve removed the need to $create anything. There also happens to be an AdoNetServiceProxy class as well that enables you to make service calls to a Data Service, similarly to how we achieved it above. So the question arises: why on earth would you ever use a DataContext vs. just using its respective service proxy? There are two reasons:</p>
<ol>
<li>You need an abstraction over your underlying request/proxy type</li>
<li>You need unit of work behavior, complete with automatic change tracking</li>
</ol>
<p>The first part is important because the WebServiceProxy and AdoNetServiceProxy are drastically different and don’t share a common base class (er prototype). This becomes problematic when you have other classes that need to consume data, but don’t want to be tied to a specific service type. A great example of this is the new DataView class, which allows you to create dynamically-templated UI. The DataView has a property called “dataProvider” that accepts a DataContext, and uses that context to handle populating its templates with the data it needs. We can now hand the DataView any DataContext subtype we want and it will be able to consume the data without a care in the world&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> dataContext = $create<span style="color: #66cc66;">&#40;</span>Sys.<span style="color: #006600;">Data</span>.<span style="color: #006600;">AdoNetDataContext</span>, <span style="color: #66cc66;">&#123;</span> serviceUri: <span style="color: #3366CC;">&quot;Customers.svc&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #003366; font-weight: bold;">var</span> customersTemplate = $create<span style="color: #66cc66;">&#40;</span>Sys.<span style="color: #006600;">UI</span>.<span style="color: #006600;">DataView</span>, <span style="color: #66cc66;">&#123;</span> 
                            autoFetch: <span style="color: #003366; font-weight: bold;">true</span>, 
                            dataProvider: dataContext, 
                            fetchOperation: <span style="color: #3366CC;">&quot;Customers&quot;</span>, 
                            fetchParameters: <span style="color: #66cc66;">&#123;</span> $top: <span style="color: #CC0000;">20</span> <span style="color: #66cc66;">&#125;</span> 
                        <span style="color: #66cc66;">&#125;</span>, 
                        <span style="color: #003366; font-weight: bold;">null</span>, 
                        <span style="color: #003366; font-weight: bold;">null</span>, 
                        $get<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;customers-template&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>I&#8217;ll save the gritty details of the DataView for another post. One point of interest though is that because we&#8217;re no longer manually calling DataContext.fetchData (because the DataView is doing that for us), the operation and parameters properties are now set on the DataView instead of the DataContext. This scenario is probably the most common usage of the DataContext, and illustrates how simple using a DataContext can be.</p>
<p>The second point made above was slipped covertly under the radar, but really should have been highlighted as being the <strong>major reason</strong> of why you would ever use a DataContext in the first place. The abstraction is nice but isn’t nearly compelling enough to warrant introducing an entirely new concept into your life. The DataContext is valuable because it provides you a unit of work experience. Up to this point I’ve purposely shown it being used for read-only scenarios, but it is so much more.</p>
<p>If you are looking to simply retrieve data from a service, you should use one of the proxy types (i.e. WebServiceProxy or AdoNetServiceProxy). In fact I would go so far as to say that if you find yourself manually calling the DataContext’s fetchData method, you are probably doing something wrong. If however you need to change the retrieved data, and then persist those changes back to the server, a DataContext is your best friend.</p>
<p>What if I told you that the code samples above that used a DataContext with a DataView will automatically provide change tracking and can save any changes back to the server as easy as a single line of code…</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">dataContext.<span style="color: #006600;">saveChanges</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>What subtleties exist with change tracking/persistence when working with DataContexts of different types? How exactly could such rich functionality be achieved with a single line of code? I&#8217;ll cover that aspect of the DataContext in another post…</p>
]]></content:encoded>
			<wfw:commentRss>http://lostintangent.com/2009/06/19/gaining-some-context-into-aspnet-ajax-4s-datacontext/feed/</wfw:commentRss>
		<feedburner:origLink>http://lostintangent.com/2009/06/19/gaining-some-context-into-aspnet-ajax-4s-datacontext/</feedburner:origLink></item>
		<item>
		<title>Announcing .NET Framework 4.0 Sentient DSLs</title>
		<link>http://feedproxy.google.com/~r/LostInTangent/~3/79mWF40XkW0/</link>
		<comments>http://lostintangent.com/2009/04/01/announcing-net-framework-40-sentient-dsls/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 07:50:18 +0000</pubDate>
		<dc:creator>Jonathan Carter</dc:creator>
		
		<category><![CDATA[.NET 4.0]]></category>

		<guid isPermaLink="false">http://lostintangent.com/2009/04/01/announcing-net-framework-40-sentient-dsls/</guid>
		<description><![CDATA[As of today, we&#8217;re officially unveiling a brand new feature coming with Visual Studio 2010 and the .NET Framework 4.0: sentient DSLs. If you&#8217;ve become intrigued with all the hype around leveraging domain-specific languages, but you&#8217;re concerned about the added complexity that comes with having to learn a bunch of different grammars, then sentient DSLs [...]]]></description>
			<content:encoded><![CDATA[<p>As of today, we&#8217;re officially unveiling a brand new feature coming with Visual Studio 2010 and the .NET Framework 4.0: sentient DSLs. If you&#8217;ve become intrigued with all the hype around leveraging domain-specific languages, but you&#8217;re concerned about the added complexity that comes with having to learn a bunch of different grammars, then sentient DSLs are for you. Write the code that you think makes sense, and allow the application to create itself. Coding should be fun, and most importantly, it should be easy.</p>
<p>Check out this new feature in action over at Channel 9: <a title="http://channel9.msdn.com/shows/10-4/10-4-Episode-14-Sentient-DSLs/" href="http://channel9.msdn.com/shows/10-4/10-4-Episode-14-Sentient-DSLs/">http://channel9.msdn.com/shows/10-4/10-4-Episode-14-Sentient-DSLs/</a>. As always, I&#8217;d love to get your feedback on how useful you think this could be to you. </p>
]]></content:encoded>
			<wfw:commentRss>http://lostintangent.com/2009/04/01/announcing-net-framework-40-sentient-dsls/feed/</wfw:commentRss>
		<feedburner:origLink>http://lostintangent.com/2009/04/01/announcing-net-framework-40-sentient-dsls/</feedburner:origLink></item>
		<item>
		<title>String Input Handling + Quantum Mechanics!</title>
		<link>http://feedproxy.google.com/~r/LostInTangent/~3/jXmuo_wI8Q4/</link>
		<comments>http://lostintangent.com/2009/04/01/string-input-handling-quantum-mechanics/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 07:10:58 +0000</pubDate>
		<dc:creator>Jonathan Carter</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Quantum Mechanics]]></category>

		<guid isPermaLink="false">http://lostintangent.com/2009/04/01/string-input-handling-quantum-mechanics/</guid>
		<description><![CDATA[One of my biggest complaints with the .NET Framework is its horrible support (or lack thereof) for handling string input. If you have an application that allows a user to enter arbitrary content, there is no single way to &#8220;project&#8221; that content into its appropriate data type, without having to write a ton of boilerplate [...]]]></description>
			<content:encoded><![CDATA[<p>One of my biggest complaints with the .NET Framework is its horrible support (or lack thereof) for handling string input. If you have an application that allows a user to enter arbitrary content, there is no single way to &#8220;project&#8221; that content into its appropriate data type, without having to write a ton of boilerplate code.</p>
<p>At first glance, you might think this is an easy problem to solve, and in some instances you&#8217;d be right, but the devil is most certainly in the details (as in figuring out quantum physics!). If you&#8217;re looking to be able to handle an infinite number of different data types, and generically interpret any possible form of input, then you&#8217;ve got quite a serious problem on your hands.</p>
<p>Thankfully this problem ends today. In addition to developing the recently released ASP.NET MVC framework, <a href="http://www.haacked.com" target="_blank">Phil Haack</a> and Eilon Lipton have also been hard at work addressing the global issue of string input handling and quantum entanglement. This afternoon, the first public CTP of the new &#8220;String Input Handling Framework&#8221; (name pending) has been released. As much as I&#8217;d love to go on about how to use it, and how integral it will become in all future .NET development, Eilon has already put together a great <a href="http://weblogs.asp.net/leftslipper/archive/2009/04/01/the-string-or-the-cat-a-new-net-framework-library.aspx" target="_blank">blog post</a> on this subject. </p>
<p>All I can say is: with innovative solutions like this under Microsoft&#8217;s belt, there is no question just how superior the .NET Framework is to everything else on the market. Move over Ruby, PHP, Python, Java, and everyone else (you know who you are), the .NET train is coming through, and we&#8217;ll be accepting any input you&#8217;ve got to throw at us!</p>
]]></content:encoded>
			<wfw:commentRss>http://lostintangent.com/2009/04/01/string-input-handling-quantum-mechanics/feed/</wfw:commentRss>
		<feedburner:origLink>http://lostintangent.com/2009/04/01/string-input-handling-quantum-mechanics/</feedburner:origLink></item>
		<item>
		<title>Dynamic Data: Putting A New Dress On An Old Model</title>
		<link>http://feedproxy.google.com/~r/LostInTangent/~3/W2SkKWMhdLI/</link>
		<comments>http://lostintangent.com/2009/02/12/dynamic-data-putting-a-new-dress-on-an-old-model/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 09:47:52 +0000</pubDate>
		<dc:creator>Jonathan Carter</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[ASP.NET Dynamic Data]]></category>

		<guid isPermaLink="false">http://lostintangent.com/2009/02/12/dynamic-data-putting-a-new-dress-on-an-old-model/</guid>
		<description><![CDATA[Now that we&#8217;ve spent the last five articles spelunking into the glorious underbelly of the Dynamic Data runtime, and seeing what it offers, we need to begin investigating how to allow a MetaModel to &#8220;light up&#8221; our UI. Everything that we&#8217;ve seen up to this point is completely agnostic to any specific presentation framework. Dynamic [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we&#8217;ve spent the last five articles spelunking into the glorious underbelly of the Dynamic Data runtime, and seeing what it offers, we need to begin investigating how to allow a MetaModel to &#8220;light up&#8221; our UI. Everything that we&#8217;ve seen up to this point is completely agnostic to any specific presentation framework. Dynamic Data (as a runtime) simply provides the infrastructure for translating a data model, complete with metadata and validation, into a higher level model (MetaModel) that has the potential to be interpreted by an application.</p>
<p>Within the .NET 3.5 SP1 release, the initial consumer of Dynamic Data (and the newly introduced data annotations) was ASP.NET WebForms. This means that if you&#8217;re developing a data-driven web application, and your weapon of choice is WebForms, then you just happen to be in luck. Work is already underway to introduce some of Dynamic Data&#8217;s behavior into ASP.NET MVC that is appropriate to its style of development, and you can expect to see additional UI frameworks taking advantage of its semantics and data annotations as well in the near future.</p>
<p>So, the question becomes: now that I&#8217;ve got my data model, and I registered it with a MetaModel, how can I begin taking advantage of it within a WebForms application? Since it is the MetaModel that contains all of the metadata and validation, as well as the shape of our underlying data model, reason would lead us to believe that we&#8217;d need some type of server-control that was aware of a MetaModel and is ready to allow it to affect its behavior.</p>
<p>If you were to begin developing a data-centric WebForms application today, what are your options as far as server controls go? There&#8217;s actually a pretty rich arsenal of controls that can get you up and running pretty quickly, without having to sacrifice functionality&#8230;</p>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="131"><u>Data Controls:</u>
<ol>
<li>GridView
<li>ListView
<li>FormView
<li>DetailsView
<li>DataPager
<li>Repeater</li>
</ol>
</td>
<td width="133"><u>Data Sources:</u>
<ol>
<li>ObjectDataSource
<li>LinqDataSource
<li>EntityDataSource
<li>SqlDataSource
<li>AccessDataSource
<li>XmlDataSource</li>
</ol>
</td>
</tr>
</tbody>
</table>
<p>All of those controls are awesome in their own way but they don&#8217;t exactly help us in our current endeavor. Not a single one of them has a clue what a MetaModel is or is really conscious of what being &#8220;data-driven&#8221; means. They support the notion of metadata and validation, but we have to explicitly define that information, instead of them deriving it from our data model.</p>
<p>While these controls lack some of the functionality we require we certainly don&#8217;t want to have to introduce a whole new suite of controls that we have to learn just to enable data-centric applications. What would be ideal is if we could extend or enhance the functionality that these controls already have to make them more fully-featured and MetaModel-aware. Luckily this is exactly what Dynamic Data provides, in the form of the following controls:</p>
<ol>
<li>DynamicField</li>
<li>DynamicControl</li>
<li>DynamicControlParameter</li>
<li>DynamicQueryStringParameter</li>
<li>DynamicDataManager</li>
<li>DynamicValidator</li>
<li>FieldTemplateUserControl</li>
</ol>
<p>While these new controls provide a lot of the functionality we&#8217;ll need, there is still one piece left to complete our little journey towards data-driven utopia: field-level templates. Since our focus is on the data in our application and the metadata that is associated with that data, a lot of times we might want to allow the presentation framework to determine how to render portions of our data automatically based on the structure of the data model, or better yet, the MetaModel.</p>
<p>These field-level templates could define the UI desired to display data of different types. This would allow us to determine how we wanted numbers, string, dates, etc. to look, in a single location, and have those templates leveraged throughout our entire application. Regardless whether we were creating tabular or form-based data entry forms, we could take advantage of the same templates, providing a consistent look and feel that is completely driven from our data model.</p>
<p>The FieldTemplateUserControl is what makes this functionality possible, and will be the focus of the next article. We&#8217;ll cover how it can be used to define custom field templates, as well as what field templates are provided out-of-the-box by Dynamic Data. Finally, we&#8217;ll cover how to wire-up your defined field templates to actually be used with your created MetaModel, effectively completing the field-level templating story (which is one of two possible scenarios provided by Dynamic Data).</p>
]]></content:encoded>
			<wfw:commentRss>http://lostintangent.com/2009/02/12/dynamic-data-putting-a-new-dress-on-an-old-model/feed/</wfw:commentRss>
		<feedburner:origLink>http://lostintangent.com/2009/02/12/dynamic-data-putting-a-new-dress-on-an-old-model/</feedburner:origLink></item>
		<item>
		<title>Dynamic Data: Associated Types And The Models They Love</title>
		<link>http://feedproxy.google.com/~r/LostInTangent/~3/EaUm6XEFTUY/</link>
		<comments>http://lostintangent.com/2008/12/18/dynamic-data-associated-types-and-the-models-they-love/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 19:55:08 +0000</pubDate>
		<dc:creator>Jonathan Carter</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[ASP.NET Dynamic Data]]></category>

		<guid isPermaLink="false">http://lostintangent.com/2008/12/18/dynamic-data-associated-types-and-the-models-they-love/</guid>
		<description><![CDATA[In the last article we introduced the ContextConfiguration class and its MetadataProviderFactory property. We also saw that if a provider factory isn&#8217;t explicitly set then an instance of the AssociatedMetadataTypeTypeDescriptorProvider class will be used. What we didn&#8217;t discuss is what an &#8220;associated metadata type&#8221; is, and why we need a type descriptor for working with [...]]]></description>
			<content:encoded><![CDATA[<p>In the last <a target="_blank" href="http://lostintangent.com/2008/12/16/dynamic-data-annotating-your-data-driven-world/">article</a> we introduced the ContextConfiguration class and its MetadataProviderFactory property. We also saw that if a provider factory isn&#8217;t explicitly set then an instance of the AssociatedMetadataTypeTypeDescriptorProvider class will be used. What we didn&#8217;t discuss is what an &#8220;associated metadata type&#8221; is, and why we need a type descriptor for working with it. Before going into those exact questions, we need to take a step back and examine the way some models are constructed.</p>
<p>We can all agree that the term &#8220;model&#8221; is pretty overloaded and has many different meanings/interpretations. You have domain models, presentation models, view models, super models, model trains, and so forth and so on. This series isn&#8217;t tied to any specific model implementation methodology per se, but rather on the intent of creating web applications that are focused around data in a <a target="_blank" href="http://en.wikipedia.org/wiki/Rapid_application_development">RAD</a> fashion. Because there are numerous ways of constructing an object model, we&#8217;d need a mechanism for discovering metadata from models that assume different shapes.</p>
<p>If you hand-craft your object model and perform the database-mapping yourself you could easily place data annotations on your entity classes and go on with it (as you might do if using NHibernate). While that is a totally valid scenario, it isn&#8217;t necessarily the right one for a data-driven application where you most likely already have a database in place and are looking to reverse engineer a model from it&#8217;s schema. For cases like that you might use something like LINQ To SQL or the Entity Framework, both of which provide designers/wizards for generating a mapped model to an existing database.</p>
<p>If I create a new Entity Data Model and point its wizard at an AdventureWorks database, I&#8217;ll end up with a whole bunch of generated entity classes that look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>global::<span style="color: #000000;">System</span>.<span style="color: #0000FF;">Data</span>.<span style="color: #0000FF;">Objects</span>.<span style="color: #0000FF;">DataClasses</span>.<span style="color: #0000FF;">EdmEntityTypeAttribute</span><span style="color: #000000;">&#40;</span>NamespaceName=<span style="color: #808080;">&quot;AdventureWorks&quot;</span>, Name=<span style="color: #808080;">&quot;Address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #000000;">&#91;</span>global::<span style="color: #000000;">System</span>.<span style="color: #0000FF;">Runtime</span>.<span style="color: #0000FF;">Serialization</span>.<span style="color: #0000FF;">DataContractAttribute</span><span style="color: #000000;">&#40;</span>IsReference=<span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #000000;">&#91;</span>global::<span style="color: #000000;">System</span>.<span style="color: #0000FF;">Serializable</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Address : global::<span style="color: #000000;">System</span>.<span style="color: #0000FF;">Data</span>.<span style="color: #0000FF;">Objects</span>.<span style="color: #0000FF;">DataClasses</span>.<span style="color: #0000FF;">EntityObject</span> 
<span style="color: #000000;">&#123;</span> 
    ... 
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Because that class lives within the confines of my EDMX, which is template-generated code, I don&#8217;t exactly want to go mucking around in there. If I later make a change with the designer, it would wipe out any changes I manually made to the entity classes, which would lead to a lot of pain and frustration. Luckily though, the entities are generated as partial classes which means we can make modifications to them from outside of the EDMX&#8217;s code files. This gives us the ability to leverage the designer and still have some level of flexibility over the codebase moving forward.</p>
<p>I can easily create a new partial class for my Address entity and begin placing some data annotations on it. Let&#8217;s say that I want to specify that the display name for it should be &#8220;Customer Address&#8221; and that the column (property) that should be used to represent it&#8217;s display value should be &#8220;Country&#8221;. I could easily do this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>DisplayColumn<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Country&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #000000;">&#91;</span>DisplayName<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Customer Address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Address 
<span style="color: #000000;">&#123;</span> 
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Because we&#8217;re using partial classes (which are combined at compile time), we&#8217;re still annotating the entity class itself, just using a different approach. We could easily retreive this metadata using standard reflection without requiring the need for a custom type descriptor. But what if I wanted to add metadata to one of the properties of my Address entity now? Let&#8217;s say that I wanted to specify that the Address2 property is required (even though it isn&#8217;t at the database level), PostalCode should be constrained by a regular expression, and that CountryRegion&#8217;s display name should just be &#8220;Country&#8221;. Because I don&#8217;t want to mess with the generated code of the EDMX I can&#8217;t go place data annotations directly on those properties. I definitely can&#8217;t do the following, since that would be declaring the properties twice on the same class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>DisplayColumn<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Country&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #000000;">&#91;</span>DisplayName<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Customer Address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Address 
<span style="color: #000000;">&#123;</span> 
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Address2 <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span> 
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> CountryRegion <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span> 
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> PostalCode <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span> 
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It might seem like we&#8217;re out of luck at this point and have to settle with changing the generated EDMX code. What if though, we could create a class whose sole purpose was to contain properties that we could annotate with metadata for another entity and then associate the two together? That would get us the functionality we need and work around the &#8220;issue&#8221; we&#8217;re dealing with between partial classes and generated code. Let&#8217;s say we created the following AddressMetadata class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">class</span> AddressMetaData 
<span style="color: #000000;">&#123;</span> 
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Address2 <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span> 
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> CountryRegion <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span> 
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> PostalCode <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span> 
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We don&#8217;t get any compiler errors (obviously) so we&#8217;re moving in the right direction. How can we then associate this new type with our original Address entity class?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>MetadataType<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>AddressMetaData<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Address 
<span style="color: #000000;">&#123;</span></pre></div></div>

<p>Now we can place any entity-level metadata on our Address partial class, and any property-level metadata on our new AddressMetaData class, because the two are associated. You can place/name your metadata type anywhere you want, but I prefer to make it a private/embedded class of the partial entity class itself. I can now add the metadata I want to my properties.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>DisplayColumn<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Country&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #000000;">&#91;</span>DisplayName<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Customer Address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #000000;">&#91;</span>MetadataType<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>AddressMetaData<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Address 
<span style="color: #000000;">&#123;</span> 
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">class</span> AddressMetaData 
    <span style="color: #000000;">&#123;</span> 
        <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#93;</span> 
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> Address2 <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span> 
&nbsp;
        <span style="color: #000000;">&#91;</span>DisplayName<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Country&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> CountryRegion <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span> 
&nbsp;
        <span style="color: #000000;">&#91;</span>RegularExpression<span style="color: #000000;">&#40;</span><span style="color: #808080;">@"\d{5}(-\d{4})?&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> PostalCode <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span> 
    <span style="color: #000000;">&#125;</span> 
<span style="color: #000000;">&#125;</span></pre></div></div>

<p class="note">Notice that I changed the type of the three properties to &#8220;object&#8221; because the property type no longer matters. Within the metadata type, we only need to be concerned with naming the property correctly.</p>
<p>We established that standard reflection could handle the discovery of the entity-level metadata, but now that we&#8217;ve created this &#8220;metadata type&#8221; which has its own set of properties and metadata, reflection would have no clue of its semantic association with the Address entity. This is where the AssociatedMetadataTypeTypeDescriptionProvider comes in.</p>
<p>The AssociatedMetadataTypeTypeDescriptionProvider class is capable of looking at a type and finding any of its class-level attributes as well as looking to see if that type has an associated metadata type (via the MetadataType attribute) and if it does, grabbing any metadata from the metadata type&#8217;s properties. This is a fairly roundabout approach, but it gets us around in the meantime. In the future you might see the notion of &#8220;partial properties&#8221; that would remove the need for the metadata type altogether.</p>
<p>So now it&#8217;s clear why the AssociatedMetadataTypeTypeDescriptionProvider exists and is the default behavior. If our model is able to apply all metadata directly to the entity classes, then it will work great. If our model requires the use of a &#8220;associated metadata type&#8221;, then it will also pick up on that as well. If we had an alternate style of model, that required a different type of metadata discovery, then we could implement a TypeDescriptionProvider and set it via the ContextConfiguration&#8217;s MetadataProviderFactory property. Dynamic Data uses the specified provider throughout its entire API, making it extremely easy to swap our your model metadata discovery logic.</p>
<p>Now that we&#8217;ve got our model created and we&#8217;ve begun adding metadata to it, how can we start seeing the fruits of our labor take effect within our web application&#8217;s UI? In the next article I&#8217;ll begin showing how Dynamic Data provides a set of extensions to ASP.NET that can take advantage of your annotated models and the MetaModels derived from them.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostintangent.com/2008/12/18/dynamic-data-associated-types-and-the-models-they-love/feed/</wfw:commentRss>
		<feedburner:origLink>http://lostintangent.com/2008/12/18/dynamic-data-associated-types-and-the-models-they-love/</feedburner:origLink></item>
		<item>
		<title>Dynamic Data: Annotating Your Data-Driven World</title>
		<link>http://feedproxy.google.com/~r/LostInTangent/~3/nVQ_AMi5y9k/</link>
		<comments>http://lostintangent.com/2008/12/16/dynamic-data-annotating-your-data-driven-world/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 04:01:43 +0000</pubDate>
		<dc:creator>Jonathan Carter</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[ASP.NET Dynamic Data]]></category>

		<guid isPermaLink="false">http://lostintangent.com/2008/12/16/dynamic-data-annotating-your-data-driven-world/</guid>
		<description><![CDATA[In the last article we discussed how a MetaModel could derive some of its metadata content from its respective provider&#8217;s underlying data model (i.e. an EDM), but also mentioned that there is a lot of valuable information that can&#8217;t necessarily be deduced from every data model type. Because a data model can come in any [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://lostintangent.com/2008/11/21/dynamic-data-the-little-metamodel-that-could/">last article</a> we discussed how a MetaModel could derive some of its metadata content from its respective provider&#8217;s underlying data model (i.e. an EDM), but also mentioned that there is a lot of valuable information that can&#8217;t necessarily be deduced from every data model type. Because a data model can come in any shape or form (i.e. EF, L2S, NHibernate, etc) it becomes tricky to try to interpret metadata in any sort of consistent fashion. Providers make an attempt at doing this, but really, their job isn&#8217;t to curry metadata back and forth between the MetaModel and underlying data model, but rather to shape the data model in such a way that the MetaModel can understand it.</p>
<p>Without a standard approach for adding metadata to data models (of any kind), data-driven UI frameworks would have a hard time providing the rich functionality we are demanding from them. By introducing the MetaModel, we&#8217;ve given the UI layer a robust view of the underlying data model such that it can provide the behavior we expect, so that problem is already solved. Now we just need a mechanism for annotating our model with metadata in such a way that our MetaModel can pick it up.</p>
<p>The .NET Framework 3.5 SP1 release contained an assembly named System.ComponentModel.DataAnnotations. Within that assembly is a collection of attributes that are meant to be associated with data models for the purpose of adding additional metadata and validation to them in a way that is agnostic to any specific UI framework. Notice that the namespace they live in isn&#8217;t &#8220;System.Web.*&#8221; or &#8220;System.Windows.*&#8221; but rather &#8220;System.ComponentModel.*&#8221;, which adds to the argument of standard use.</p>
<p>Because these data annotations are simply attributes, we can easily begin attaching them to our entity classes and properties, which adds additional information to our model such as validation constraints (range, regular expression, string length, etc.) and higher level semantics (data type, UI hint, scaffolding, etc.). The question is: how does our MetaModel discover these annotations once we&#8217;ve placed them on our model?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>DisplayColumn<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;FirstName&quot;</span>, <span style="color: #808080;">&quot;LastName&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Customer
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FirstName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>StringLength<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> MiddleName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> LastName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>Range<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">18</span>, <span style="color: #FF0000;">30</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Age <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p class="note">For any <a href="http://en.wikipedia.org/wiki/Domain-driven_design">DDD</a> proponents, I realize the above model class would be considered <a href="http://en.wikipedia.org/wiki/Anemic_Domain_Model">anemic</a>. It&#8217;s important to keep in mind that the methodology being employed in this series is that of data-driven applications not domain-driven.</p>
<p>When you register a context/provider with a MetaModel (as we saw in a previous post), an instance of type ContextConfiguration is created and associated with your model. You can either explicitly create a ContextConfiguration instance and use one of the overloaded versions of the RegisterContext method that accepts one, or you can allow the MetaModel to create one for you. ContextConfiguration is an extremely simple class that contains only two properties: ScaffoldAllTables and MetadataProviderFactory. The ScaffoldAllTables property is only applicable if you&#8217;re leveraging Dynamic Data for full-blown scaffolding, which is a scenario we&#8217;ll touch on later.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #008080; font-style: italic;">// Implict ContextConfiguration...</span>
var model = <span style="color: #008000;">new</span> MetaModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
model.<span style="color: #0000FF;">RegisterContext</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>AdventureWorksContext<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008080; font-style: italic;">// Explicit ContextConfiguration...</span>
var model2 = <span style="color: #008000;">new</span> MetaModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
model2.<span style="color: #0000FF;">RegisterContext</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>AdventureWorksContext<span style="color: #000000;">&#41;</span>,
                      <span style="color: #008000;">new</span> ContextConfiguration <span style="color: #000000;">&#123;</span> ScaffoldAllTables = <span style="color: #0600FF;">false</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p class="note"> The ScaffoldAllTables property defaults to false, so if we aren&#8217;t concerned with Dynamic Data&#8217;s scaffolding behavior (which in this case we aren&#8217;t), we can just omit that property instead of explicitly setting it.</p>
<p>The MetadataProviderFactory property is of type Func&lt;Type, TypeDescriptionProvider&gt;, which might look scarier than it actually is. Func is simply a generic delegate that returns a value and optionally takes up to four parameters (there are five variations of Func). In this case we&#8217;re dealing with a Func that will accept a parameter of type Type and return an object of type TypeDescriptionProvider. When we register a context/provider with a MetaModel, it will enumerate through every entity type in our model, calling the delegate within the ContextConfiguration&#8217;s MetadataProviderFactory property, which will then return an TypeDescriptionProvider.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">model.<span style="color: #0000FF;">RegisterContext</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>AdventureWorksContext<span style="color: #000000;">&#41;</span>,
                       <span style="color: #008000;">new</span> ContextConfiguration
                       <span style="color: #000000;">&#123;</span>
                           MetadataProviderFactory = <span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span> =&gt; <span style="color: #008000;">new</span> CustomTypeProvider<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                       <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p class="information">A TypeDescriptionProvider is (as you can probably guess) responsible for providing a type descriptor (more specifically an object that implements ICustomTypeDescriptor or inherits from CustomTypeDescriptor). An ICustomTypeDescriptor implementation is able to determine information about a type such as its properties, events, attributes, etc. This allows applications to use a type descriptor to retrieve metadata about a type instead of using reflection directly. Why is this useful? Because you could create a custom ICustomTypeDescriptor that represented the shape of a type differently then it is in code.</p>
<p>As was mentioned above, the standard data annotations are provided as attribute classes, so the fact that a type descriptor allows you to retrieve attributes from a type (as well as that type&#8217;s properties), makes it very useful. By being able to create a custom TypeDescriptionProvider we can effectively implement logic that retrieves our model metadata from any source (i.e. XML, database, etc.), but uses the same standard data annotations, giving us both consistency and flexibility.</p>
<p>If you don&#8217;t explicitly set the MetadataProviderFactory property, the Metamodel will internally create an instance of the AssociatedMetadataTypeTypeDescriptionProvider class (that is a hell of a name!). This TypeDescriptionProvider implementation simply retrieves all attributes for a type via reflection. Why is this any better than just using reflection directly? Because it also retrieves any attributes from the requested type&#8217;s associated metadata type as well.</p>
<p>What exactly does &#8220;associated metadata type&#8221; mean? And why is it beneficial that the TypeDescriptionProvider be able to retrieve its attributes? We&#8217;ll discuss these two questions and expand on the usage of data annotations in the next article.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostintangent.com/2008/12/16/dynamic-data-annotating-your-data-driven-world/feed/</wfw:commentRss>
		<feedburner:origLink>http://lostintangent.com/2008/12/16/dynamic-data-annotating-your-data-driven-world/</feedburner:origLink></item>
		<item>
		<title>Top 50 Albums of 2008 (…By "Top" I Mean "My Favorite")</title>
		<link>http://feedproxy.google.com/~r/LostInTangent/~3/qESoaWx57hA/</link>
		<comments>http://lostintangent.com/2008/12/10/top-50-albums-of-2008-by-top-i-mean-my-favorite/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 09:50:13 +0000</pubDate>
		<dc:creator>Jonathan Carter</dc:creator>
		
		<category><![CDATA[Music]]></category>

		<guid isPermaLink="false">http://lostintangent.com/2008/12/10/top-50-albums-of-2008-by-top-i-mean-my-favorite/</guid>
		<description><![CDATA[I wasn&#8217;t going to do this post but I ended up not being able to sleep last night and decided &#8220;what the hell?&#8221;. I purposely did the list in no particular order as I feel that it isn&#8217;t realistic to quantify a specific placement of each album and have it mean anything. What exactly does [...]]]></description>
			<content:encoded><![CDATA[<p>I wasn&#8217;t going to do this post but I ended up not being able to sleep last night and decided &#8220;what the hell?&#8221;. I purposely did the list in no particular order as I feel that it isn&#8217;t realistic to quantify a specific placement of each album and have it mean anything. What exactly does the difference between #18 and #19 really look like? I spent many years writing for magazines and can attest to the fact that these lists are just subjective babble.</p>
<p>The majority of these albums are post-rock, electronic and instrumental artists. There are a few metal, indie, folk and post-hardcore albums as well. I did a TON of traveling this year and this is the collection that kept me sane around the world. If you care for a more &#8220;formal&#8221; list, you can check out my other site, <a href="http://thesilentballet.com/dnn/">The Silent Ballet</a>, in a few weeks.</p>
<ol>
<li>Helios - &#8220;Ceasura&#8221; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendid=69036995" target="_blank">Link</a>)</li>
<li>Lights Out Asia - &#8220;Eyes Like Brontide&#8221; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendID=13935750" target="_blank">Link</a>)</li>
<li>Shearwater - &#8220;Rook&#8221; (<a href="http://www.myspace.com/shearwater" target="_blank">Link</a>)</li>
<li>No.9 - &#8220;<span id="dnn_ctr384_ArticleDetails_lblTitle" class="Head">Usual Revolution And Nine</span>&#8221; (<a href="http://www.myspace.com/lifeno9" target="_blank">Link</a>)</li>
<li>Sumner McKane - &#8220;What A Great Place To Be&#8221; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendid=109980441" target="_blank">Link</a>)</li>
<li>Suffocate For Fuck Sake - &#8220;Blazing Fires And Helicopters On The Front Page Of The Newspaper. There&#8217;s A War Going On And I&#8217;m Marching In Heavy Boots&#8221; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewProfile&amp;friendID=26269330" target="_blank">Link</a>)</li>
<li>Bersarin Quartett - &#8220;Bersarin Quartett&#8221; (<a href="http://www.myspace.com/bersarinquartett" target="_blank">Link</a>)</li>
<li>This Town Needs Guns - &#8220;Animals&#8221; (<a href="http://www.myspace.com/thistownneedsguns" target="_blank">Link</a>)</li>
<li>Jóhann Jóhannsson - &#8220;Fordlandia&#8221; (<a href="http://www.myspace.com/johannjohannsson" target="_blank">Link</a>)</li>
<li>Cult Of Luna - &#8220;Eternal Kingdom&#8221; (<a href="http://www.myspace.com/cultofluna" target="_blank">Link</a>)</li>
<li>This Is Your Captain Speaking - &#8220;Eternal Return&#8221; (<a href="http://www.myspace.com/tiycs" target="_blank">Link</a>)</li>
<li>Up-C Down-C Left-C Right-C ABC + Start - &#8220;Embers&#8221; (<a href="http://www.myspace.com/upcdownc" target="_blank">Link</a>)</li>
<li>Balmorhea - &#8220;River Arms&#8221; (<a href="http://www.myspace.com/balmorhea" target="_blank">Link</a>)</li>
<li>Strangers Die Every Day - &#8220;Aperture For Departure&#8221; (<a href="http://www.myspace.com/strangersdieeveryday" target="_blank">Link</a>)</li>
<li>Takahiro Kido - &#8220;Fleursy Music&#8221; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendid=1000948056" target="_blank">Link</a>)</li>
<li>M83 - &#8220;Saturdays = Youth&#8221; (<a href="http://www.myspace.com/m83" target="_blank">Link</a>)</li>
<li>Mouth Of The Architect - &#8220;Quietly&#8221; (<a href="http://www.myspace.com/mouthofthearchitect" target="_blank">Link</a>)</li>
<li>Lakes Of Russia - &#8220;Stars Decorate The Fire&#8221; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendID=73424327" target="_blank">Link</a>)</li>
<li>Earth - &#8220;The Bees Made Honey In The Lion&#8217;s Skull&#8221; (<a href="http://www.myspace.com/earthofficial" target="_blank">Link</a>)</li>
<li>Fuck Buttons - &#8220;Street Horrrsing&#8221; (<a href="http://www.myspace.com/fuckbuttons" target="_blank">Link</a>)</li>
<li>The Drift - &#8220;Memory Drawings&#8221; (<a href="http://www.myspace.com/trldrift" target="_blank">Link</a>)</li>
<li>Beneva Vs. Clark Nova - &#8220;Sombunall&#8221; (<a href="http://www.myspace.com/benevavsclarknova" target="_blank">Link</a>)</li>
<li>Mooncake - &#8220;Langrange Points&#8221; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendID=82684942" target="_blank">Link</a>)</li>
<li>We&#8217;re From Japan! - &#8220;Now Breath&#8221; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewProfile&amp;friendID=31375751" target="_blank">Link</a>)</li>
<li>The Evpatoria Report - &#8220;Maar&#8221; (<a href="http://www.myspace.com/theevpatoriareport" target="_blank">Link</a>)</li>
<li>Arms &amp; Sleepers - &#8220;Black Paris 86&#8243; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendID=96752936" target="_blank">Link</a>)</li>
<li>d_rradio - &#8220;d_rradio&#8221; (<a href="http://www.myspace.com/deathrowradio" target="_blank">Link</a>)</li>
<li>Gregor Samsa - &#8220;Rest&#8221; (<a href="http://www.myspace.com/gregorsamsamyspace" target="_blank">Link</a>)</li>
<li>Russian Circles - &#8220;Station&#8221; (<a href="http://www.myspace.com/russiancircles" target="_blank">Link</a>)</li>
<li>Ef - &#8220;I Am Responsible&#8221; (<a href="http://www.myspace.com/ef" target="_blank">Link</a>)</li>
<li>Pg.Lost - &#8220;It&#8217;s Not Me, It&#8217;s You!&#8221; (<a href="http://www.myspace.com/pglost" target="_blank">Link</a>)</li>
<li>Neil On Impression - &#8220;<span id="dnn_ctr384_ArticleDetails_lblTitle" class="Head">L&#8217;oceano Delle Onde Che Restano Onde Per Sempre</span>&#8221; (<a href="http://www.myspace.com/neilonimpression" target="_blank">Link</a>)</li>
<li>He Can Jog - &#8220;Middlemarch&#8221; (<a href="http://www.myspace.com/hecanjogmusic" target="_blank">Link</a>)</li>
<li>The Seven Mile Journey - &#8220;The Metamorphosis Project&#8221; (<a href="http://www.myspace.com/thesevenmilejourney" target="_blank">Link</a>)</li>
<li>Goodnight, Sleep Well - &#8220;The Recovery&#8221; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendID=357246537" target="_blank">Link</a>)</li>
<li>Kyte - &#8220;Kyte&#8221; (<a href="http://profile.myspace.com/index.cfm?fuseaction=user.viewProfile&amp;friendID=38745460" target="_blank">Link</a>)</li>
<li>Unwed Sailor - &#8220;Little Wars&#8221; (<a href="http://www.myspace.com/unwedsailor" target="_blank">Link</a>)</li>
<li>Daturah - &#8220;Reverie&#8221; (<a href="http://www.myspace.com/daturah" target="_blank">Link</a>)</li>
<li>Port Blue - &#8220;The Albatross&#8221; (<a href="http://www.myspace.com/portblue" target="_blank">Link</a>)</li>
<li>My Education - &#8220;Bad Vibrations&#8221; (<a href="http://www.myspace.com/myeducation" target="_blank">Link</a>)</li>
<li>Build - &#8220;Build&#8221; (<a href="http://www.myspace.com/buildbuildbuild" target="_blank">Link</a>)</li>
<li>God Is An Astronaut - &#8220;God Is An Astronaut&#8221; (<a href="http://www.myspace.com/godisanastronaut" target="_blank">Link</a>)</li>
<li>MGMT - &#8220;Oracular Spectacular&#8221; (<a href="http://www.myspace.com/mgmt" target="_blank">Link</a>)</li>
<li>Lindstrøm - &#8220;Where You Go I Go Too&#8221; (<a href="http://www.myspace.com/feedelity" target="_blank">Link</a>)</li>
<li>September Malevolence - &#8220;After This Darkness, There&#8217;s A Next&#8221; (<a href="http://www.myspace.com/septembermalevolence" target="_blank">Link</a>)</li>
<li>The Absent Sound - &#8220;Gathering Of The Clan Mothers&#8221; (<a href="http://www.myspace.com/absentsound" target="_blank">Link</a>)</li>
<li>The Calm Blue Sea - &#8220;The Calm Blue Sea&#8221; (<a href="http://myspace.com/thecalmbluesea">Link</a>)</li>
<li>Beast, Please Be Still! - &#8220;Beast, Please Be Still!&#8221; (<a href="http://www.myspace.com/beastpleasebestill" target="_blank">Link</a>)</li>
<li>Robin Guthrie - &#8220;3:19 Bande Originale du Film&#8221; (<a href="http://www.myspace.com/robinguthrie" target="_blank">Link</a>)</li>
<li>Peter Broderick - &#8220;Float&#8221; (<a href="http://www.myspace.com/peterbroderick" target="_blank">Link</a>)</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://lostintangent.com/2008/12/10/top-50-albums-of-2008-by-top-i-mean-my-favorite/feed/</wfw:commentRss>
		<feedburner:origLink>http://lostintangent.com/2008/12/10/top-50-albums-of-2008-by-top-i-mean-my-favorite/</feedburner:origLink></item>
		<item>
		<title>Dynamic Data: The Little MetaModel That Could</title>
		<link>http://feedproxy.google.com/~r/LostInTangent/~3/tkBhz9SqJSw/</link>
		<comments>http://lostintangent.com/2008/11/21/dynamic-data-the-little-metamodel-that-could/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 00:01:28 +0000</pubDate>
		<dc:creator>Jonathan Carter</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[ASP.NET Dynamic Data]]></category>

		<guid isPermaLink="false">http://lostintangent.com/2008/11/21/dynamic-data-the-little-metamodel-that-could/</guid>
		<description><![CDATA[When a MetaModel is initialized from a data source (via its respective provider), what additional information is determined in the process? The MetaModel itself contains a couple of useful instance properties and methods (more of which we&#8217;ll examine later), but for the purpose of this article, we only care about its Tables property, which is [...]]]></description>
			<content:encoded><![CDATA[<p>When a MetaModel is initialized from a data source (via its respective provider), what additional information is determined in the process? The MetaModel itself contains a couple of useful instance properties and methods (more of which we&#8217;ll examine later), but for the purpose of this article, we only care about its Tables property, which is of type ReadOnlyCollection&lt;MetaTable&gt;. As you can imagine, that property returns a list of MetaTable instances that represent every entity in our underlying data source.</p>
<p>The MetaTable class contains a whole slew of useful properties that we otherwise couldn&#8217;t deduce from its respective model class, such as: ForeignKeyColumnNames, DisplayName, PrimaryKeyColumnNames and SortColumn. As you can see, because of the higher-level semantics provided us by the MetaModel, any controls within our UI, that are aware of the MetaModel, can now take advantage of these additional attributes. The MetaTable class also contains a Columns property, which is of type ReadOnlyCollection&lt;MetaColumn&gt;, and contains a list of every column/property in the entity.</p>
<p>The MetaColumn class is where the real meat of the metadata system lies. It contains loads of properties for gathering information about a column/property from its underlying model, such as: DefaultValue, IsGenerated, IsRequired, MaxLength, NullDisplayText and RequiredErrorMessage. In addition, a MetaColumn can be of type MetaChildColumn or MetaForeignKeyColumn if it participates in an association.</p>
<p>Why does any of this matter? Because this rich MetaModel, in conjunction with our actual data model, provides us with enough information about the structure/semantics of the data that we can begin deducing lots of functionality/behavior from it. Better yet, our UI can do that for us. It&#8217;s clear at this point that the only thing we need in order to begin developing smarter data-centric applications are UI frameworks that can understand and react to the contents of our MetaModel.</p>
<p>Before we move on to examining how Dynamic Data provides extensions to ASP.NET that allow our UI to make use of a MetaModel, we need to briefly address where the MetaModel is getting its contents from. How exactly does it know what the DefaultValue for a column is, or the DisplayName for an entity is? It simply asks the provider. If you&#8217;re using an Entity Data Model, its respective provider can scavenge metadata from the EDM; if you&#8217;re using a LINQ To SQL model, its respective provider can examine the MetaModel (yes L2S uses this term as well) for metadata; if you&#8217;re using any other datasource, then you can create a custom provider that can determine that information however it makes sense.</p>
<p>You may be thinking right now that neither an EDM nor a L2S model provide the full extent of metadata that I&#8217;ve highlighted our MetaModel as exposing, and you&#8217;d be exactly right. In addition to the providers looking at their datasource specific mediums for metadata, there is also a set of common data annotations that can be applied to your data model and will be picked up by the MetaModel. These annotations live in the System.ComponentModel.DataAnnotations assembly/namespace.</p>
<p>These annotations have absolutely nothing to do with Dynamic Data, and are meant solely for annotating model classes within data-driven applications. Dynamic Data just happens to be the first UI framework (technically ASP.NET is) that understands them and makes use of them. In the next article I&#8217;ll explain how to make use of these annotations.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostintangent.com/2008/11/21/dynamic-data-the-little-metamodel-that-could/feed/</wfw:commentRss>
		<feedburner:origLink>http://lostintangent.com/2008/11/21/dynamic-data-the-little-metamodel-that-could/</feedburner:origLink></item>
		<item>
		<title>Dynamic Data: Models, MetaModels And Everything In Between</title>
		<link>http://feedproxy.google.com/~r/LostInTangent/~3/eX18lY0tP4o/</link>
		<comments>http://lostintangent.com/2008/11/19/dynamic-data-models-metamodels-and-everything-in-between/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 19:32:52 +0000</pubDate>
		<dc:creator>Jonathan Carter</dc:creator>
		
		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[ASP.NET Dynamic Data]]></category>

		<guid isPermaLink="false">http://lostintangent.com/2008/11/19/dynamic-data-models-metamodels-and-everything-in-between/</guid>
		<description><![CDATA[In the previous article, I mentioned that the existing data controls in WebForms aren&#8217;t very smart as regards to your data, but in all fairness, it isn&#8217;t really their fault. How is a GridView supposed to know that a property is required? How can a FormView know that a specific control should include regular expression [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://lostintangent.com/2008/11/19/dynamic-data-kickin-it-old-school/" target="_blank">previous article</a>, I mentioned that the existing data controls in WebForms aren&#8217;t very smart as regards to your data, but in all fairness, it isn&#8217;t really their fault. How is a GridView supposed to know that a property is required? How can a FormView know that a specific control should include regular expression validation? How can a DetailsView know that certain columns shouldn&#8217;t be displayed? Since those controls are completely agnostic as to the type of data they are bound to, there is no set of common assumptions they can make about their bound data in order to provide a richer set of UI behaviors.</p>
<p>It almost seems like what we need is some sort of meta-model, that provides additional semantics/metadata on top of our data model that our WebForms controls can leverage to make the desired assumptions. That way, we could bind any type of data to the controls, and they wouldn&#8217;t have to rely on any specific model type, but could rather use the meta-model to determine the additional actions it should (or shouldn&#8217;t) take. Because the meta-model would have a common form, the controls would only have to be aware of a single metadata system.</p>
<p>While having a common metadata representation is great, how can a meta-model be created based on every possible type of data? Obviously that wouldn&#8217;t be practical to have a single codebase that determined metadata from every data type. Rather, what we&#8217;d need is the ability to create a provider that was aware of a specific data type and could create a meta-model from it. This way, allowing a new data type to work with our common metadata system would be as easy as creating a new provider.</p>
<p>The solution described thus far is exactly what Dynamic Data provides. A common meta-model system for describing data models with a higher semantic that a UI can leverage to be smarter by default. Out of the box, it includes providers for LINQ-To-SQL and the Entity Framework (which is why it has gotten a reputation for working solely against databases), but nothing stops it from working against any other data source. In fact, prototypes already exist that have Dynamic Data working against an ADO.NET Data Service, as well as a SQL Data Service.</p>
<p>So what does the code look like for creating a meta-model from an existing data model?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Global : HttpApplication
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Application_Start<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var model = <span style="color: #008000;">new</span> MetaModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        model.<span style="color: #0000FF;">RegisterContext</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>AdventureWorksContext<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In that code sample, &#8220;AdventureWorksContext&#8221; is the name of my ObjectContext (a generated class used for working with an Entity Data Model). It could have also been a DataContext (a class used for working with a LINQ To SQL model), and the MetaModel would have been created just fine. This is because the RegisterContext method is aware of L2S and EF models and will created the necessary provider to fill the MetaModel. What if your data source is something other then a L2S or EF model? Then you can just pass an instance of your custom provider to the RegisterContext method.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">var model = <span style="color: #008000;">new</span> MetaModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
model.<span style="color: #0000FF;">RegisterContext</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ArbitraryDataModelProvider<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Your provider would then be responsible for traversing your data source and retrieving the metadata that described it. The first MetaModel that you create is considered the default instance, and is stored in the static MetaModel.Default property. MetaModels can also be retreived by type using the MetaModel.GetModel method (in case you have multiple). Once you&#8217;ve created your MetaModel during your application&#8217;s start event in the Global.asax file, your MetaModel is now globally available throughout your application, just waiting to be leveraged by your UI.</p>
<p>While this all seems dandy, two questions arise: what metadata does the MetaModel expose, and where did it get it from? I&#8217;ll cover those questions in the next article.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostintangent.com/2008/11/19/dynamic-data-models-metamodels-and-everything-in-between/feed/</wfw:commentRss>
		<feedburner:origLink>http://lostintangent.com/2008/11/19/dynamic-data-models-metamodels-and-everything-in-between/</feedburner:origLink></item>
	</channel>
</rss>
