Wednesday, May 28, 2008

blueprintuiThis is the final post in this series, and for a complete reference, here are 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)
  • Part 8 covered the consumption of the service with .NET CF
  • Part 9 showed the implementation of the user interface (MVC)

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.

On the upper right you see the UX of the sample client included in the architecture blueprint, and the functionality is that the combo box is filled with the categories when the application starts. Then, when a category is selected, the product names are shown in the text box below.

Even if this series is complete, we 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!

posted on Wednesday, May 28, 2008 6:31:06 AM UTC  by Chris  #    Comments [0]
 Monday, May 26, 2008

It was not easy trying to spot a Windows Mobile-phone on a public spot a couple of years ago. Not so anymore. When I'm out travelling I see Windows Mobile-phones more and more often, both among business users and consumers. The latest issue of the magazine "M3 Digital World" includes a test of eight GPS enabled mobile phones. Five of the eight phones are Windows Mobile-phones.

The reviewer gives the E-TENs Glofiish X800 a thumbs up! Check out engagdet's review of the unit here…

We'll see more and more Windows Mobile-phones out on the streets.


Image: engadget.com

posted on Monday, May 26, 2008 8:14:47 AM UTC  by Andy  #    Comments [0]
 Friday, May 23, 2008

WeI 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)
  • Part 8 covered the consumption of the service with .NET CF

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.

It's time to look at how we can implement the MVC pattern in the Windows Mobile client project that we created in the previous part of this series. As we've touched on before in this series, we have built this part of the architecture on a very simple and straightforward implementation by Alex Yakhnin, that he published as a small blog series last fall (see part 1 and part 2).

As Alex, I started with the core interfaces...

interface IView
{
   
void Show();
   
void Hide();
   
void Close();
   
string Text { get; set; }
}
interface IController
{
   
IView View { get; set; }
}

...and then the interface for the main view (form)...

interface IMainView : IView
{
   
Category SelectedCategory { get; }

   
void SetCategories(Category[] categories);
   
void SetProducts(string products);

   
event EventHandler Done;
   
event EventHandler CategorySelected;
}

...which already tells us what the main form can do. It allows us to set a number of categories (to choose from, SetCategories) and it will notify when a category is selected (CategorySelected). It will also make that selected category available through a public property (SelectedCategory) and allow us to set the products for a (the selected) category (SetProducts). Finally, it will notify when the user is done with it (Done).

The implementation of the controller for the main form looks like this...

class MainController : IController
{
   
private IMainView view;
   
private ServiceClient service;

   
public MainController(IMainView view)
    {
        View = view;

       
ServiceClient.EndpointAddress = new EndpointAddress("http://192.168.0.100:5610/Service.svc/basic");
        service =
new ServiceClient();

       
Category[] categories = service.GetCategories();
        view.SetCategories(categories);
    }

   
private void attachView(IMainView view)
    {
       
this.view = view;
        view.CategorySelected +=
new EventHandler(view_CategorySelected);
        view.Done +=
new EventHandler(view_Done);
    }

   
void view_CategorySelected(object sender, EventArgs e)
    {
        Category category = view.SelectedCategory;
       
string s = string.Empty;
       
foreach(Product product in category.Products)
            s += product.ProductName +
"\r\n";
        view.SetProducts(s);
    }

   
void view_Done(object sender, EventArgs e)
    {
        Application.Exit();
    }

    #region IController Members
   
public IView View
    {
       
get { return view; }
       
set { attachView(value as IMainView); }
    }
    #endregion
}

..and upon creation, the controller saves its view, and set up the event handlers to capture event from the view. Then it make the first call to the service to get the category list which is sent to the view. When a category is selected, a string is created (I know, in a real solution it would be a StringBuilder, but this code is simpler to read) and passed to the view.

The implementation of the main view (form) looks like this...

public partial class MainForm : Form, IMainView
{
    public MainForm()
    {
        InitializeComponent();
    }

    public Category SelectedCategory
    {
       
get { return categoryComboBox.SelectedItem as Category; }
    }

    public void SetCategories(Category[] categories)
    {
       
foreach(Category category in categories)
            categoryComboBox.Items.Add(category);
    }

   
public void SetProducts(string products)
    {
        productsTextBox.Text = products;
    }

    public event EventHandler CategorySelected;
   
private void categoryComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
       
if(CategorySelected != null)
            CategorySelected(
this, null);
    }

   
public event EventHandler Done;
   
private void doneMenuItem_Click(object sender, EventArgs e)
    {
       
if(Done != null)
            Done(
this, null);
    }
}

...and here we see that the categories are loaded in a combo box which is used to determine the currently selected category, and also to raise the event when a category is selected. The products string is simply put into a text box, and a menu option is used to offer the user to exit.

To manage (can cache) the various controllers and views, we use the following singleton class...

class ApplicationManager
{
   
public static readonly ApplicationManager Instance = new ApplicationManager();
   
private ApplicationManager() { }

   
private Dictionary<string, IController> controllersCache;

   
public MainForm GetMainForm()
    {
       
if(!controllersCache.ContainsKey("Main"))
            controllersCache.Add(
"Main", new MainController(new MainForm()));
       
return controllersCache["Main"].View as MainForm;
    }
}

...and the application is kicked off like this...

[MTAThread]
static void Main()
{
   
Application.Run(ApplicationManager.Instance.GetMainForm());
}

...which concludes the walkthrough of the basic architectore! In the next post, we will summarize this series of blog posts on the architecture blueprint implementation.

posted on Friday, May 23, 2008 10:28:58 AM UTC  by Chris  #    Comments [0]
 Thursday, May 22, 2008

This article came in very handy in our project!

"Creating controls for .NET Compact Framework–based applications—which include transparency, gradients, and three-dimensional glass-like appearance—is well within reach today, using existing Microsoft Windows Mobile development tools. This article will demonstrate how to achieve transparency, gradients, and glass effects by extending existing .NET Compact Framework controls and leveraging some powerful native graphics features available in the Windows Mobile operating system."

Read it here...

posted on Thursday, May 22, 2008 7:27:07 AM UTC  by Andy  #    Comments [0]
 Wednesday, May 21, 2008

My latest column was published in Computer Sweden today. You can read it here…

In the column I write about a field trip with some biologists. We had designed a Windows Mobile-application that they used when stock taking meadows and pasture lands. During the field trip I analyzed how the solution worked in reality… and I had the privilige of learning a lot about the landscape.

The most inspiring thing was when one of the biologists "read" the landscape and could foretell what butterfly we would likely to find at a particular place. He was right. See my pictures from the event below!

When I look into the IT-landscape today I see mobile device management (Sybase OneBridge, Microsoft Mobile Device Manager and The Institution), Web 2.0 (Sharepoint, Confluence, Incentive from Mindroute), and development tools that melt together with designer tools (Flash, Silverlight).

But the most beautiful creature out there? It's the butterfly!

 

 

 

posted on Wednesday, May 21, 2008 10:04:50 AM UTC  by Andy  #    Comments [0]
 Friday, May 16, 2008

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).

posted on Friday, May 16, 2008 10:28:31 AM UTC  by Chris  #    Comments [0]
 Friday, May 09, 2008

WeI 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, we 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 we have added a new "WCF Service Application" project called Blueprint.Facade to the solution that already included the business domain project (Blueprint.Domain). Our first service will be publishing the very simple functionality that the business domain provides (the ability to retrieve all categories from the database). First, we 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.

posted on Friday, May 09, 2008 10:27:52 AM UTC  by Andy  #    Comments [0]
 Wednesday, May 07, 2008

I am migrating a .NET CF 2-application to .NET CF 3.5. We haven't come across any breaking changes in our project. In fact, we've just seen benefits from the upgrade (ie performance). However, if you are about to do the same thing, don't miss the article: .NET Compact Framework 3.5 Run-Time Breaking Changes...

posted on Wednesday, May 07, 2008 7:27:28 AM UTC  by Andy  #    Comments [0]
 Friday, May 02, 2008

In this part, we 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, we 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), we have started with a plain class library called Blueprint.Domain where we 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 we 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, we will cover more parts of the architecture blueprint implementation.

posted on Friday, May 02, 2008 10:23:32 AM UTC  by Andy  #    Comments [0]