<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Windows Mobile Development</title>
    <link>http://blog.wmdev.net/</link>
    <description>Windows Mobile, Compact Framework, development, and life...</description>
    <language>en-us</language>
    <copyright>Christian Forsberg &amp; Andreas Sjöström</copyright>
    <lastBuildDate>Tue, 25 Nov 2008 22:05:50 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>chris@businessanyplace.net</managingEditor>
    <webMaster>chris@businessanyplace.net</webMaster>
    <item>
      <trackback:ping>http://blog.wmdev.net/Trackback.aspx?guid=e12c1c81-139f-44b1-a566-d10d2ad37011</trackback:ping>
      <pingback:server>http://blog.wmdev.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.wmdev.net/PermaLink,guid,e12c1c81-139f-44b1-a566-d10d2ad37011.aspx</pingback:target>
      <dc:creator>Chris</dc:creator>
      <wfw:comment>http://blog.wmdev.net/CommentView,guid,e12c1c81-139f-44b1-a566-d10d2ad37011.aspx</wfw:comment>
      <wfw:commentRss>http://blog.wmdev.net/SyndicationService.asmx/GetEntryCommentsRss?guid=e12c1c81-139f-44b1-a566-d10d2ad37011</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://blog.wmdev.net/content/binary/WindowsLiveWriter/KISSArchitecturepart10_B5DB/kisswmux_2.png">
            <img style="border-top-width: 1px; border-left-width: 1px; border-bottom-width: 1px; border-right-width: 1px" height="221" alt="KISS Architecture Mobile Sample" src="http://blog.wmdev.net/content/binary/WindowsLiveWriter/KISSArchitecturepart10_B5DB/kisswmux_thumb.png" width="176" align="right" border="0" />
          </a> This
is the final post in this series, and for a complete reference, here are the previous
parts: 
</p>
        <ul>
          <li>
            <a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank">Part
1</a> was a general introduction 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank">Part
2</a> outlined the architecture (tiers, etc) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank">Part
3</a> showed the benefit of loosely coupled tiers (distribution, cloud, etc)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank">Part
4</a> started the implementation by creating the entity data model (using ADO.NET
Entity Framework) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/07/KISSArchitecturePart5.aspx" target="_blank">Part
5</a> published the entity data model as a data service (using ADO.NET Data Services)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/11/KISSArchitecturePart6.aspx" target="_blank">Part
6</a> implemented the business domain (using the data service)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/14/KISSArchitecturePart7.aspx" target="_blank">Part
7</a> created the service (using WCF)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/18/KISSArchitecturePart8.aspx" target="_blank">Part
8</a> started the implementation of the mobile client (using WCF)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/21/KISSArchitecturePart9.aspx" target="_blank">Part
9</a> added offline support to the mobile client (using SQL Server Compact, etc)</li>
        </ul>
        <p>
The implemented architecture is published on <a href="http://www.codeplex.com/" target="_blank">CodePlex</a> in
a project called <a href="http://www.codeplex.com/kiss" target="_blank">KISS Architecture</a>,
and this means that you can access the full source code as well as discuss it, come
with suggested improvements, etc.
</p>
        <p>
On the upper right you see the <a href="http://www.microsoft.com/design/" target="_blank">UX</a> of
the sample client included in the architecture sample, and the functionality is that
a part of a city name can be entered in the text box, and when the "Get" menu item
is selected, the grid is filled with the matching customers. 
</p>
        <p>
Even if this series is complete, I will continue to build further on this architecture
blueprint, and any suggestions on things to add are most welcome. Any other feedback,
for that matter, is also welcome!
</p>
        <img width="0" height="0" src="http://blog.wmdev.net/aggbug.ashx?id=e12c1c81-139f-44b1-a566-d10d2ad37011" />
      </body>
      <title>KISS Architecture (part 10)</title>
      <guid isPermaLink="false">http://blog.wmdev.net/PermaLink,guid,e12c1c81-139f-44b1-a566-d10d2ad37011.aspx</guid>
      <link>http://blog.wmdev.net/2008/11/25/KISSArchitecturePart10.aspx</link>
      <pubDate>Tue, 25 Nov 2008 22:05:50 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://blog.wmdev.net/content/binary/WindowsLiveWriter/KISSArchitecturepart10_B5DB/kisswmux_2.png"&gt;&lt;img style="border-top-width: 1px; border-left-width: 1px; border-bottom-width: 1px; border-right-width: 1px" height="221" alt="KISS Architecture Mobile Sample" src="http://blog.wmdev.net/content/binary/WindowsLiveWriter/KISSArchitecturepart10_B5DB/kisswmux_thumb.png" width="176" align="right" border="0"&gt;&lt;/a&gt; This
is the final post in this series, and for a complete reference, here are the previous
parts: 
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank"&gt;Part
1&lt;/a&gt; was a general introduction 
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank"&gt;Part
2&lt;/a&gt; outlined the architecture (tiers, etc) 
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank"&gt;Part
3&lt;/a&gt; showed the benefit of loosely coupled tiers (distribution, cloud, etc)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank"&gt;Part
4&lt;/a&gt; started the implementation by creating the entity data model (using ADO.NET
Entity Framework) 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/07/KISSArchitecturePart5.aspx" target="_blank"&gt;Part
5&lt;/a&gt; published the entity data model as a data service (using ADO.NET Data Services)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/11/KISSArchitecturePart6.aspx" target="_blank"&gt;Part
6&lt;/a&gt; implemented the business domain (using the data service)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/14/KISSArchitecturePart7.aspx" target="_blank"&gt;Part
7&lt;/a&gt; created the service (using WCF)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/18/KISSArchitecturePart8.aspx" target="_blank"&gt;Part
8&lt;/a&gt; started the implementation of the mobile client (using WCF)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/21/KISSArchitecturePart9.aspx" target="_blank"&gt;Part
9&lt;/a&gt; added offline support to the mobile client (using SQL Server Compact, etc)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The implemented architecture is published on &lt;a href="http://www.codeplex.com/" target="_blank"&gt;CodePlex&lt;/a&gt; in
a project called &lt;a href="http://www.codeplex.com/kiss" target="_blank"&gt;KISS Architecture&lt;/a&gt;,
and this means that you can access the full source code as well as discuss it, come
with suggested improvements, etc.
&lt;/p&gt;
&lt;p&gt;
On the upper right you see the &lt;a href="http://www.microsoft.com/design/" target="_blank"&gt;UX&lt;/a&gt; of
the sample client included in the architecture sample, and the functionality is that
a part of a city name can be entered in the text box, and when the "Get" menu item
is selected, the grid is filled with the matching customers. 
&lt;p&gt;
Even if this series is complete, I will continue to build further on this architecture
blueprint, and any suggestions on things to add are most welcome. Any other feedback,
for that matter, is also welcome!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.wmdev.net/aggbug.ashx?id=e12c1c81-139f-44b1-a566-d10d2ad37011" /&gt;</description>
      <comments>http://blog.wmdev.net/CommentView,guid,e12c1c81-139f-44b1-a566-d10d2ad37011.aspx</comments>
      <category>Architecture</category>
      <category>Chris</category>
      <category>Compact Framework</category>
      <category>Windows Mobile</category>
    </item>
    <item>
      <trackback:ping>http://blog.wmdev.net/Trackback.aspx?guid=dd84dd97-f6a1-4c6a-97ef-098c77374410</trackback:ping>
      <pingback:server>http://blog.wmdev.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.wmdev.net/PermaLink,guid,dd84dd97-f6a1-4c6a-97ef-098c77374410.aspx</pingback:target>
      <dc:creator>Chris</dc:creator>
      <wfw:comment>http://blog.wmdev.net/CommentView,guid,dd84dd97-f6a1-4c6a-97ef-098c77374410.aspx</wfw:comment>
      <wfw:commentRss>http://blog.wmdev.net/SyndicationService.asmx/GetEntryCommentsRss?guid=dd84dd97-f6a1-4c6a-97ef-098c77374410</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In this part, I will continue the implementation of the presentation tier for Windows
Mobile, and if you want some background, please see the previous parts: 
</p>
        <ul>
          <li>
            <a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank">Part
1</a> was a general introduction 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank">Part
2</a> outlined the architecture (tiers, etc) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank">Part
3</a> showed the benefit of loosely coupled tiers (distribution, cloud, etc)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank">Part
4</a> started the implementation by creating the entity data model (using ADO.NET
Entity Framework) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/07/KISSArchitecturePart5.aspx" target="_blank">Part
5</a> published the entity data model as a data service (using ADO.NET Data Services)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/11/KISSArchitecturePart6.aspx" target="_blank">Part
6</a> implemented the business domain (using the data service)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/14/KISSArchitecturePart7.aspx" target="_blank">Part
7</a> created the service (using WCF)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/18/KISSArchitecturePart8.aspx" target="_blank">Part
8</a> started the implementation of the mobile client (using WCF)</li>
        </ul>
        <p>
The implemented architecture is published on <a href="http://www.codeplex.com/" target="_blank">CodePlex</a> in
a project called <a href="http://www.codeplex.com/kiss" target="_blank">KISS Architecture</a>,
and this means that you can access the full source code as well as discuss it, come
with suggested improvements, etc. As I walk you through the creation of the architecture,
I suggest you keep the source code handy to check out more details.
</p>
        <p>
In the previous part (8) of this blog series, I showed how the Windows Mobile client
can call a <a href="http://msdn.microsoft.com/en-us/library/ms735119.aspx" target="_blank">WCF</a> service
when connected to the network. However, there are a number of times when the mobile
device is not connected and still need to allow the application to function. It would
be really awesome if the new technologies like <a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" target="_blank">ADO.NET
Data Services</a>, <a href="http://msdn.microsoft.com/en-us/library/bb399572.aspx" target="_blank">ADO.NET
Entity Framework</a>, <a href="http://msdn.microsoft.com/en-us/library/bb386964.aspx" target="_blank">LINQ
to Entities</a>, etc, were available also for Windows Mobile and .NET Compact Framework,
as this would mean that we could use identical code (assemblies/tiers) on the client,
but unfortunately they are not (yet).
</p>
        <p>
So, what is the second best solution? I still want to make use of the entities defined
by the (data) service tier, and not maintain any custom entity code. Also, I want
the implementation to be as simple as possible (remember the <a href="http://en.wikipedia.org/wiki/KISS_principle" target="_blank">KISS
principle</a>) and make it as easy as possible to upgrade when these new technologies
are available. That means that I don't want to spend time writing my own custom framework,
but rather something simple that just works.
</p>
        <p>
To allow local data storage, I create a SQL Server Compact database by adding a "Database
File" named Northwind.sdf to the Kiss.Mobile project. To that database, I then add
the same tables that our order (data) service defines (see part 4 of this blog series).
I also add a plain class named Common.cs to the same project that is a singleton holding
a single connection to the database.
</p>
        <p>
With the local database in place and a connection to it, the code for the "Get" menu
item can be changed into:
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">private</span>
          <span style="color: black">
          </span>
          <span style="color: blue">void</span>
          <span style="color: black"> getMenuItem_Click(</span>
          <span style="color: blue">object</span>
          <span style="color: black"> sender, </span>
          <span style="color: #2b91af">EventArgs</span>
          <span style="color: black"> e)<br />
{<br />
    </span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black">[]
customers;<br />
    </span>
          <span style="color: blue">if</span>
          <span style="color: black">(online)<br />
    {<br />
        </span>
          <span style="color: #2b91af">OrderServiceClient</span>
          <span style="color: black">.EndpointAddress
= </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">EndpointAddress</span>
          <span style="color: black">(</span>
          <span style="color: #a31515">"http://192.168.0.100:2222/OrderService.svc"</span>
          <span style="color: black">);<br />
        </span>
          <span style="color: #2b91af">OrderServiceClient</span>
          <span style="color: black"> service
= </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderServiceClient</span>
          <span style="color: black">();<br />
        customers = service.GetCustomersByCity(cityTextBox.Text);<br />
    }<br />
    </span>
          <span style="color: blue">else<br /></span>
          <span style="color: black">    {<br />
        </span>
          <span style="color: #2b91af">OrderHandler</span>
          <span style="color: black"> data
= </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderHandler</span>
          <span style="color: black">();<br />
        customers = data.GetCustomersByCity(cityTextBox.Text);<br />
    }<br />
    dataGrid.DataSource = customers;<br />
}</span>
        </div>
        <p>
The variable indicating connection state (online) can be either manual or automatic
through SystemState.ConnectionsCount. Before we take a look at the OrderHandler class,
here's the updated code for the "Update" menu item:
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">private</span>
          <span style="color: black">
          </span>
          <span style="color: blue">void</span>
          <span style="color: black"> updateMenuItem_Click(</span>
          <span style="color: blue">object</span>
          <span style="color: black"> sender, </span>
          <span style="color: #2b91af">EventArgs</span>
          <span style="color: black"> e)<br />
{<br />
    </span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black"> customer
= ((</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black">[])dataGrid.DataSource)[0];<br /></span>
          <span style="color: black">    customer.City = customer.City
+ </span>
          <span style="color: #a31515">"X"</span>
          <span style="color: black">;<br /></span>
          <span style="color: black">
            <br />
    </span>
          <span style="color: blue">if</span>
          <span style="color: black">(online)<br />
    {<br />
        </span>
          <span style="color: #2b91af">OrderServiceClient</span>
          <span style="color: black"> service
= </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderServiceClient</span>
          <span style="color: black">();<br />
        service.UpdateCustomer(customer);<br />
    }<br />
    </span>
          <span style="color: blue">else<br /></span>
          <span style="color: black">    {<br />
        </span>
          <span style="color: #2b91af">OrderHandler</span>
          <span style="color: black"> data
= </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderHandler</span>
          <span style="color: black">();<br />
        data.UpdateCustomer(customer);<br />
    }<br />
}</span>
        </div>
        <p>
Ok, so with that in place, here's the implementation of the OrderHandler class:
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">class</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderHandler<br /></span>
          <span style="color: black">{<br />
    </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black">[]
GetCustomersByCity(</span>
          <span style="color: blue">string</span>
          <span style="color: black"> city)<br />
    {<br />
        </span>
          <span style="color: green">//var
q = from c in data.CustomerSet<br /></span>
          <span style="color: black">        </span>
          <span style="color: green">//       
where c.City.Contains(city)<br /></span>
          <span style="color: black">        </span>
          <span style="color: green">//       
select c;<br /></span>
          <span style="color: black">        </span>
          <span style="color: blue">return</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">HandlerHelper</span>
          <span style="color: black">.GetObjects&lt;</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black">&gt;(</span>
          <span style="color: blue">string</span>
          <span style="color: black">.Format(<br />
            </span>
          <span style="color: #a31515">"SELECT
* FROM Customers WHERE City LIKE '%{0}%'"</span>
          <span style="color: black">, city));<br />
    }<br /><br />
    </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">void</span>
          <span style="color: black"> UpdateCustomer(</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black"> customer)<br />
    {<br />
        </span>
          <span style="color: #2b91af">HandlerHelper</span>
          <span style="color: black">.UpdateObject&lt;</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black">&gt;(customer, </span>
          <span style="color: blue">string</span>
          <span style="color: black">.Format(<br />
            </span>
          <span style="color: #a31515">"SELECT
* FROM Customers WHERE CustomerID='{0}'"</span>
          <span style="color: black">, customer.CustomerID));<br />
    }<br />
}</span>
        </div>
        <p>
Not much happening here except that the handler make use of a helper that I wrote
to take care of the plumbing. I can use the entity type and I need to define the query
as plain SQL, but note that I've kept the desired LINQ as a comment in preparation
for the day when it can be used. Let's look at the implementation of the helper class:
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">static</span>
          <span style="color: black">
          </span>
          <span style="color: blue">class</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">HandlerHelper<br /></span>
          <span style="color: black">{<br />
    </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">static</span>
          <span style="color: black"> T[]
GetObjects&lt;T&gt;(</span>
          <span style="color: blue">string</span>
          <span style="color: black"> sql) </span>
          <span style="color: blue">where</span>
          <span style="color: black"> T
: </span>
          <span style="color: blue">new</span>
          <span style="color: black">()<br />
    {<br />
        </span>
          <span style="color: #2b91af">List</span>
          <span style="color: black">&lt;T&gt;
objects = </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">List</span>
          <span style="color: black">&lt;T&gt;();<br />
        </span>
          <span style="color: #2b91af">SqlCeCommand</span>
          <span style="color: black"> command
= </span>
          <span style="color: #2b91af">Common</span>
          <span style="color: black">.Values.DatabaseConnection.CreateCommand();<br />
        command.CommandText = sql;<br />
        </span>
          <span style="color: #2b91af">SqlCeDataReader</span>
          <span style="color: black"> reader
= command.ExecuteReader();<br />
        </span>
          <span style="color: #2b91af">Type</span>
          <span style="color: black"> type
= </span>
          <span style="color: blue">typeof</span>
          <span style="color: black">(T);<br />
        </span>
          <span style="color: blue">while</span>
          <span style="color: black">(reader.Read())<br />
        {<br />
            T entity = </span>
          <span style="color: blue">new</span>
          <span style="color: black"> T();<br />
            </span>
          <span style="color: blue">for</span>
          <span style="color: black">(</span>
          <span style="color: blue">int</span>
          <span style="color: black"> i
= 0; i &lt; reader.FieldCount; i++)<br />
            {<br /></span>
          <span style="color: black">                </span>
          <span style="color: #2b91af">PropertyInfo</span>
          <span style="color: black"> propertyInfo
= type.GetProperty(reader.GetName(i));<br />
               
propertyInfo.SetValue(entity, reader[i], </span>
          <span style="color: blue">null</span>
          <span style="color: black">);<br />
            }<br />
            objects.Add(entity);<br />
        }<br />
        </span>
          <span style="color: blue">return</span>
          <span style="color: black"> objects.ToArray();<br />
    }<br /><br />
    </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">static</span>
          <span style="color: black">
          </span>
          <span style="color: blue">void</span>
          <span style="color: black"> UpdateObject&lt;T&gt;(T
entity, </span>
          <span style="color: blue">string</span>
          <span style="color: black"> sql)<br />
    {<br />
        </span>
          <span style="color: #2b91af">SqlCeCommand</span>
          <span style="color: black"> command
= </span>
          <span style="color: #2b91af">Common</span>
          <span style="color: black">.Values.DatabaseConnection.CreateCommand();<br />
        command.CommandText = sql;<br />
        </span>
          <span style="color: #2b91af">SqlCeResultSet</span>
          <span style="color: black"> resultSet
= command.ExecuteResultSet(</span>
          <span style="color: #2b91af">ResultSetOptions</span>
          <span style="color: black">.Updatable);<br />
        resultSet.Read();<br />
        </span>
          <span style="color: #2b91af">Type</span>
          <span style="color: black"> type
= </span>
          <span style="color: blue">typeof</span>
          <span style="color: black">(T);<br />
        </span>
          <span style="color: blue">for</span>
          <span style="color: black">(</span>
          <span style="color: blue">int</span>
          <span style="color: black"> i
= 0; i &lt; resultSet.FieldCount; i++)<br />
        {<br />
            </span>
          <span style="color: #2b91af">PropertyInfo</span>
          <span style="color: black"> propertyInfo
= type.GetProperty(resultSet.GetName(i));<br />
            </span>
          <span style="color: blue">object</span>
          <span style="color: black"> value
= propertyInfo.GetValue(entity, </span>
          <span style="color: blue">null</span>
          <span style="color: black">);<br /></span>
          <span style="color: black">            </span>
          <span style="color: blue">if</span>
          <span style="color: black">(!value.Equals(resultSet.GetValue(i)))<br />
               
resultSet.SetValue(i, value);<br />
        }<br />
        resultSet.Update();<br />
    }<br />
}<br /></span>
        </div>
        <p>
The database connection kept by the singleton (Common.Values) is used to either query
or update the local database. Both methods are implemented using generics, and reflection
is used to find all the attributes (fields). To optimize performance, a data reader
is used for the query and a resultset is used for the update. Both methods are somewhat
simplified as some extra null handling is necessary but does not add to the discussion.
Using reflection is not an ideal solution, but remember that my goal is to keep the
implementation as simple as possible and also that this is not a solution for the
future, it's a temporary fix in the lack of the right technologies on the device.
When that day comes, the helper class can be scrapped, and the handler class either
updated or even replaced by something like LINQ to Entities.
</p>
        <p>
In the next part, I will conclude this series.
</p>
        <img width="0" height="0" src="http://blog.wmdev.net/aggbug.ashx?id=dd84dd97-f6a1-4c6a-97ef-098c77374410" />
      </body>
      <title>KISS Architecture (part 9)</title>
      <guid isPermaLink="false">http://blog.wmdev.net/PermaLink,guid,dd84dd97-f6a1-4c6a-97ef-098c77374410.aspx</guid>
      <link>http://blog.wmdev.net/2008/11/21/KISSArchitecturePart9.aspx</link>
      <pubDate>Fri, 21 Nov 2008 22:04:02 GMT</pubDate>
      <description>&lt;p&gt;
In this part, I will continue the implementation of the presentation tier for Windows
Mobile, and if you want some background, please see the previous parts: 
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank"&gt;Part
1&lt;/a&gt; was a general introduction 
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank"&gt;Part
2&lt;/a&gt; outlined the architecture (tiers, etc) 
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank"&gt;Part
3&lt;/a&gt; showed the benefit of loosely coupled tiers (distribution, cloud, etc)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank"&gt;Part
4&lt;/a&gt; started the implementation by creating the entity data model (using ADO.NET
Entity Framework) 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/07/KISSArchitecturePart5.aspx" target="_blank"&gt;Part
5&lt;/a&gt; published the entity data model as a data service (using ADO.NET Data Services)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/11/KISSArchitecturePart6.aspx" target="_blank"&gt;Part
6&lt;/a&gt; implemented the business domain (using the data service)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/14/KISSArchitecturePart7.aspx" target="_blank"&gt;Part
7&lt;/a&gt; created the service (using WCF)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/18/KISSArchitecturePart8.aspx" target="_blank"&gt;Part
8&lt;/a&gt; started the implementation of the mobile client (using WCF)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The implemented architecture is published on &lt;a href="http://www.codeplex.com/" target="_blank"&gt;CodePlex&lt;/a&gt; in
a project called &lt;a href="http://www.codeplex.com/kiss" target="_blank"&gt;KISS Architecture&lt;/a&gt;,
and this means that you can access the full source code as well as discuss it, come
with suggested improvements, etc. As I walk you through the creation of the architecture,
I suggest you keep the source code handy to check out more details.
&lt;/p&gt;
&lt;p&gt;
In the previous part (8) of this blog series, I showed how the Windows Mobile client
can call a &lt;a href="http://msdn.microsoft.com/en-us/library/ms735119.aspx" target="_blank"&gt;WCF&lt;/a&gt; service
when connected to the network. However, there are a number of times when the mobile
device is not connected and still need to allow the application to function. It would
be really awesome if the new technologies like &lt;a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" target="_blank"&gt;ADO.NET
Data Services&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/bb399572.aspx" target="_blank"&gt;ADO.NET
Entity Framework&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/bb386964.aspx" target="_blank"&gt;LINQ
to Entities&lt;/a&gt;, etc, were available also for Windows Mobile and .NET Compact Framework,
as this would mean that we could use identical code (assemblies/tiers) on the client,
but unfortunately they are not (yet).
&lt;/p&gt;
&lt;p&gt;
So, what is the second best solution? I still want to make use of the entities defined
by the (data) service tier, and not maintain any custom entity code. Also, I want
the implementation to be as simple as possible (remember the &lt;a href="http://en.wikipedia.org/wiki/KISS_principle" target="_blank"&gt;KISS
principle&lt;/a&gt;) and make it as easy as possible to upgrade when these new technologies
are available. That means that I don't want to spend time writing my own custom framework,
but rather something simple that just works.
&lt;/p&gt;
&lt;p&gt;
To allow local data storage, I create a SQL Server Compact database by adding a "Database
File" named Northwind.sdf to the Kiss.Mobile project. To that database, I then add
the same tables that our order (data) service defines (see part 4 of this blog series).
I also add a plain class named Common.cs to the same project that is a singleton holding
a single connection to the database.
&lt;/p&gt;
&lt;p&gt;
With the local database in place and a connection to it, the code for the "Get" menu
item can be changed into:
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt;&lt;span style="color: black"&gt; getMenuItem_Click(&lt;/span&gt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&lt;span style="color: black"&gt; sender, &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt;&lt;span style="color: black"&gt; e)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;[]
customers;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;if&lt;/span&gt;&lt;span style="color: black"&gt;(online)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderServiceClient&lt;/span&gt;&lt;span style="color: black"&gt;.EndpointAddress
= &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EndpointAddress&lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: #a31515"&gt;"http://192.168.0.100:2222/OrderService.svc"&lt;/span&gt;&lt;span style="color: black"&gt;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderServiceClient&lt;/span&gt;&lt;span style="color: black"&gt; service
= &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderServiceClient&lt;/span&gt;&lt;span style="color: black"&gt;();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; customers = service.GetCustomersByCity(cityTextBox.Text);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;else&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderHandler&lt;/span&gt;&lt;span style="color: black"&gt; data
= &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderHandler&lt;/span&gt;&lt;span style="color: black"&gt;();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; customers = data.GetCustomersByCity(cityTextBox.Text);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; dataGrid.DataSource = customers;&lt;br&gt;
}&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
The variable indicating connection state (online) can be either manual or automatic
through SystemState.ConnectionsCount. Before we take a look at the OrderHandler class,
here's the updated code for the "Update" menu item:
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt;&lt;span style="color: black"&gt; updateMenuItem_Click(&lt;/span&gt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&lt;span style="color: black"&gt; sender, &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt;&lt;span style="color: black"&gt; e)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt; customer
= ((&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;[])dataGrid.DataSource)[0];&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; customer.City = customer.City
+ &lt;/span&gt;&lt;span style="color: #a31515"&gt;"X"&lt;/span&gt;&lt;span style="color: black"&gt;;&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;if&lt;/span&gt;&lt;span style="color: black"&gt;(online)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderServiceClient&lt;/span&gt;&lt;span style="color: black"&gt; service
= &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderServiceClient&lt;/span&gt;&lt;span style="color: black"&gt;();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; service.UpdateCustomer(customer);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;else&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderHandler&lt;/span&gt;&lt;span style="color: black"&gt; data
= &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderHandler&lt;/span&gt;&lt;span style="color: black"&gt;();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data.UpdateCustomer(customer);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
Ok, so with that in place, here's the implementation of the OrderHandler class:
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;class&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderHandler&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;[]
GetCustomersByCity(&lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt; city)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green"&gt;//var
q = from c in data.CustomerSet&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green"&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
where c.City.Contains(city)&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green"&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
select c;&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HandlerHelper&lt;/span&gt;&lt;span style="color: black"&gt;.GetObjects&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt;.Format(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #a31515"&gt;"SELECT
* FROM Customers WHERE City LIKE '%{0}%'"&lt;/span&gt;&lt;span style="color: black"&gt;, city));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt;&lt;span style="color: black"&gt; UpdateCustomer(&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt; customer)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HandlerHelper&lt;/span&gt;&lt;span style="color: black"&gt;.UpdateObject&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;&amp;gt;(customer, &lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt;.Format(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #a31515"&gt;"SELECT
* FROM Customers WHERE CustomerID='{0}'"&lt;/span&gt;&lt;span style="color: black"&gt;, customer.CustomerID));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
Not much happening here except that the handler make use of a helper that I wrote
to take care of the plumbing. I can use the entity type and I need to define the query
as plain SQL, but note that I've kept the desired LINQ as a comment in preparation
for the day when it can be used. Let's look at the implementation of the helper class:
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;class&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HandlerHelper&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt;&lt;span style="color: black"&gt; T[]
GetObjects&amp;lt;T&amp;gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt; sql) &lt;/span&gt;&lt;span style="color: blue"&gt;where&lt;/span&gt;&lt;span style="color: black"&gt; T
: &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt;()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&lt;span style="color: black"&gt;&amp;lt;T&amp;gt;
objects = &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&lt;span style="color: black"&gt;&amp;lt;T&amp;gt;();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlCeCommand&lt;/span&gt;&lt;span style="color: black"&gt; command
= &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Common&lt;/span&gt;&lt;span style="color: black"&gt;.Values.DatabaseConnection.CreateCommand();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; command.CommandText = sql;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlCeDataReader&lt;/span&gt;&lt;span style="color: black"&gt; reader
= command.ExecuteReader();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;&lt;span style="color: black"&gt; type
= &lt;/span&gt;&lt;span style="color: blue"&gt;typeof&lt;/span&gt;&lt;span style="color: black"&gt;(T);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;while&lt;/span&gt;&lt;span style="color: black"&gt;(reader.Read())&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T entity = &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; T();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;for&lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&lt;span style="color: black"&gt; i
= 0; i &amp;lt; reader.FieldCount; i++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyInfo&lt;/span&gt;&lt;span style="color: black"&gt; propertyInfo
= type.GetProperty(reader.GetName(i));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
propertyInfo.SetValue(entity, reader[i], &lt;/span&gt;&lt;span style="color: blue"&gt;null&lt;/span&gt;&lt;span style="color: black"&gt;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; objects.Add(entity);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt;&lt;span style="color: black"&gt; objects.ToArray();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt;&lt;span style="color: black"&gt; UpdateObject&amp;lt;T&amp;gt;(T
entity, &lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt; sql)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlCeCommand&lt;/span&gt;&lt;span style="color: black"&gt; command
= &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Common&lt;/span&gt;&lt;span style="color: black"&gt;.Values.DatabaseConnection.CreateCommand();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; command.CommandText = sql;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlCeResultSet&lt;/span&gt;&lt;span style="color: black"&gt; resultSet
= command.ExecuteResultSet(&lt;/span&gt;&lt;span style="color: #2b91af"&gt;ResultSetOptions&lt;/span&gt;&lt;span style="color: black"&gt;.Updatable);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; resultSet.Read();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;&lt;span style="color: black"&gt; type
= &lt;/span&gt;&lt;span style="color: blue"&gt;typeof&lt;/span&gt;&lt;span style="color: black"&gt;(T);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;for&lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&lt;span style="color: black"&gt; i
= 0; i &amp;lt; resultSet.FieldCount; i++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyInfo&lt;/span&gt;&lt;span style="color: black"&gt; propertyInfo
= type.GetProperty(resultSet.GetName(i));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&lt;span style="color: black"&gt; value
= propertyInfo.GetValue(entity, &lt;/span&gt;&lt;span style="color: blue"&gt;null&lt;/span&gt;&lt;span style="color: black"&gt;);&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;if&lt;/span&gt;&lt;span style="color: black"&gt;(!value.Equals(resultSet.GetValue(i)))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
resultSet.SetValue(i, value);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; resultSet.Update();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;
The database connection kept by the singleton (Common.Values) is used to either query
or update the local database. Both methods are implemented using generics, and reflection
is used to find all the attributes (fields). To optimize performance, a data reader
is used for the query and a resultset is used for the update. Both methods are somewhat
simplified as some extra null handling is necessary but does not add to the discussion.
Using reflection is not an ideal solution, but remember that my goal is to keep the
implementation as simple as possible and also that this is not a solution for the
future, it's a temporary fix in the lack of the right technologies on the device.
When that day comes, the helper class can be scrapped, and the handler class either
updated or even replaced by something like LINQ to Entities.
&lt;/p&gt;
&lt;p&gt;
In the next part, I will conclude this series.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.wmdev.net/aggbug.ashx?id=dd84dd97-f6a1-4c6a-97ef-098c77374410" /&gt;</description>
      <comments>http://blog.wmdev.net/CommentView,guid,dd84dd97-f6a1-4c6a-97ef-098c77374410.aspx</comments>
      <category>Architecture</category>
      <category>Chris</category>
      <category>Compact Framework</category>
      <category>Windows Mobile</category>
    </item>
    <item>
      <trackback:ping>http://blog.wmdev.net/Trackback.aspx?guid=2b2ec7c1-01e2-4cd3-ae21-cdb17d617990</trackback:ping>
      <pingback:server>http://blog.wmdev.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.wmdev.net/PermaLink,guid,2b2ec7c1-01e2-4cd3-ae21-cdb17d617990.aspx</pingback:target>
      <dc:creator>Chris</dc:creator>
      <wfw:comment>http://blog.wmdev.net/CommentView,guid,2b2ec7c1-01e2-4cd3-ae21-cdb17d617990.aspx</wfw:comment>
      <wfw:commentRss>http://blog.wmdev.net/SyndicationService.asmx/GetEntryCommentsRss?guid=2b2ec7c1-01e2-4cd3-ae21-cdb17d617990</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In this part, I will start the implementation of the presentation tier for Windows
Mobile, and if you want some background, please see the previous parts: 
</p>
        <ul>
          <li>
            <a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank">Part
1</a> was a general introduction 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank">Part
2</a> outlined the architecture (tiers, etc) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank">Part
3</a> showed the benefit of loosely coupled tiers (distribution, cloud, etc)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank">Part
4</a> started the implementation by creating the entity data model (using ADO.NET
Entity Framework) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/07/KISSArchitecturePart5.aspx" target="_blank">Part
5</a> published the entity data model as a data service (using ADO.NET Data Services)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/11/KISSArchitecturePart6.aspx" target="_blank">Part
6</a> implemented the business domain (using the data service)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/14/KISSArchitecturePart7.aspx" target="_blank">Part
7</a> created the service (using WCF)</li>
        </ul>
        <p>
The implemented architecture is published on CodePlex in a project called <a href="http://www.codeplex.com/kiss" target="_blank">KISS
Architecture</a>, and this means that you can access the full source code as well
as discuss it, come with suggested improvements, etc. As I walk you through the creation
of the architecture, I suggest you keep the source code handy to check out more details.
</p>
        <p>
          <a href="http://blog.wmdev.net/content/binary/WindowsLiveWriter/KISSArchitecturepart8_9776/kisswmform_2.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 0px 0px; border-right-width: 0px" height="224" alt="kisswmform" src="http://blog.wmdev.net/content/binary/WindowsLiveWriter/KISSArchitecturepart8_9776/kisswmform_thumb.png" width="180" align="left" border="0" />
          </a>
        </p>
        <p>
I start the implementation of a Windows Mobile client by creating a new project of
type "Smart Device Project", name it Kiss.Mobile, select "Windows Mobile 5.0 Smartphone
SDK", ".NET Compact Framework Version 3.5", and "Device Application". This will allow
it to run on many of the devices on the market, and still use the latest framework
with new functionality. That new functionality include WCF, so let's move on by creating
a reference to the WCF service I created in the previous part (7). You do that by
using the tool NetCFSvcUtil that is included in the <a href="http://blog.wmdev.net/ct.ashx?id=366632de-0a7b-46b2-9cd4-ca4e1662b4f1&amp;url=http%3a%2f%2fwww.microsoft.com%2fdownloads%2fdetails.aspx%3ffamilyId%3dc8174c14-a27d-4148-bf01-86c2e0953eab">Power
Toys for .NET Compact Framework 3.5</a>. For more details on using this tool, please
see <a href="http://blog.wmdev.net/2008/05/16/ANewMobileNtierArchitecturePart8.aspx" target="_blank">A
New Mobile N-tier Architecture (part 8)</a>.
</p>
        <p>
Let's implement a simple user interface by renaming the automatically generated form
(Form1) to MainForm, and add three controls (a Label named cityLabel, a TextBox named
cityTextBox, and a DataGrid named simply dataGrid) to it. Also, add a MainMenu named
mainMenu and add menu options to it named doneMenuItem, getMenuItem, and updateMenuItem.
The result should look similar to the figure on the left.
</p>
        <p>
With the service reference and the user interface in place, it's time to implement
some presentation logic. Here's the code for the "Get" menu item:
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">private</span>
          <span style="color: black">
          </span>
          <span style="color: blue">void</span>
          <span style="color: black"> getMenuItem_Click(</span>
          <span style="color: blue">object</span>
          <span style="color: black"> sender, </span>
          <span style="color: #2b91af">EventArgs</span>
          <span style="color: black"> e)<br />
{<br /></span>
          <span style="color: black">    </span>
          <span style="color: #2b91af">OrderServiceClient</span>
          <span style="color: black">.EndpointAddress
= </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">EndpointAddress</span>
          <span style="color: black">(</span>
          <span style="color: #a31515">"http://192.168.0.100:2222/OrderService.svc"</span>
          <span style="color: black">);<br />
    </span>
          <span style="color: #2b91af">OrderServiceClient</span>
          <span style="color: black"> service
= </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderServiceClient</span>
          <span style="color: black">();<br />
    <span style="color: #2b91af">Customer</span><span style="color: black">[] </span>customers
= service.GetCustomersByCity(cityTextBox.Text);<br /></span>
          <span style="color: black">    dataGrid.DataSource = customers;<br />
}</span>
        </div>
        <p>
Note how easy it would be to redirect the client to use the service in another location
(other server, cloud, etc) by just changing the service URI. Also note how the definition
of the entity (Customer) have come all the way from the data service without any manual
coding anywhere, and when it is changed, it's simply a matter of updating the service
references in all tiers. The observant will note that the List&lt;Customer&gt; return
value of the service is transformed into a plain array when serialized by WCF, but
if a list is preferred, this code could be used:
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: #2b91af">List</span>
          <span style="color: black">&lt;</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black">&gt;
customers = service.GetCustomersByCity(cityTextBox.Text).ToList();</span>
        </div>
        <p>
The code for the "Update" menu item looks like this:
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">private</span>
          <span style="color: black">
          </span>
          <span style="color: blue">void</span>
          <span style="color: black"> updateMenuItem_Click(</span>
          <span style="color: blue">object</span>
          <span style="color: black"> sender, </span>
          <span style="color: #2b91af">EventArgs</span>
          <span style="color: black"> e)<br />
{<br />
    </span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black"> customer
= ((</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black">[])dataGrid.DataSource)[0];<br /></span>
          <span style="color: black">    customer.City = customer.City
+ </span>
          <span style="color: #a31515">"X"</span>
          <span style="color: black">;<br /></span>
          <span style="color: black">    </span>
          <span style="color: #2b91af">OrderServiceClient</span>
          <span style="color: black"> service
= </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderServiceClient</span>
          <span style="color: black">();<br />
    service.UpdateCustomer(customer);<br /></span>
          <span style="color: black">}</span>
        </div>
        <p>
It first takes the first row in the grid, and adds an "X" to the City attribute. Then
the service is called with the updated entity instance (customer). It can't be much
simpler than this!
</p>
        <p>
In the next part, I will continue the implementation of the Windows Mobile client
by adding offline support.
</p>
        <img width="0" height="0" src="http://blog.wmdev.net/aggbug.ashx?id=2b2ec7c1-01e2-4cd3-ae21-cdb17d617990" />
      </body>
      <title>KISS Architecture (part 8)</title>
      <guid isPermaLink="false">http://blog.wmdev.net/PermaLink,guid,2b2ec7c1-01e2-4cd3-ae21-cdb17d617990.aspx</guid>
      <link>http://blog.wmdev.net/2008/11/18/KISSArchitecturePart8.aspx</link>
      <pubDate>Tue, 18 Nov 2008 22:02:41 GMT</pubDate>
      <description>&lt;p&gt;
In this part, I will start the implementation of the presentation tier for Windows
Mobile, and if you want some background, please see the previous parts: 
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank"&gt;Part
1&lt;/a&gt; was a general introduction 
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank"&gt;Part
2&lt;/a&gt; outlined the architecture (tiers, etc) 
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank"&gt;Part
3&lt;/a&gt; showed the benefit of loosely coupled tiers (distribution, cloud, etc)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank"&gt;Part
4&lt;/a&gt; started the implementation by creating the entity data model (using ADO.NET
Entity Framework) 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/07/KISSArchitecturePart5.aspx" target="_blank"&gt;Part
5&lt;/a&gt; published the entity data model as a data service (using ADO.NET Data Services)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/11/KISSArchitecturePart6.aspx" target="_blank"&gt;Part
6&lt;/a&gt; implemented the business domain (using the data service)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/14/KISSArchitecturePart7.aspx" target="_blank"&gt;Part
7&lt;/a&gt; created the service (using WCF)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The implemented architecture is published on CodePlex in a project called &lt;a href="http://www.codeplex.com/kiss" target="_blank"&gt;KISS
Architecture&lt;/a&gt;, and this means that you can access the full source code as well
as discuss it, come with suggested improvements, etc. As I walk you through the creation
of the architecture, I suggest you keep the source code handy to check out more details.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.wmdev.net/content/binary/WindowsLiveWriter/KISSArchitecturepart8_9776/kisswmform_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 0px 0px; border-right-width: 0px" height="224" alt="kisswmform" src="http://blog.wmdev.net/content/binary/WindowsLiveWriter/KISSArchitecturepart8_9776/kisswmform_thumb.png" width="180" align="left" border="0"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
I start the implementation of a Windows Mobile client by creating a new project of
type "Smart Device Project", name it Kiss.Mobile, select "Windows Mobile 5.0 Smartphone
SDK", ".NET Compact Framework Version 3.5", and "Device Application". This will allow
it to run on many of the devices on the market, and still use the latest framework
with new functionality. That new functionality include WCF, so let's move on by creating
a reference to the WCF service I created in the previous part (7). You do that by
using the tool NetCFSvcUtil that is included in the &lt;a href="http://blog.wmdev.net/ct.ashx?id=366632de-0a7b-46b2-9cd4-ca4e1662b4f1&amp;amp;url=http%3a%2f%2fwww.microsoft.com%2fdownloads%2fdetails.aspx%3ffamilyId%3dc8174c14-a27d-4148-bf01-86c2e0953eab"&gt;Power
Toys for .NET Compact Framework 3.5&lt;/a&gt;. For more details on using this tool, please
see &lt;a href="http://blog.wmdev.net/2008/05/16/ANewMobileNtierArchitecturePart8.aspx" target="_blank"&gt;A
New Mobile N-tier Architecture (part 8)&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Let's implement a simple user interface by renaming the automatically generated form
(Form1) to MainForm, and add three controls (a Label named cityLabel, a TextBox named
cityTextBox, and a DataGrid named simply dataGrid) to it. Also, add a MainMenu named
mainMenu and add menu options to it named doneMenuItem, getMenuItem, and updateMenuItem.
The result should look similar to the figure on the left.
&lt;/p&gt;
&lt;p&gt;
With the service reference and the user interface in place, it's time to implement
some presentation logic. Here's the code for the "Get" menu item:
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt;&lt;span style="color: black"&gt; getMenuItem_Click(&lt;/span&gt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&lt;span style="color: black"&gt; sender, &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt;&lt;span style="color: black"&gt; e)&lt;br&gt;
{&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderServiceClient&lt;/span&gt;&lt;span style="color: black"&gt;.EndpointAddress
= &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EndpointAddress&lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: #a31515"&gt;"http://192.168.0.100:2222/OrderService.svc"&lt;/span&gt;&lt;span style="color: black"&gt;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderServiceClient&lt;/span&gt;&lt;span style="color: black"&gt; service
= &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderServiceClient&lt;/span&gt;&lt;span style="color: black"&gt;();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;[] &lt;/span&gt;customers
= service.GetCustomersByCity(cityTextBox.Text);&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataGrid.DataSource = customers;&lt;br&gt;
}&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
Note how easy it would be to redirect the client to use the service in another location
(other server, cloud, etc) by just changing the service URI. Also note how the definition
of the entity (Customer) have come all the way from the data service without any manual
coding anywhere, and when it is changed, it's simply a matter of updating the service
references in all tiers. The observant will note that the List&amp;lt;Customer&amp;gt; return
value of the service is transformed into a plain array when serialized by WCF, but
if a list is preferred, this code could be used:
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&lt;span style="color: black"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;&amp;gt;
customers = service.GetCustomersByCity(cityTextBox.Text).ToList();&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
The code for the "Update" menu item looks like this:
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt;&lt;span style="color: black"&gt; updateMenuItem_Click(&lt;/span&gt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&lt;span style="color: black"&gt; sender, &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt;&lt;span style="color: black"&gt; e)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt; customer
= ((&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;[])dataGrid.DataSource)[0];&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; customer.City = customer.City
+ &lt;/span&gt;&lt;span style="color: #a31515"&gt;"X"&lt;/span&gt;&lt;span style="color: black"&gt;;&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderServiceClient&lt;/span&gt;&lt;span style="color: black"&gt; service
= &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderServiceClient&lt;/span&gt;&lt;span style="color: black"&gt;();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; service.UpdateCustomer(customer);&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;}&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
It first takes the first row in the grid, and adds an "X" to the City attribute. Then
the service is called with the updated entity instance (customer). It can't be much
simpler than this!
&lt;/p&gt;
&lt;p&gt;
In the next part, I will continue the implementation of the Windows Mobile client
by adding offline support.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.wmdev.net/aggbug.ashx?id=2b2ec7c1-01e2-4cd3-ae21-cdb17d617990" /&gt;</description>
      <comments>http://blog.wmdev.net/CommentView,guid,2b2ec7c1-01e2-4cd3-ae21-cdb17d617990.aspx</comments>
      <category>Architecture</category>
      <category>Chris</category>
      <category>Compact Framework</category>
      <category>Windows Mobile</category>
    </item>
    <item>
      <trackback:ping>http://blog.wmdev.net/Trackback.aspx?guid=8689c7f7-5721-4740-914f-b7da38c56ae2</trackback:ping>
      <pingback:server>http://blog.wmdev.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.wmdev.net/PermaLink,guid,8689c7f7-5721-4740-914f-b7da38c56ae2.aspx</pingback:target>
      <dc:creator>Chris</dc:creator>
      <wfw:comment>http://blog.wmdev.net/CommentView,guid,8689c7f7-5721-4740-914f-b7da38c56ae2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.wmdev.net/SyndicationService.asmx/GetEntryCommentsRss?guid=8689c7f7-5721-4740-914f-b7da38c56ae2</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In this part, I will continue the implementation of the middle tier by publishing
a <a href="http://msdn.microsoft.com/en-us/library/ms735119.aspx" target="_blank">WCF</a> service,
and if you want some background, please see the previous parts: 
</p>
        <ul>
          <li>
            <a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank">Part
1</a> was a general introduction 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank">Part
2</a> outlined the architecture (tiers, etc) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank">Part
3</a> showed the benefit of loosely coupled tiers (distribution, cloud, etc)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank">Part
4</a> started the implementation by creating the entity data model (using ADO.NET
Entity Framework) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/07/KISSArchitecturePart5.aspx" target="_blank">Part
5</a> published the entity data model as a data service (using ADO.NET Data Services)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/11/KISSArchitecturePart6.aspx" target="_blank">Part
6</a> implemented the business domain (using the data service)</li>
        </ul>
        <p>
The implemented architecture is published on CodePlex in a project called <a href="http://www.codeplex.com/kiss" target="_blank">KISS
Architecture</a>, and this means that you can access the full source code as well
as discuss it, come with suggested improvements, etc. As I walk you through the creation
of the architecture, I suggest you keep the source code handy to check out more details.
</p>
        <p>
It's time to create the service, and I do that by creating a new project of type "WCF
Service Application" and name it Kiss.Service. First I add a reference to the Kiss.Domain
project, and then I rename the automatically generated interface to IOrderService.cs.
I change its code to...
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">namespace</span>
          <span style="color: black"> Kiss.Service<br />
{<br />
    [</span>
          <span style="color: #2b91af">ServiceContract</span>
          <span style="color: black">]<br />
    </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">interface</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">IOrderService<br /></span>
          <span style="color: black">    {<br />
        [</span>
          <span style="color: #2b91af">OperationContract</span>
          <span style="color: black">]<br />
        </span>
          <span style="color: #2b91af">List</span>
          <span style="color: black">&lt;</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black">&gt;
GetCustomersByCity(</span>
          <span style="color: blue">string</span>
          <span style="color: black"> city);<br /><br />
        [</span>
          <span style="color: #2b91af">OperationContract</span>
          <span style="color: black">]<br />
        </span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black"> GetCustomer(</span>
          <span style="color: blue">string</span>
          <span style="color: black"> id);<br /><br />
        [</span>
          <span style="color: #2b91af">OperationContract</span>
          <span style="color: black">]<br />
        </span>
          <span style="color: blue">void</span>
          <span style="color: black"> UpdateCustomer(</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black"> customer);<br />
    }<br />
}</span>
        </div>
        <p>
...and make sure all references to it is refactored (there are a number of places,
like in the Web.config file). Then I rename the generated service class to OrderService.cs
and update its (references and) code to:
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">namespace</span>
          <span style="color: black"> Kiss.Service<br />
{<br />
    [</span>
          <span style="color: #2b91af">AspNetCompatibilityRequirements</span>
          <span style="color: black">(RequirementsMode
= </span>
          <span style="color: #2b91af">AspNetCompatibilityRequirementsMode</span>
          <span style="color: black">.Allowed)]<br />
    </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">class</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderService</span>
          <span style="color: black"> : </span>
          <span style="color: #2b91af">IOrderService<br /></span>
          <span style="color: black">    {<br />
        </span>
          <span style="color: blue">private</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderDomain</span>
          <span style="color: black"> orderDomain
= </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderDomain</span>
          <span style="color: black">();<br /><br />
        </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">List</span>
          <span style="color: black">&lt;</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black">&gt;
GetCustomersByCity(</span>
          <span style="color: blue">string</span>
          <span style="color: black"> city)<br />
        {<br />
            </span>
          <span style="color: blue">return</span>
          <span style="color: black"> orderDomain.GetCustomersByCity(city);<br />
        }<br /><br />
        </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black"> GetCustomer(</span>
          <span style="color: blue">string</span>
          <span style="color: black"> id)<br />
        {<br />
            </span>
          <span style="color: blue">return</span>
          <span style="color: black"> orderDomain.GetCustomer(id);<br />
        }<br /><br />
        </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">void</span>
          <span style="color: black"> UpdateCustomer(</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black"> customer)<br />
        {<br />
            orderDomain.UpdateCustomer(customer);<br />
        }<br />
    }<br />
}</span>
        </div>
        <p>
This is the actual implementation of the service that implement the interface mentioned
above, and each method simply use the domain class to do its work. The decoration
(attribute) of the service class is needed to make the service available for a Silverlight
2 client. Even if no such client exist for Windows Mobile yet, this is a good measure
to prepare for the future.
</p>
        <p>
Another similar measure is to include two more files in the service project, and the
first is clientaccesspolicy.xml and the second is crossdomain.xml. The purpose of
these files is to make the service available for applications that make cross domain
calls, and the first is specific to Silverlight and the second is used by Flash applications,
among others. Here is a sample clientaccesspolicy.xml file...
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">&lt;?</span>
          <span style="color: #a31515">xml</span>
          <span style="color: blue">
          </span>
          <span style="color: red">version</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">1.0</span>
          <span style="color: black">"</span>
          <span style="color: blue">
          </span>
          <span style="color: red">encoding</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">utf-8</span>
          <span style="color: black">"</span>
          <span style="color: blue"> ?&gt;<br />
&lt;</span>
          <span style="color: #a31515">access-policy</span>
          <span style="color: blue">&gt;<br />
    &lt;</span>
          <span style="color: #a31515">cross-domain-access</span>
          <span style="color: blue">&gt;<br />
        &lt;</span>
          <span style="color: #a31515">policy</span>
          <span style="color: blue">&gt;<br />
            &lt;</span>
          <span style="color: #a31515">allow-from</span>
          <span style="color: blue">
          </span>
          <span style="color: red">http-request-headers</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">*</span>
          <span style="color: black">"</span>
          <span style="color: blue">&gt;<br />
               
&lt;</span>
          <span style="color: #a31515">domain</span>
          <span style="color: blue">
          </span>
          <span style="color: red">uri</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">*</span>
          <span style="color: black">"</span>
          <span style="color: blue"> /&gt;<br />
            &lt;/</span>
          <span style="color: #a31515">allow-from</span>
          <span style="color: blue">&gt;<br />
            &lt;</span>
          <span style="color: #a31515">grant-to</span>
          <span style="color: blue">&gt;<br />
               
&lt;</span>
          <span style="color: #a31515">resource</span>
          <span style="color: blue">
          </span>
          <span style="color: red">path</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">/</span>
          <span style="color: black">"</span>
          <span style="color: blue">
          </span>
          <span style="color: red">include-subpaths</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">true</span>
          <span style="color: black">"</span>
          <span style="color: blue"> /&gt;<br />
            &lt;/</span>
          <span style="color: #a31515">grant-to</span>
          <span style="color: blue">&gt;<br />
        &lt;/</span>
          <span style="color: #a31515">policy</span>
          <span style="color: blue">&gt;<br />
    &lt;/</span>
          <span style="color: #a31515">cross-domain-access</span>
          <span style="color: blue">&gt;<br />
&lt;/</span>
          <span style="color: #a31515">access-policy</span>
          <span style="color: blue">&gt;</span>
        </div>
        <p>
...that allow any client to access the service. A final measure that is necessary
for both Silverlight and Windows Mobile WCF clients is to make sure that the service
use the basicHttpBinding, and here is an extract of the Web.config file:
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">system.serviceModel</span>
          <span style="color: blue">&gt;<br />
    &lt;</span>
          <span style="color: #a31515">services</span>
          <span style="color: blue">&gt;<br />
        &lt;</span>
          <span style="color: #a31515">service</span>
          <span style="color: blue">
          </span>
          <span style="color: red">behaviorConfiguration</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">Kiss.Service.OrderServiceBehavior</span>
          <span style="color: black">"</span>
          <span style="color: blue">
          </span>
          <span style="color: red">name</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">Kiss.Service.OrderService</span>
          <span style="color: black">"</span>
          <span style="color: blue">&gt;<br />
            &lt;</span>
          <span style="color: #a31515">endpoint</span>
          <span style="color: blue">
          </span>
          <span style="color: red">address</span>
          <span style="color: blue">=</span>
          <span style="color: black">""</span>
          <span style="color: blue">
          </span>
          <span style="color: red">binding</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">basicHttpBinding</span>
          <span style="color: black">"</span>
          <span style="color: blue">
          </span>
          <span style="color: red">contract</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">Kiss.Service.IOrderService</span>
          <span style="color: black">"</span>
          <span style="color: blue">&gt;</span>
        </div>
        <p>
In the next part, I will start implementing the Windows Mobile client.
</p>
        <img width="0" height="0" src="http://blog.wmdev.net/aggbug.ashx?id=8689c7f7-5721-4740-914f-b7da38c56ae2" />
      </body>
      <title>KISS Architecture (part 7)</title>
      <guid isPermaLink="false">http://blog.wmdev.net/PermaLink,guid,8689c7f7-5721-4740-914f-b7da38c56ae2.aspx</guid>
      <link>http://blog.wmdev.net/2008/11/14/KISSArchitecturePart7.aspx</link>
      <pubDate>Fri, 14 Nov 2008 22:01:15 GMT</pubDate>
      <description>&lt;p&gt;
In this part, I will continue the implementation of the middle tier by publishing
a &lt;a href="http://msdn.microsoft.com/en-us/library/ms735119.aspx" target="_blank"&gt;WCF&lt;/a&gt; service,
and if you want some background, please see the previous parts: 
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank"&gt;Part
1&lt;/a&gt; was a general introduction 
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank"&gt;Part
2&lt;/a&gt; outlined the architecture (tiers, etc) 
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank"&gt;Part
3&lt;/a&gt; showed the benefit of loosely coupled tiers (distribution, cloud, etc)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank"&gt;Part
4&lt;/a&gt; started the implementation by creating the entity data model (using ADO.NET
Entity Framework) 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/07/KISSArchitecturePart5.aspx" target="_blank"&gt;Part
5&lt;/a&gt; published the entity data model as a data service (using ADO.NET Data Services)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/11/KISSArchitecturePart6.aspx" target="_blank"&gt;Part
6&lt;/a&gt; implemented the business domain (using the data service)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The implemented architecture is published on CodePlex in a project called &lt;a href="http://www.codeplex.com/kiss" target="_blank"&gt;KISS
Architecture&lt;/a&gt;, and this means that you can access the full source code as well
as discuss it, come with suggested improvements, etc. As I walk you through the creation
of the architecture, I suggest you keep the source code handy to check out more details.
&lt;/p&gt;
&lt;p&gt;
It's time to create the service, and I do that by creating a new project of type "WCF
Service Application" and name it Kiss.Service. First I add a reference to the Kiss.Domain
project, and then I rename the automatically generated interface to IOrderService.cs.
I change its code to...
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;namespace&lt;/span&gt;&lt;span style="color: black"&gt; Kiss.Service&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceContract&lt;/span&gt;&lt;span style="color: black"&gt;]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;interface&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IOrderService&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;/span&gt;&lt;span style="color: #2b91af"&gt;OperationContract&lt;/span&gt;&lt;span style="color: black"&gt;]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&lt;span style="color: black"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;&amp;gt;
GetCustomersByCity(&lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt; city);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;/span&gt;&lt;span style="color: #2b91af"&gt;OperationContract&lt;/span&gt;&lt;span style="color: black"&gt;]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt; GetCustomer(&lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt; id);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;/span&gt;&lt;span style="color: #2b91af"&gt;OperationContract&lt;/span&gt;&lt;span style="color: black"&gt;]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt;&lt;span style="color: black"&gt; UpdateCustomer(&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt; customer);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
...and make sure all references to it is refactored (there are a number of places,
like in the Web.config file). Then I rename the generated service class to OrderService.cs
and update its (references and) code to:
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;namespace&lt;/span&gt;&lt;span style="color: black"&gt; Kiss.Service&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;/span&gt;&lt;span style="color: #2b91af"&gt;AspNetCompatibilityRequirements&lt;/span&gt;&lt;span style="color: black"&gt;(RequirementsMode
= &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AspNetCompatibilityRequirementsMode&lt;/span&gt;&lt;span style="color: black"&gt;.Allowed)]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;class&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderService&lt;/span&gt;&lt;span style="color: black"&gt; : &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IOrderService&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderDomain&lt;/span&gt;&lt;span style="color: black"&gt; orderDomain
= &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderDomain&lt;/span&gt;&lt;span style="color: black"&gt;();&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&lt;span style="color: black"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;&amp;gt;
GetCustomersByCity(&lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt; city)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt;&lt;span style="color: black"&gt; orderDomain.GetCustomersByCity(city);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt; GetCustomer(&lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt; id)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt;&lt;span style="color: black"&gt; orderDomain.GetCustomer(id);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt;&lt;span style="color: black"&gt; UpdateCustomer(&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt; customer)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; orderDomain.UpdateCustomer(customer);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
This is the actual implementation of the service that implement the interface mentioned
above, and each method simply use the domain class to do its work. The decoration
(attribute) of the service class is needed to make the service available for a Silverlight
2 client. Even if no such client exist for Windows Mobile yet, this is a good measure
to prepare for the future.
&lt;/p&gt;
&lt;p&gt;
Another similar measure is to include two more files in the service project, and the
first is clientaccesspolicy.xml and the second is crossdomain.xml. The purpose of
these files is to make the service available for applications that make cross domain
calls, and the first is specific to Silverlight and the second is used by Flash applications,
among others. Here is a sample clientaccesspolicy.xml file...
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #a31515"&gt;xml&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;version&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;1.0&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;encoding&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;utf-8&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt; ?&amp;gt;&lt;br&gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;access-policy&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cross-domain-access&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;policy&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;allow-from&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;http-request-headers&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;*&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;domain&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;uri&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;*&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt; /&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;allow-from&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;grant-to&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;resource&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;path&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;/&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;include-subpaths&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;true&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt; /&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;grant-to&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;policy&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;cross-domain-access&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;access-policy&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
...that allow any client to access the service. A final measure that is necessary
for both Silverlight and Windows Mobile WCF clients is to make sure that the service
use the basicHttpBinding, and here is an extract of the Web.config file:
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;services&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;service&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;behaviorConfiguration&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;Kiss.Service.OrderServiceBehavior&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;Kiss.Service.OrderService&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;endpoint&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;address&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;""&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;basicHttpBinding&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;contract&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;Kiss.Service.IOrderService&lt;/span&gt;&lt;span style="color: black"&gt;"&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
In the next part, I will start implementing the Windows Mobile client.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.wmdev.net/aggbug.ashx?id=8689c7f7-5721-4740-914f-b7da38c56ae2" /&gt;</description>
      <comments>http://blog.wmdev.net/CommentView,guid,8689c7f7-5721-4740-914f-b7da38c56ae2.aspx</comments>
      <category>Architecture</category>
      <category>Chris</category>
      <category>Compact Framework</category>
      <category>Windows Mobile</category>
    </item>
    <item>
      <trackback:ping>http://blog.wmdev.net/Trackback.aspx?guid=7a099d05-e7d3-407b-a5f2-18a40c1a03c1</trackback:ping>
      <pingback:server>http://blog.wmdev.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.wmdev.net/PermaLink,guid,7a099d05-e7d3-407b-a5f2-18a40c1a03c1.aspx</pingback:target>
      <dc:creator>Chris</dc:creator>
      <wfw:comment>http://blog.wmdev.net/CommentView,guid,7a099d05-e7d3-407b-a5f2-18a40c1a03c1.aspx</wfw:comment>
      <wfw:commentRss>http://blog.wmdev.net/SyndicationService.asmx/GetEntryCommentsRss?guid=7a099d05-e7d3-407b-a5f2-18a40c1a03c1</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In this part, I will start the implementation of the middle tier, and if you want
some background, please see the previous parts: 
</p>
        <ul>
          <li>
            <a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank">Part
1</a> was a general introduction 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank">Part
2</a> outlined the architecture (tiers, etc) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank">Part
3</a> showed the benefit of loosely coupled tiers (distribution, cloud, etc)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank">Part
4</a> started the implementation by creating the entity data model (using ADO.NET
Entity Framework) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/11/07/KISSArchitecturePart5.aspx" target="_blank">Part
5</a> published the entity data model as a data service (using ADO.NET Data Services)</li>
        </ul>
        <p>
The implemented architecture is published on CodePlex in a project called <a href="http://www.codeplex.com/kiss" target="_blank">KISS
Architecture</a>, and this means that you can access the full source code as well
as discuss it, come with suggested improvements, etc. As I walk you through the creation
of the architecture, I suggest you keep the source code handy to check out more details.
</p>
        <p>
With the data tier in place, I'm ready to create the middle tier, and I start by creating
a plain class library named Kiss.Domain which will hold my business domain classes.
I add a service reference to the data service created in the previous part (5) of
this blog series, and get the complete entity data model through the generated proxy
class. Then I create a plain class named OrderDomain.cs and add the following code
to it:
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">namespace</span>
          <span style="color: black"> Kiss.Domain<br />
{<br />
    </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">class</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderDomain<br /></span>
          <span style="color: black">    {<br />
        </span>
          <span style="color: #2b91af">OrderEntities</span>
          <span style="color: black"> data
= </span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderEntities</span>
          <span style="color: black">(</span>
          <span style="color: blue">new</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">Uri</span>
          <span style="color: black">(</span>
          <span style="color: #a31515">"http://localhost:1111/OrderDataService.svc"</span>
          <span style="color: black">));<br /><br />
        </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">List</span>
          <span style="color: black">&lt;</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black">&gt;
GetCustomersByCity(</span>
          <span style="color: blue">string</span>
          <span style="color: black"> city)<br />
        {<br />
            </span>
          <span style="color: blue">var</span>
          <span style="color: black"> q
= </span>
          <span style="color: blue">from</span>
          <span style="color: black"> c </span>
          <span style="color: blue">in</span>
          <span style="color: black"> data.CustomerSet<br />
                    </span>
          <span style="color: blue">where</span>
          <span style="color: black"> c.City.Contains(city)<br />
                    </span>
          <span style="color: blue">select</span>
          <span style="color: black"> c;<br /><br />
            </span>
          <span style="color: blue">return</span>
          <span style="color: black"> q.ToList();<br />
        }<br /><br />
        </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black"> GetCustomer(</span>
          <span style="color: blue">string</span>
          <span style="color: black"> id)<br />
        {<br />
            </span>
          <span style="color: blue">return</span>
          <span style="color: black"> data.CustomerSet.SingleOrDefault(c
=&gt; c.CustomerID == id);<br />
        }<br /><br />
        </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">void</span>
          <span style="color: black"> UpdateCustomer(</span>
          <span style="color: #2b91af">Customer</span>
          <span style="color: black"> customer)<br />
        {<br />
            data.AttachTo(</span>
          <span style="color: #a31515">"CustomerSet"</span>
          <span style="color: black">,
customer);<br />
            data.UpdateObject(customer);<br />
            data.SaveChanges();<br />
        }<br />
    }<br />
}</span>
        </div>
        <p>
It implements three common methods for getting a list of customers for a specified
city, getting a specific customer by identity, and for updating a customer. Note that
we in the list query method (GetCustomersByCity) can use LINQ to query the data service,
and if you debug the code and check out the value of the query variable (q), you will
see the actual query (<a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">REST</a>)
URI generated. LINQ is also used in the second query method (GetCustomer), but here
in an alternative format (and with a lambda expression). Note also how simple the
code is to update a customer using the data service. I have intentionally left out
the other CRUD operations (insert and delete) as they should not be to hard to figure
out, and don't add much to the architectural discussion.
</p>
        <p>
With this in place, you see how easy it would be to move the data service to another
location (like the cloud), by simply changing the URI. Likewise, the switch between
online (to the server or cloud) and offline (to a local data service) would be as
easy. Unfortunately, as the ADO.NET Data Services is not yet available on Windows
Mobile (yet), we need to accomplish something similar in another way (which I will
show in a later part of this blog series).
</p>
        <p>
In the next part, I will implement the services tier.
</p>
        <img width="0" height="0" src="http://blog.wmdev.net/aggbug.ashx?id=7a099d05-e7d3-407b-a5f2-18a40c1a03c1" />
      </body>
      <title>KISS Architecture (part 6)</title>
      <guid isPermaLink="false">http://blog.wmdev.net/PermaLink,guid,7a099d05-e7d3-407b-a5f2-18a40c1a03c1.aspx</guid>
      <link>http://blog.wmdev.net/2008/11/11/KISSArchitecturePart6.aspx</link>
      <pubDate>Tue, 11 Nov 2008 21:59:15 GMT</pubDate>
      <description>&lt;p&gt;
In this part, I will start the implementation of the middle tier, and if you want
some background, please see the previous parts: 
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank"&gt;Part
1&lt;/a&gt; was a general introduction 
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank"&gt;Part
2&lt;/a&gt; outlined the architecture (tiers, etc) 
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank"&gt;Part
3&lt;/a&gt; showed the benefit of loosely coupled tiers (distribution, cloud, etc)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank"&gt;Part
4&lt;/a&gt; started the implementation by creating the entity data model (using ADO.NET
Entity Framework) 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://blog.wmdev.net/2008/11/07/KISSArchitecturePart5.aspx" target="_blank"&gt;Part
5&lt;/a&gt; published the entity data model as a data service (using ADO.NET Data Services)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The implemented architecture is published on CodePlex in a project called &lt;a href="http://www.codeplex.com/kiss" target="_blank"&gt;KISS
Architecture&lt;/a&gt;, and this means that you can access the full source code as well
as discuss it, come with suggested improvements, etc. As I walk you through the creation
of the architecture, I suggest you keep the source code handy to check out more details.
&lt;/p&gt;
&lt;p&gt;
With the data tier in place, I'm ready to create the middle tier, and I start by creating
a plain class library named Kiss.Domain which will hold my business domain classes.
I add a service reference to the data service created in the previous part (5) of
this blog series, and get the complete entity data model through the generated proxy
class. Then I create a plain class named OrderDomain.cs and add the following code
to it:
&lt;/p&gt;
&lt;div style="font-size: 10pt; font-family: monospace; background-color: white"&gt;&lt;span style="color: blue"&gt;namespace&lt;/span&gt;&lt;span style="color: black"&gt; Kiss.Domain&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;class&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderDomain&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderEntities&lt;/span&gt;&lt;span style="color: black"&gt; data
= &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;OrderEntities&lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;&lt;span style="color: black"&gt;(&lt;/span&gt;&lt;span style="color: #a31515"&gt;"http://localhost:1111/OrderDataService.svc"&lt;/span&gt;&lt;span style="color: black"&gt;));&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&lt;span style="color: black"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt;&amp;gt;
GetCustomersByCity(&lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt; city)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span style="color: black"&gt; q
= &lt;/span&gt;&lt;span style="color: blue"&gt;from&lt;/span&gt;&lt;span style="color: black"&gt; c &lt;/span&gt;&lt;span style="color: blue"&gt;in&lt;/span&gt;&lt;span style="color: black"&gt; data.CustomerSet&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;where&lt;/span&gt;&lt;span style="color: black"&gt; c.City.Contains(city)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;select&lt;/span&gt;&lt;span style="color: black"&gt; c;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt;&lt;span style="color: black"&gt; q.ToList();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt; GetCustomer(&lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&lt;span style="color: black"&gt; id)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt;&lt;span style="color: black"&gt; data.CustomerSet.SingleOrDefault(c
=&amp;gt; c.CustomerID == id);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span style="color: black"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt;&lt;span style="color: black"&gt; UpdateCustomer(&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;span style="color: black"&gt; customer)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data.AttachTo(&lt;/span&gt;&lt;span style="color: #a31515"&gt;"CustomerSet"&lt;/span&gt;&lt;span style="color: black"&gt;,
customer);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data.UpdateObject(customer);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data.SaveChanges();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
It implements three common methods for getting a list of customers for a specified
city, getting a specific customer by identity, and for updating a customer. Note that
we in the list query method (GetCustomersByCity) can use LINQ to query the data service,
and if you debug the code and check out the value of the query variable (q), you will
see the actual query (&lt;a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" target="_blank"&gt;REST&lt;/a&gt;)
URI generated. LINQ is also used in the second query method (GetCustomer), but here
in an alternative format (and with a lambda expression). Note also how simple the
code is to update a customer using the data service. I have intentionally left out
the other CRUD operations (insert and delete) as they should not be to hard to figure
out, and don't add much to the architectural discussion.
&lt;/p&gt;
&lt;p&gt;
With this in place, you see how easy it would be to move the data service to another
location (like the cloud), by simply changing the URI. Likewise, the switch between
online (to the server or cloud) and offline (to a local data service) would be as
easy. Unfortunately, as the ADO.NET Data Services is not yet available on Windows
Mobile (yet), we need to accomplish something similar in another way (which I will
show in a later part of this blog series).
&lt;/p&gt;
&lt;p&gt;
In the next part, I will implement the services tier.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.wmdev.net/aggbug.ashx?id=7a099d05-e7d3-407b-a5f2-18a40c1a03c1" /&gt;</description>
      <comments>http://blog.wmdev.net/CommentView,guid,7a099d05-e7d3-407b-a5f2-18a40c1a03c1.aspx</comments>
      <category>Architecture</category>
      <category>Chris</category>
      <category>Compact Framework</category>
      <category>Windows Mobile</category>
    </item>
    <item>
      <trackback:ping>http://blog.wmdev.net/Trackback.aspx?guid=e87f33c4-a449-4252-9ea0-be93ef9279ff</trackback:ping>
      <pingback:server>http://blog.wmdev.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.wmdev.net/PermaLink,guid,e87f33c4-a449-4252-9ea0-be93ef9279ff.aspx</pingback:target>
      <dc:creator>Chris</dc:creator>
      <wfw:comment>http://blog.wmdev.net/CommentView,guid,e87f33c4-a449-4252-9ea0-be93ef9279ff.aspx</wfw:comment>
      <wfw:commentRss>http://blog.wmdev.net/SyndicationService.asmx/GetEntryCommentsRss?guid=e87f33c4-a449-4252-9ea0-be93ef9279ff</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In this part, I will continue with the implementation of the data service using <a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" target="_blank">ADO.NET
Data Services</a>, and if you want some background, please see the previous parts: 
</p>
        <ul>
          <li>
            <a href="http://blog.wmdev.net/2008/10/10/KISSArchitecturePart1.aspx" target="_blank">Part
1</a> was a general introduction 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/17/KISSArchitecturePart2.aspx" target="_blank">Part
2</a> outlined the architecture (tiers, etc) 
</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/24/KISSArchitecturePart3.aspx" target="_blank">Part
3</a> showed the benefit of loosely coupled tiers (distribution, cloud, etc)</li>
          <li>
            <a href="http://blog.wmdev.net/2008/10/31/KISSArchitecturePart4.aspx" target="_blank">Part
4</a> started the implementation by creating the entity data model (using ADO.NET
Entity Framework)</li>
        </ul>
        <p>
The implemented architecture is published on CodePlex in a project called <a href="http://www.codeplex.com/kiss" target="_blank">KISS
Architecture</a>, and this means that you can access the full source code as well
as discuss it, come with suggested improvements, etc. As I walk you through the creation
of the architecture, I suggest you keep the source code handy to check out more details.
</p>
        <p>
Now it's time to publish the entity data model (OrderModel.edmx) that we created in
the last part (4) of this blog series, and that is done by adding an "ADO.NET Data
Service" named OrderDataService.svc to the data tier assembly, Kiss.Data. The modifications
necessary to this service class is trivial, and involves specifying the type of the
data model (object context, in my case OrderEntities) and making the complete model
available to anyone. The resulting code looks like this: 
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">namespace</span>
          <span style="color: black"> Kiss.Data<br />
{<br />
    </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">class</span>
          <span style="color: black">
          </span>
          <span style="color: #2b91af">OrderDataService</span>
          <span style="color: black"> : </span>
          <span style="color: #2b91af">DataService</span>
          <span style="color: black">&lt;</span>
          <span style="color: #2b91af">OrderEntities</span>
          <span style="color: black">&gt;<br />
    {<br />
        </span>
          <span style="color: blue">public</span>
          <span style="color: black">
          </span>
          <span style="color: blue">static</span>
          <span style="color: black">
          </span>
          <span style="color: blue">void</span>
          <span style="color: black"> InitializeService(</span>
          <span style="color: #2b91af">IDataServiceConfiguration</span>
          <span style="color: black"> config)<br />
        {<br />
            config.SetEntitySetAccessRule(</span>
          <span style="color: #a31515">"*"</span>
          <span style="color: black">, </span>
          <span style="color: #2b91af">EntitySetRights</span>
          <span style="color: black">.All);<br />
        }<br />
    }<br />
}</span>
        </div>
        <p>
Amazing as it sounds, the data tier is now ready to be used, and we can start querying
the database through the newly built data service. For example, if we go to http://localhost:1111/OrderDataService.svc/CustomerSet('ALFKI'),
we get the following result (somewhat simplified)... 
</p>
        <div style="font-size: 10pt; font-family: monospace; background-color: white">
          <span style="color: blue">&lt;?</span>
          <span style="color: #a31515">xml</span>
          <span style="color: blue">
          </span>
          <span style="color: red">version</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">1.0</span>
          <span style="color: black">"</span>
          <span style="color: blue">
          </span>
          <span style="color: red">encoding</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">utf-8</span>
          <span style="color: black">"</span>
          <span style="color: blue">
          </span>
          <span style="color: red">standalone</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">yes</span>
          <span style="color: black">"</span>
          <span style="color: blue"> ?&gt;<br />
&lt;</span>
          <span style="color: #a31515">entry</span>
          <span style="color: blue">
          </span>
          <span style="color: red">xmlns</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">http://www.w3.org/2005/Atom</span>
          <span style="color: black">"</span>
          <span style="color: blue">&gt;<br />
    &lt;</span>
          <span style="color: #a31515">id</span>
          <span style="color: blue">&gt;</span>
          <span style="color: black">http://localhost:1111/OrderDataService.svc/CustomerSet('ALFKI')</span>
          <span style="color: blue">&lt;/</span>
          <span style="color: #a31515">id</span>
          <span style="color: blue">&gt;<br />
    &lt;</span>
          <span style="color: #a31515">title</span>
          <span style="color: blue">
          </span>
          <span style="color: red">type</span>
          <span style="color: blue">=</span>
          <span style="color: black">"</span>
          <span style="color: blue">text</span>
          <span style="color: black">"</span>
          <span style="color: blue"> /&gt;<br />
    &lt;</span>
          <span style="color: #a31515">updated</span>
          <span style="color: blue">&gt;</span>
          <span style="color: black">2008-11-25T02:24:45Z</span>
          <span style="color: blue">&lt;/</span>
          <span style="color: #a31515">updated</span>
          <span style="color: blue">&gt;<br />
    &lt;</span>
          <span style="color: #a31515">author</span>
      