Posts Tagged ‘silverlight enabled WCF Service’

h1

Silverlight 2.0 MVP Architecture

November 6, 2008

I have recently kicked off a new project at work and decided to use Silverlight 2.0 as the technology of choice. Why? Because web 2.0 is dead and RIA is the way of the future. I got the structure of Tim Ross’s blog, he wrote an excellent in depth article with source code. I thought I would also write an article about it but by introducing some of the extra projects that I found necessary and some of the design decisions involved. The rest of this article will give you an idea of how to structure a Silverlight 2.0 application in visual studio 2008 that uses Silverlight WCF enabled services over BasicHttpBinding (the only one currently supported) to communicate with the web server. The separation of concerns pattern that I will be using will be the MVP pattern. I chose this pattern over the MVVM pattern because of two reasons. 1) It has been officially documented on the MSDN site and MVVM at this time of writing hasn’t

2) It has been around for longer than MVVM which was invented by a team of Microsoft guys during the writing of blend and therefore the chances of people knowing it will be higher. Because I’m using an emerging technology, my concerns are that people will already have a big learning curve to conquer and I don’t want to introduce a new(ish) pattern on top of it.

Here is a snip of the solution structure

Client and Server

To start off, we create two solution folders, one named Client and one named Server.

Client

This folder contains 3 projects

SilverlightMvp.View <- The actual SilverlightApplication

SilverlightMvp.Core <- Silverlight Class Library

SilverlightMvp.Core.UnitTests <- Unit tests for the presenters.

The unit test project won’t actually reside on the client machine but it makes more sense to group it in the client folder as it is purely testing the presenters.

Server

This folder contains 5 projects.

Database <- The database

DataAccess <- the CRUD library

Common <- Entities folder (The Model) and ExtensionMethods

SilverlightApplication.Web <- Web Application that hosts the Silverlight application. This has a folder in it called WCFServices which contains the Silverlight enabled WCF services. To add a Silverlight enabled WCF service, select the WebApplication that is hosting your Silverlight project. Right click -> Add -> New File.

This is where you will find the WCF template:

Business <- Business logic that the WCF Services will expose to the Silverlight application (SilverlightMvp.Core)

Tests <- The reason why this project is called Tests is because it will be hosting both my Unit tests and Integration tests. I have created two folders in there named Unit Tests and Integration Tests. Using the dependency injection pattern we ensure mocking the DataAcess Layer will be easy. For my requirements I will not be testing the WCF Services inside the SilverlightApplicaiton.Web projects because they will just expose Business Library methods and have no logic in them at all.

Notes

  • The Client and Server folder structure help enforce the developer to be constantly aware of the fact that the Silverlight application is actually a tiny subset of the .net CLR and therefor is missing a lot of functionality.
  • Because you are using a separate CLR that’s on the clients machine, you cannot point WCF to your existing Model. They must be dynamically generated.
  • Make sure you do all your data retrieval on the web server and pass it down in one hit rather than putting loops in your client service proxy’s retrieving data multiple times and therefore opening pooled database connections. As a general rule of thumb, loops in the presenter or client service making single calls to the database can usually be avoided and can speed things up

Conclusion

Overall I’m happy with the final design of the solution and I’m convinced that it’s the simplest solution that has the right balance of complexity to provide functionality, separation of concerns and maintainability but at the same time, not overly architected to cause Shunting friction

If I wasn’t so busy, I would look into creating a Guidence Automation package for this structure.

I would be very interested to hear comments on the Tests and Unit Test structure and would also love to hear about any alternatives you may have come up with.