A New Mobile N-tier Architecture (part 8)

by Chris 31. October 2008 15:25

We will continue the more practical part of this series by showing how the new mobile architecture looks in code, and if you want some background, please see the previous parts:

  • Part 1 was a general introduction
  • Part 2 talked about the changes in the lower tiers (logic + data, LINQ2SQL)
  • Part 3 discussed the changes in communication (WCF)
  • Part 4 covered important stuff in the user interface (MVC)
  • Part 5 summarized the theory and outlined the new architecture
  • Part 6 started the walkthrough of the architecture code by looking at the business domain
  • Part 7 continued the code walkthrough with a look at the service (WCF)

The implemented architecture is published on CodePlex in a project called Windows Mobile Architecture Blueprint, 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, we suggest you keep the source code handy to check out more details.

We are now ready to consume the WCF service that we created in the previous post, and therefore we have added a new "Smart Device Project" and selected to create a Device application targeting Windows Mobile 6 Standard (a.k.a Smartphone) and the latest .NET Compact Framework (3.5).

As the tools support is not yet in place in Visual Studio, to consume a WCF service, you need to first download and install the Power Toys for .NET Compact Framework 3.5. The tool that you are after is named NetCFSvcUtil.exe, and you use it like this...

NetCFSvcUtil /language:cs http://localhost:5610/Service.svc

...and in the source code you can find a batch file (CreateServiceClient.bat) that will run this command (note that you may have to change the path to the NetCFSvcUtil utility depending on whether you're running on 32- or 64-bit). Note that the WCF service must be running when you run this command, and the out of running this batch file looks something like this...

netcfsvcutl

...with the result that two files are generated (Service.cs and CFClientBase.cs). As there files are already part of the source code, they will simply be replaced and the procedure with running the batch file is the next best thing to what you would expect from integrated support in Visual Studio.

Now, with these two files in place, we can start consuming the WCF service with code like this...

ServiceClient.EndpointAddress = new EndpointAddress("http://192.168.0.100:5610/Service.svc/basic");
ServiceClient service = new ServiceClient();
Category[] categories = service.GetCategories();

...and the first thing we do is to set an endpoint of the service that the mobile device can reach (in this case an IP address). Then the service proxy (ServiceClient) is instantiated and used to retrieve the list of categories. Note that the business entity (Category) that was declared back in the business domain (automatically generated by LINQ2SQL, and automatically serialized by WCF) is readily available on the mobile client (thanks to the proxy we generated above).

In future posts, we will cover more parts of the architecture blueprint implementation.

In some projects, we have used a very simple and straightforward implementation by our former fellow MVP, Alex Yakhnin (he's now employed by Microsoft), that he published as a small blog series last fall (see part 1 and part 2).

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Architecture | Chris | Compact Framework | Windows Mobile

A New Mobile N-tier Architecture (part 7)

by Chris 24. October 2008 15:19

I will continue the more practical part of this series by showing how the new mobile architecture looks in code, and if you want some background, please see the previous parts:

  • Part 1 was a general introduction
  • Part 2 talked about the changes in the lower tiers (logic + data, LINQ2SQL)
  • Part 3 discussed the changes in communication (WCF)
  • Part 4 covered important stuff in the user interface (MVC)
  • Part 5 summarized the theory and outlined the new architecture
  • Part 6 started the walkthrough of the architecture code by looking at the business domain

The implemented architecture is published on CodePlex in a project called Windows Mobile Architecture Blueprint, 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.

The next step in building the architecture is to create the service interface (i.e. the service), and therefore I have added a new "WCF Service Application" project called Blueprint.Facade to the solution that already included the business domain project (Blueprint.Domain). My first service will be publishing the very simple functionality that the business domain provides (the ability to retrieve all categories from the database). First, I have renamed the service interface to IService and the service implementation to Service (removed the "1" at the end on both), and here you need to make sure that all references are updated (four occurences in the web.config file and don't forget the one in Service.svc). The implementation of the service interface (IService.cs) can be done like this...

[ServiceContract]
public interface IService
{
    [
OperationContract]
   
Category[] GetCategories();
}

...and with a reference to the business domain project, the service implementation (Service.svc.cs) can be implemented with this code...

public class Service : IService
{
   
public Category[] GetCategories()
    {
       
using(NorthwindDataContext dc = new NorthwindDataContext())
           
return dc.GetCategories();
    }
}

...to complete the implementation of the service. When the project is built and run, the WCF is ready to be consumed, and therefore it's a good idea to remember the URL to the service as we will need it in the next post when we will look at how this service can be consumed from a mobile client application.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Architecture | Chris | Compact Framework | Windows Mobile

A New Mobile N-tier Architecture (part 6)

by Chris 17. October 2008 15:14

In this part, I will get more practical by showing how the first parts of this new mobile architecture looks in code, and if you want some background, please see the previous parts:

  • Part 1 was a general introduction
  • Part 2 talked about the changes in the lower tiers (logic + data, LINQ2SQL)
  • Part 3 discussed the changes in communication (WCF)
  • Part 4 covered important stuff in the user interface (MVC)
  • Part 5 summarized the theory and outlined the new architecture

The implemented architecture is published on CodePlex in a project called Windows Mobile Architecture Blueprint, 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.

nwdatacontext Ok, let's start building the architecture from the ground up!  To make things really simple (remember the KISS principle), I have started with a plain class library called Blueprint.Domain where I will keep my business domains. My first domain will be covering a small part of the classic Northwind database. I started by creating a LINQ2SQL data context called Northwind and dragged two of the Northwind tables into it (if you're like me, and don't have the Northwind database installed, you can get it here) as you can see on the right (note that I have removed the Picture field in the Category table to save bandwidth).

When that is done, a neat trick that will come in very handy is to select Properties on the design canvas of the data context and set Serialization Mode to Unidirectional. By doing this simple task, the code generated for the data context will include the necessary decorations (attributes) to make the data context ready to be published by WCF. If we look at a stripped version of the generated code (in Northwind.designer.cs)...

public partial class NorthwindDataContext : System.Data.Linq.DataContext
{
    [DataContract()]
    public partial class Category
    {
        [DataMember(Order=1)]
       
public int CategoryID
        {

...you will see that each class (entity) has a DataContract attribute, and each field (attribute) has a DataMember attribute. These attributes will be use by WCF when the entities are communicated.

In the code above, please also note that the generated class for the data context (NorthwindDataContext) is declared "partial" which perfectly matches our intention of implementing the business logic in a parallel class. By creating such a class (Northwind.cs), and putting in some simple code like this...

public partial class NorthwindDataContext
{
   
public Category[] GetCategories()
    {
        return Categories.ToArray();
    }
}

...we have implemented the first (however sparse) service into our first business domain, and the functionality is that it returns the list of all categories. Note that behind the scenes, LINQ comes into play, and to show a more obvious example, we can do this instead...

var q = from c in Categories select c;
return q.ToArray();

...and this will create the same result as the code above. Worth mentioning already is the fact that even if the Category entity holds a relation to all products in that category, those child entities are not loaded by default. This is a good thing as you probably don't want to load all products with all categories, but when you want to load related entities, you can do this by explicitly call...

var q = from c in Categories select c;
Category[] categories = q.ToArray();
categories[0].Products.Load();

...to load all the products for the first category. There are some other options for controlling how related entities are loaded, but it's out-of-scope for this blog post. In future posts in this series, I will cover more parts of the architecture blueprint implementation.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

Architecture | Chris | Compact Framework | Windows Mobile

A New Mobile N-tier Architecture (part 5)

by Chris 10. October 2008 15:08

In the previous parts of this series, I have discussed the changes that affect the architecture design. It's now time to conclude what this all leads up to architecture-wise, and I propose that the architecture should look something like this:

newarch Most of the fundamental parts (three tiers, common services, etc), and even many of the more specific (service interfaces/agents, data access, business entities, etc) are exactly the same as in the Application Architecture for .NET that I've mentioned before in this series of blog posts.

Note that a significant change is the merge of the lower part of the middle (business services) tier and the bottom (data services) tier. As I've outlined before, this is where the business logic and its data comes together in a nice mix called the business domains. With technologies like LINQ to SQL and WCF, most (if not all) of the data services tier are actually created using code generation. This means that any changes made to the different sources (data sources and services) can be captured by regenerating the code. The tools support is actually prepared for this kind of evolution in the integration between systems, and a simple "Update Service Reference" menu selection is all that is needed.

Another similar change is the clear separation between the service interfaces and the business domains. Where the business domains have a clear responsibility to handle the logic and data for a certain business domain, the service interfaces (designed according to the facade pattern) has a different set of responsibilities. The primary distinction of a service interface is that it probably fulfills a specific part of the system's functionality (use case). Also, as they are the "first line of defense", they should handle things like basic security (authentication, authorization, encryption, etc) as well as secure coding techniques (parameter checking, prevent things like SQL/code injection, etc), compression, transactions, logging, etc. A consequence of the separation with the business domains is that the service can be placed in a separate assembly. This is a good thing because a service interface probably uses a number of business domains that may (or may not) exist in numerous assemblies.

Finally, the user interface parts have been renamed to align with the use of the MVC pattern, and as already mentioned in previous posts, this will allow for better testing and the use of the same user interface logic for different clients (thin Web interfaces built with ASP.NET, rich interfaces built with WinForms, WPF or Silverlight, and of course mobile clients built with .NET CF and soon Silverlight, etc).

In upcoming posts, I will continue with more of my thoughts on the changes in mobile architecture, and of course also some code samples to show the theory in practice. Stay tuned!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

Architecture | Chris | Compact Framework | Windows Mobile

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen