Monday, March 29, 2010

Making use of the Command Pattern on Windows Mobile/phone

The Command pattern is a great pattern for abstracting business processes from your implementation code. This pattern is becoming very popular throughout different types of systems. Whether this is MVC thin client, fat clients such as MVP, MVVM.

The Command pattern works really nicely in combination with a IoC container and DI framework that I have talked about before on this blog.

Often it is desirable for a command to take a context or state. A command should only have one method and should only have one role (single responsibility). The interface for a command could look like so:
public interface ICommand<T>
{
void Execute(T context);
}
So our context here is a generic and is defined when the command is registered with the container.

So imagine we have a CRM system that when a customer is registered, we want to send that customer an email to confirm he/she has been setup correctly. You might have a domain model in this case that raises an event that is caught on the middle tier. When this occurs, instead of baking that code into the presenter/controller/business class, you abstract it out into a command. This not only makes your system more readable/maintainable but makes it easier to test too.

So in this case you could have a context class that contains the state such as the Customer domain object like so:
public class EmailCustomerConfirmationContext
{

public EmailCustomerConfirmationContext(Customer customer)
{
Customer = customer;
}

public Customer Customer{get; private set;}
}
Our command might look something like the following:
public class EmailCustomerConfirmationCommand : ICommand<EmailCustomerConfirmationContext>
{
private IEmailAdapter _emailAdapter;

public EmailCustomerConfirmationCommand(IEmailAdapter emailAdapter)
{
//inject dependencies here.
_emailAdapter = emailAdapter;
}

public void Execute(EmailCustomerConfirmationContext context)
{
_emailAdapter.Send(context.Customer);
}
}
Registering the command with the container (Compact Container - see previous posts on using this container) would look something like the following:
container.AddComponent<ICommand<EmailCustomerConfirmationContext>, EmailCustomerConfirmationCommand>();


Very clean approach. Of course the more dependencies you add to the command, the more complex it will become which means harder to test. So sometimes commands can become over complex. Bear this in mind when adopting this pattern.

Executing the command could look something like the following(assuming you are using the service locator):
var command = ServiceLocator.Current.GetInstance<ICommand<TContext>>();
if (!command.IsNull())
{
command.Execute(context);
}
In terms of handling errors etc, this could be handled via events using some sort of event aggregator pattern or the context itself to pass back data so the middle tier can act accordingly.

Sunday, March 28, 2010

TFS 2010 - the new build definition window

Jason Prickett has recently written a good article on the new Build definition window in VS 2010 - it's now dockable! It also has a new CI type named Gated-checkin. So developers need to have a green CI before they can checkin. Cool. I have been checking this new Gated-checkin feature out in the RC version recently, and I'm not sure it is working correctly. But will write back soon on my findings.

See here:
http://blogs.msdn.com/jpricket/archive/2010/01/19/tfs-2010-the-new-build-definition-window.aspx

Cool new feature with VS2010 and TFS 2010

I recently installed TFS 2010 RC x64 and only now got round to playing with it. I have to say once I got over the 'gremlins' during the installation everything else is pretty slick. There are many features I like and many that are going to make our lifes better.

One of the really horible things I hated about VS 2008 and TFS 2008 was after a partially succeeded build, opening up the build output in Build Explorer would only tell you something went wrong. In other words, the build partially suceeded. What? what does this mean? we know from experience this generally means a unit test had failed, but which one, and how do I fix it?



The only way to know which unit test had failed would be to troll through the build log - very painful when you have a large build script spanning multiple projects and 1000's of unit tests. I used to search for "FAIL". But some developers name their unit tests with the word "FAIL" in it so this doesn't help matters as it takes forever to find the real error.


So I created a really simple test project and added a test method that throw an exception, after I checked in under a CI build definition, I got the following:






Now, how cool is that! Weldone Microsoft. I think this will make many developers very happy. Look at the options we get at the top, [Open Drop Folder], [Retain Indefinately] etc it just makes our lifes easier. But the best bit, the reason for posting this blog entry, If I click "View Test Results" I get the following:







I'm impressed, this will make us so much more productive, a reason alone to upgrade. Excellent stuff!

Wednesday, March 24, 2010

MVVM - Windows phone 7 series pattern of choice

This post is really for my benefit (although it might help others). Here is a good article on the MVVM (Model-View-ViewModel) pattern that is showing a lot of interest in the WPF communities.

It is very much a different way of thinking when designing UI architecture although very similar to Fowlers relatively new Presentation Model pattern.

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Implementing an IoC container in Silverlight on Windows phone 7 series

I wrote a while ago about implementing a Service Locator for the CompactContainer on the Compact Framework here: http://www.simonrhart.com/2010/02/implementing-commonservicelocator-on.html

I mentioned in that post that I was using the CompactContainer for the CF freely available here: http://code.google.com/p/compactcontainer/ But now as I'm getting into Silverlight as it's the technology of choice when building applications for the Windows phone 7 series platform (as well as XNA), I needed an IoC container to solve the same problems on Silverlight as they do on the Compact Framework.

So I tried to port the container along with the ServiceLocator as blogged about before. I recieved errors in the ComponentCollection class after I tried to compile under Silverlight. Mainly because the following predicate methods such as:
  1. List.FindAll
  2. List.Find

Are not supported in Silverlight 3 as the documentation suggests. However, they are in XNA. As the generic List class belongs to mscorlib.dll (System.Collections.Generic) I noticed that mscorlib is shared for both XNA and Silverlight applications on WP7. I'm trying to find out why this is, or if there is a way of making those methods work in Silverlight.

Anyway it's not the end of the world that those methods are not in Silverlight, all I had to do was replace the following methods in the ComponentCollection class:


public List<ComponentInfo> GetComponentInfoListFor(Type serviceType)
{
return _list.FindAll(ci => ci.ServiceType == serviceType);
}

public ComponentInfo FindForService(Type serviceType)
{
return _list.Find(ci => ci.ServiceType.Equals(serviceType));
}

public ComponentInfo FindForClass(Type classType)
{
return _list.Find(ci => ci.ClassType.Equals(classType));
}

public ComponentInfo FindKey(string key)
{
return _list.Find(ci => ci.Key.Equals(key));
}
With the following code:
public List GetComponentInfoListFor(Type serviceType)
{
List<ComponentInfo> results = new List<ComponentInfo>();

foreach(ComponentInfo component in _list)
{
if (component.ServiceType == serviceType)
results.Add(component);
}
return results;
}

public ComponentInfo FindForService(Type serviceType)
{
ComponentInfo result = null;
foreach(ComponentInfo component in _list)
{
if (component.ServiceType == serviceType)
{
result = component;
break;
}
}
return result;
}

public ComponentInfo FindForClass(Type classType)
{
ComponentInfo result = null;
foreach (ComponentInfo component in _list)
{
if (component.ClassType.Equals(classType))
{
result = component;
break;
}
}
return result;
}

public ComponentInfo FindKey(string key)
{
ComponentInfo result = null;
foreach (ComponentInfo component in _list)
{
if (component.Key.Equals(key))
{
result = component;
break;
}
}
return result;
}
That was all I had to change everything else just compiled and worked. Full dependency injection worked as per on the Compact Framework. It seems moving to Silverlight is going to be less painful than I originally thought!

Tuesday, March 16, 2010

Windows Phone 7 Series emulator is running slow

When I first downloaded the new Windows Phone 7 Series developer tools CTP package, I found the new emulator to be very slow. This is the opposite of what I was expecting as the new emulator is built for x86, runs within a VM and supports hardware GPU host acceleration (so long as you have a gx capable of DirectX10 and at least support for DDI10) - which my laptop does.

To determin whether you have DirectX10 support and at least DDI10, run DxDiag.exe from the command-prompt and inspect the Display tab.

So what next, ok so I learned that in order for the emulator to make use of the GPU host from the VM I needed to enable HW virtualization. So how do I know if I have this enabled? this is enabled at BIOS level and normally disabled by OEMs by default. You can run the Microsoft Hardware-Assisted Virtualization Tool here: http://go.microsoft.com/fwlink/?LinkId=163321



So after running this tool I got the above. So this confirms that I didn't have hw virtualization enabled. After enabling it in the BIOS my Windows Phone 7 Series emulator is now running much faster!

More details about this process here (not WP7 specific): http://www.microsoft.com/windows/virtual-pc/support/configure-bios.aspx



New Windows phone series 7 forum

Ask your new Windows phone 7 series questions here:
http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/

Monday, March 15, 2010

Friday, March 12, 2010

Pre-order VS 2010 discount

Microsoft Visual Studio 2010 Professional will launch on April 12 but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of £484.99.

If you use a previous version of Visual Studio or any other development tool then you are eligible for this upgrade. Along with all the great new features in Visual Studio 2010 (see www.microsoft.com/visualstudio) Visual Studio 2010 Professional includes a 12-month MSDN Essentials subscription which gives you access to core Microsoft platforms: Windows 7 Ultimate, Windows Server 2008 R2 Enterprise, and Microsoft SQL Server 2008 R2 Datacenter.

So visit http://www.microsoft.com/visualstudio/en-gb/pre-order-visual-studio-2010 to check out all the new features and sign up for this great offer.