MICHELVANDERVLUGT.NET RSS 2.0
 Friday, August 20, 2010

image

In addition to my previous post Windows Azure Table Storage in the development environment I’ve created a demo application in which responsibilities are distributed over classes in a way that hides the implementation details of Azure’s table storage.

The application is pretty simple, a one page ASP.NET app in which you can add/edit movies. The movies are displayed on the same page in a GridView.

Movie model
The model has changed a bit. The RowKey is now filled with a GUID (instead of the title) to uniquely identify a movie in the table. The title now has a dedicated property. The PartitionKey remains the movie category as this helps Azure to partition the data over multiple servers if the load gets too heavy.

StorageContext

The StorageContext is the actual entry point to the data. It inherits from TableServiceContext and provides a property Movies that returns an IQueryable of type Movie, which can be used to query the data from storage. It also provides an AddMovie method to avoid exposure of the table name that is required for using TableServiceContext.AddObject.

To create a new StorageContext the StorageContextFactory can be used, which hides the creation/initialization of the StorageContext.

MovieRepository
The repository is used to access the data from the application. Creation of a MovieRepository requires a context that implements IStorageContext (e.g. StorageContext), which can be obtained by the StorageContextFactory. The reason for using an interface to the context is to be able to replace the context by some other implementation, for example in case of a non-Azure application or in case of unit-testing.

Example

   1:  MovieRepository repository = 
   2:      new MovieRepository(StorageContextFactory.GetContext());
   3:   
   4:  // Get all movies
   5:  IEnumerable<Movie> allMovies = repository.GetAll();
   6:   
   7:  // Get a single movie
   8:  Movie movie = repository.GetById(key);
   9:   
  10:  // Add a new movie
  11:  Movie newMovie = MovieFactory.Create();
  12:  newMovie.PartitionKey = "Action";
  13:  newMovie.Title = "An action movie!";
  14:  repository.Add(newMovie);

A few notes:

  • A MovieFactory is used to create a new Movie. In this factory an instance of a Movie is created and the RowKey property is populated with a new GUID. There are several alternatives but in this way you avoid unnecessary creation of GUIDs when objects are created by the context (in case you initialize in the Movie’s constructor), and you avoid the need for an initialization method that can potentially be forgotten to invoke.
  • The Add and Update methods on repository directly save to the storage. This might not be what you want in other apps, but for this example (with only one update in each postback) it is good enough.

The source of the example can be downloaded here.

Friday, August 20, 2010 1:33:32 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] -
 | 
 Sunday, August 05, 2007
SilverlightExpired

When opening my blog today I received an error message indicating that the WPF/E (Silverlight) version installed on my system is out of date. The result of the error is that my post about Silverlight isn't working anymore (in fact, the area of the app is not refreshed and shows bits and pieces of other content on the page).

The new 1.0 RC (javascript) and 1.1 Alpha Refresh (javascript and managed code) versions can be downloaded from here. However, that doesn't help with my example, although the 'Get Microsoft Silverlight' image is back.

The key to get it working again is to update the project with the new 'Silverlight Tools Alpha Refresh for Visual Studio', which is for VS2008 Beta II. But than I ran into error 'AG_E_INVALID_ARGUMENT 2210 Error' when the app tried to load my button (which is in another assembly). To solve this problem you have to make sure that the build action of the XAML control is set as 'Embedded Resource', which is described in a blog post of Rob Conery.

So the browser requires you to upgrade the client machine, but the software isn't backwards compatible. I guess you shouldn't write software with beta's if you don't want this to happen...

Sunday, August 05, 2007 3:39:23 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] -

 Tuesday, June 26, 2007

This blog post is about how to develop a small Silverlight (the new MS plug-in for Rich Interactive Applications (RIAs) on the web) application and get it  running inside this post. The demo app is quite simple, it has a button that pop ups the Silverlight logo when it is pushed.

Install development environment

For development of Silverlight apps the Orcas edition of VS is required plus some additional Silverlight packages, which can be downloaded from the Silverlight web site: the runtime 1.1 alpha, development tools for VS Orcas and the SDK for the necessary documentation and samples.

 

Using one of the Expression designer tools is optional but if you want to some fancy design stuff a designer is really a must. For this project I used Expression Blend 2 May Preview as the older version doesn't support Silverlight.

Design with Expression Blend

To get to know a bit about Silverlight and the graphical design possibilities using Expression Blend I used the great tutorials of nibbles tutorials. Playing with these tutorials gives you a great insight in using Blend to create graphical assets and use them in you app.

 

For this applications I've created the round button as a user control in a separate assembly. By adding some animations the button changes color when the mouse enters, and transforms a little smaller when you click it to create a button down effect. The rest of the app, including an animation to pop up the picture, is designed on the page.xaml, which is generated when you create a Silverlight app in Visual Studio. To use the button (from the separate assembly) on the page you have to define a namespace in the main canvas of the page.xaml to include the dll:

<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:myctrl="clr-namespace:SilverlightDemoClasslib; assembly=ClientBin/SilverlightDemoClasslib.dll" ... >

Now you can add the button by including the namespace prefix:

<Canvas x:Name="buttonCanvas" Canvas.Left="15" Canvas.Top="55"> <myctrl:SilverPlayButton x:Name="PlayButton"></myctrl:SilverPlayButton> </Canvas>

Unfortunately there is a little problem now, the page.xaml doesn't open in Blend anymore because it cannot find the button. At this moment I don't know how to fix this yet. I worked around it by adding the button at the last moment, when I didn't need Blend anymore.

 

Animations and Events

The timelines that were recorded in Blend (see the XAML file) have to be connected to the mouse events of the Canvas object. The MouseEnter and MouseLeave events are left out of the code below because it is pretty much the same.

public class SilverPlayButton : Control { private FrameworkElement implementationRoot; private Canvas _button; private Storyboard _mouseDown; public SilverPlayButton() { System.IO.Stream s = this.GetType().Assembly .GetManifestResourceStream("SilverlightDemoClasslib.SilverPlayButton.xaml"); implementationRoot = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd()); // Assign private variables _button = implementationRoot.FindName("button") as Canvas; _mouseDown = implementationRoot.FindName("mouseDown") as Storyboard; // Add eventhandlers _button.MouseLeftButtonDown += new System.Windows.Input .MouseEventHandler(_arcButton_MouseLeftButtonDown); } void _arcButton_MouseLeftButtonDown(object sender, System.Windows.Input.MouseEventArgs e) { // Start animation this._mouseDown.Begin(); } }

The last thing to do is to add an eventhandler for the button click in the page.xaml to start the animation of the picture.

public partial class Page : Canvas { public void Page_Loaded(object o, EventArgs e) { InitializeComponent(); PlayButton.MouseLeftButtonDown += new MouseEventHandler(PlayButton_MouseLeftButtonDown); } void PlayButton_MouseLeftButtonDown(object sender, MouseEventArgs e) { popupImageAnimation.Begin(); } }

Running Silverlight in dasBlog

That is not a real problem because, from a server point of view, Silverlight is just a bunch of javascript code. To get it running you copy the ClientBin folder (including the dll's), page.xaml file and the javascript files (Silverlight.js and TestPage.html.js) to the root of your web folder.  However, because I didn't want the root of my web server to be filled with these kind of files I created a separate ClientScripts folder for the xaml and js files. When you do so you have to make a change in the TestPage.html.js, which has a reference to the page.xaml: 

function createSilverlight() { Sys.Silverlight.createObjectEx({ source: "ClientScripts/Page.xaml", ... }

Now you can add a div to your blog post that acts as the host of the Silverlight app (note that you add the path to the js files):

 

<script src="ClientScripts/Silverlight.js" type="text/javascript"> </script> <script src="ClientScripts/TestPage.html.js" type="text/javascript"> </script> <div id="SilverlightControlHost"> <script type="text/javascript">createSilverlight();</script> </div>

And it should work...

Update: Removed the demo from this post because it doesn't work anymore with the new SL versions.

Tuesday, June 26, 2007 9:48:37 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] -

 Monday, June 18, 2007

Test Driven Development is a great thing in software development as it makes it possible to verify your code very easily during development and when you have to make changes. However, it is not always easy to write solid unit tests when external influences are involved. 

For example, in case of Web Services you might want to avoid the actual Web Service calls in your unit tests for reasons like availability, performance, predictability, ... of the Web Service. In my case I had to avoid the Web Services because I couldn't rely on the information they returned (the content of the underlying ERP database was out of my control).

To solve this problem for the unit tests and get a predictable response that can be tested there are basically two solutions:

  • Create a dummy Web Service that replaces the actual Web Service;
  • Replace the proxy of Web Service in the project by a dummy proxy.

In my project I choose for the latter on, based on a technique that is described in a blog post by David Waddleton. The trick that makes this technique possible and fairly easy is that the generated proxy is a partial class (.NET 2.0). This makes it possible to define an interface on the generated proxy (without changing it) by writing your own partial class. The service consumer can now communicate with the Web Service using the interface instead of directly to the Web Service. At this point you are free to replace the Web Service implementation for a mock object during the unit tests using some form of dependency injection.


ClassDiagram

Note: In the picture above the class MyService is displayed as two classes for clarity of the concept, in reality this is only one class implemented in two cs files.

Creating the interface might seem to be a lot of work, especially for larger Web Services, but it can easily be generated using the refactor tool [Extract Interface] on the proxy class defined in Reference.cs. The implementation of the partial MyService class stays empty, it only defines the interface IMyService (the implementation of IMyService is in the generated proxy).

For dependency injection I've used a simple factory class that either instantiates the Web Service or, in case of the unit tests, an instance of a mock object defined in the web.config.

However, as easy this is in C# 2.0 it cannot be implemented the same way in VB.NET. The reason is that VB.NET doesn't support implicit interface implementation, you have to define the interface implementation explicitly using the Implements keyword:

Public Function DoSomething() Implements IMyService.DoSomething End Function

This is too bad because you either have to alter Reference.cs (which you shouldn't because when you update the web reference you loose the changes) or implement new properties/functions in the otherwise empty class MyService and map them to the Web Service, which is a lot more work. 


The source code can be downloaded from here: WebServiceMockTechnique.zip (62,52 KB)

Monday, June 18, 2007 7:15:04 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [1] -
 |  | 
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010
Michel van der Vlugt
Sign In
All Content © 2010, Michel van der Vlugt
DasBlog theme 'Business' created by Christoph De Baene (delarou)