Category Archives: Silverlight

Runtime Windows Phone App

My latest app, Runtime for WP7 is here!

RunTime is a FREE stopwatch app for Windows Phone 7.
The stopwatch continues to run in the background even if you lock the screen or leave the app.
The app allows saving laps.
The app shows both the main stopwatch timer as well as the current lap timer.

You can download the app by scanning the following QR code with Bing Vision (Press the Search button on your WP, press “Vision” then point the camera at the scree and follow the link):

Otherwise you can download it by visit the following link:

http://www.windowsphone.com/en-us/apps/e9e7b11c-242e-4d49-b4a9-c828b40f8d2d

1617e241-8731-4adc-aad0-285d534baa2c

I hope you like the app! Enjoy

Testing the Entity Framework 4.0 RIA Services Domain Service

Recently I’ve been working on refactoring and testing a Silverlight project that uses the EntityFramework.

Making all the changes of the auto-generated code in order to test it proved to be a harder task that I initially imagined.

This article summarises my findings and for getting the system ready for testing.

First of all, here is a list of links that helped me:

Testing out the Entity Framework WCF RIA Services Domain Service

WCF RIA Services Part 8 – Testing and Debugging

Testability and Entity Framework 4.0

However, none of these resources fully describe a working solution.

Here is how I finally got it working:

  • Step 1

Create an IUnitOfWork interface that will abstract the ObjectContext object used by your domain Service
Create additional operations the ObjectContext exposes as extension methods (e.g. AttachAsModified<TEntity>)

  • Step 2

Create an InMemoryUnitOfWork inheriting from IUnitOfWork (used for testing)
Create an InMemoryIObjectSet<T> inheriting from IObjectSet<T> (used for testing)

  • Step 3

Create a testing base class EFTestBaseClass for putting all the things together
Create a particular TestHelperClass for testing a particular test

There are additional important tweeks I will highlight in bold during the example.
Example:
Consider the following table in the entity container called NewTechCodeTestingEntities
An automatically generated Domain Service for manipulating it will look something like:

public class DomainService1 : LinqToEntitiesDomainService<NewTechCodeTestingEntities>
 {
 ...
 }

All the methods we are interested in testing will be hosted in this class. But instantiating it for testing will result in all operations being executed against the database. In order to avoid this situation we will have to somehow decouple this class form the EF.

(Step1)

First of we have to create the previously mentioned IUnitOfWork:

public interface IUnitOfWork
 {
 IObjectSet<Book> Books { get; }

ObjectStateManager ObjectStateManager { get; }
 int SaveChanges();
 }

This class abstracts the NewTechCodeTestingEntities.

This way we can create a special constructor for the Domain Service that we can use during testing:

public class DomainService1 : LinqToEntitiesDomainService<NewTechCodeTestingEntities>
 {
 new private readonly IUnitOfWork ObjectContext;

public DomainService1()
 {
 this.ObjectContext = base.ObjectContext;
 }

public DomainService1(IUnitOfWork unitOfWork)
 {
 this.ObjectContext = unitOfWork;
 }

...
 }

It’s important to notice how we created a new ObjectContext filed by using the new operator which either defaults to the base class ObjectContext (used for the production code when you call the parameterless contructor) or sets it to a specified one we create during testing.

An important thing to notice is the use of IObjectSet<Book> instead of ObjectSet<Book>.

The auto-generated file exposes the object in ObjectSet<T> collections.

In order to use the current approach you will have to manually change All the ObjectSet<T> declarations to IObjectSet<T> (Find and replace should do the trick in seconds)

The second thing you will have to do is make the NewTechCodeTestingEntities implement the IUnitOfWork.

The code will therefore look something like this:

public partial class NewTechCodeTestingEntities : ObjectContext, IUnitOfWork
 {
 ...
 }

Notice that these two steps (changing the collection declaration and inheriting from the IUnitOfWork) will have to be performed each time you update your EF model. I find this approach acceptable mostly because it is the only way I got the whole system to work.

Creating the extension methods are needed for keeping the auto generated code working. The only method I needed was AttachAsModified<T>:

public static class Extension
 {
 public static void AttachAsModified<TEntity&gt;(this IObjectSet<TEntity> objectSet, TEntity current, object original) where TEntity : class
 {
 throw new NotImplementedException();
 }
 }

(Step 2)

The following two classes were created for keeping the testing data:

public class InMemoryUnitOfWork : IUnitOfWork
 {
 public IObjectSet<Book> Books
 {
 get;
 set;
 }

public ObjectStateManager ObjectStateManager
 {
 get;
 set;
 }

public int SaveChanges()
 {
 throw new NotImplementedException();
 }
 }

and

public class InMemoryIObjectSet<T> : IObjectSet<T> where T : class
 {
 public InMemoryIObjectSet()
 : this(Enumerable.Empty<T>())
 {
 }

public InMemoryIObjectSet(IEnumerable<T> entities)
 {
 _set = new HashSet<T>();
 foreach (var entity in entities)
 {
 _set.Add(entity);
 }
 _queryableSet = _set.AsQueryable();
 }
 public void AddObject(T entity)
 {
 _set.Add(entity);
 }
 public void Attach(T entity)
 {
 _set.Add(entity);
 }
 public void DeleteObject(T entity)
 {
 _set.Remove(entity);
 }
 public void Detach(T entity)
 {
 _set.Remove(entity);
 }
 public Type ElementType
 {
 get { return _queryableSet.ElementType; }
 }
 public Expression Expression
 {
 get { return _queryableSet.Expression; }
 }

public IQueryProvider Provider
 {
 get { return _queryableSet.Provider; }
 }

public IEnumerator<T> GetEnumerator()
 {
 return _set.GetEnumerator();
 }

IEnumerator IEnumerable.GetEnumerator()
 {
 return GetEnumerator();
 }

readonly HashSet<T> _set;

readonly IQueryable<T> _queryableSet;
 }

If you are interested in finding more about these classes check the links at the beginning of the article.

(Step 3)

public class EFTestBaseClass
 {
 public EFTestBaseClass()
 {
 this.unitOfWork = new InMemoryUnitOfWork();
 this.controller = new DomainService1(unitOfWork);
 }

protected InMemoryUnitOfWork unitOfWork;
 protected DomainService1 controller;
 }

and

public class BooksTestHelper : EFTestBaseClass
 {
 protected IList<Book> books;

protected InMemoryIObjectSet<Book> repositoryBooks;

public BooksTestHelper()
 {
 this.PopulateCatalogueMaster();
 }

public void PopulateCatalogueMaster()
 {
 this.books = CreateCatalogueMasters().ToList();
 this.repositoryBooks = new InMemoryIObjectSet<Book&gt;(this.books);
 this.unitOfWork.Books = repositoryBooks;
 }

public static IEnumerable<Book> CreateCatalogueMasters()
 {
 yield return new Book()
 {
 Id = 1,
 Title = "Madame Bovary",
 Author = "Gustave Flauber"
 };
 yield return new Book()
 {
 Id = 2,
 Title = "The Lady of the Camellias",
 Author = "Alexandre Dumas, fils"
 };
 yield return new Book()
 {
 Id = 3,
 Title = "Notre Dame de Paris",
 Author = "Victor Hugo"
 };
 }
 }

allows us to create the following test:

 

[TestClass]
 public class BooksTest : BooksTestHelper
 {
 [TestMethod]
 public void GetBooks()
 {
 var books = controller.GetBooks();
 // insert test here
 }
 }

The tweaks that helped me get the system working were the observations highlighted in green which on top of the knowledge you get by reading the introduction articles should help you in testing your application.

Puzzler Windows Phone Game

My second WP7 app, Puzzler  is out.

Puzzler is an entertaining puzzle game with configurable levels and different game modes.

The trial offers all the functions of the game so you can experience the gameplay in all game modes and difficulty levels. It contains two puzzle image packs of seven images each.

The paid version contains five puzzle image packs, the ability to use pictures from the phone’s media library and also pictures captured with your camera. The paid version is ad free, so you have more space for your puzzle pieces and no distractions. Transform your best pictures into puzzles and start have fun arranging their pieces!

The application can be downloaded from the Zune marketplace by following this link:

Here are some screenshots:

This slideshow requires JavaScript.

Here are two videos about the game:

Presentation

Managing personal puzzles

Enjoy!

Windows Phone Mango – Puzzle Match

After learning about the new network capabilities in Windows Phone Mango, I decided to create a hopefully fun and entertaining game for Windows Phone platform.

The app is going to be a turn-based Puzzle game, called Puzzle Match, that can be  played in either singleplayer mode or multiplayer, over a WI-FI network.

The current requirements I defined for the game are:

  • Easy to create a new game, with as little user intervention as possible
  • Easy to attend a live game
  • The ability to use personal photos as puzzles, either from the gallery, from the camera or from an URL
In each turn, the user will have to find a match as fast as possible in order to win points. The faster he finds a match the more points he gets.
The game will have multiple difficulty levels depending on the puzzle pieces.
The game will use either straight rectangle pieces or curved pieces created using Bezier curves.
I hope you like the prototype, I’m looking forward to implementing it.

Building Applications for Windows Phone Mango Jump Start, Day I

First of all, I would like to present a list of resources to get you started developing WP7 Mango apps.

  • Visual Studio Service Pack 1 (prerequisite for installing the other tools)

http://www.microsoft.com/download/en/details.aspx?id=23691

  • WP7 7.1 SDK

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27153

  • August 2011 Silverlight SDK – some amazing new controls are available, be sure to check the available samples

http://silverlight.codeplex.com/releases/view/71550

One of the controls that really got my attentions and I think it’s gonna be useful in many scenarios is the HubTile – an dynamic icon similar to the ones on the start screen which can display an image and additional text information which you can change.

Here are some images:


This slideshow requires JavaScript.

The emulator got a bunch of new features like the ability to change the location,  accelerometer parameters and the ability to take snapshots:

This slideshow requires JavaScript.

These functions can be accessed the double-arrow pointing right  icon  (last on on the bottom) on the side menu
An here is a list of things I’ve learned today about the new WP7 Mango platform:
  • TextBox now has a InputScope property which dictates the keyboard layout once the control is activated.
  • NavigationService.RemoveBackEntry() allows to remove pages from the navigation stack.
  • Fast App Switch – on application activated handler you can not check if the handle with app switch or complete reactivation
if (!e.IsApplicationInstancePreserved)
{// tombstone}
else
{ // switch}</span>
  • On the project properties, on Debug page, you can choose if the emulator should switch between apps or close them when debugging
  • Phone contacts access by using the “Contacts” class which offers handy methods for contact retrieval, look-up etc.
  • Real time camera capture without leaving the app context
  • Further integration with Bing services
Good luck with these new tools and features, and remember (quoting Rob Miles):
“Doing stuff is the best way to be successful”