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 modelThe 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.
MovieRepositoryThe 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);
The source of the example can be downloaded here.
Remember Me
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.