Windows 7 Phone Database Rapid Repository Set Up

Download the code and a fully working example application from http://rapidrepository.codeplex.com/

Tutorials

  1. Introduction
  2. Set up the Rapid Repository
  3. Create, Read, Update and Delete
  4. Eager Loading
  5. Examining Pending Changes
  6. Exclude entities from the cache

    As my windows 7 phone database open source project is about to go to beta, I thought I’d better start putting some tutorials in place.

    Even though this tutorial is titled setup, to use the repository actually doesn’t really require much setup at all, there is no config, schemas, tables, stored procedures etc. to worry about so this will be a pretty short blog.

    First you will need to download the code from http://rapidrepository.codeplex.com/ (there will be a binary beta dll available in the next couple of days, for now just build the solution once you’ve downloaded it).

     

    Setup Requirements

    You will need to be using Visual studio 2010.
    You will need to have the the Windows 7 Phone sdk installed, you can download it from http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c17ba869-9671-4330-a63e-1fd44e0e2505&displaylang=en.

    The minimum steps for getting up and running with the repository are as follows:

    • Add a reference to the RapidRepository.dll in your project
    • Implement the IRapidEntity interface on your root entities (sometimes known as the aggregate root)
    • Create your repositories (they will derive from the generic RapidRepository<> )

    That’s it.

     

    Example

    Create the entity classes

    Lets say you want to store a customer object that has an address object – the classes could look something like the following:

    Customer & Address Classes
    1. public class Customer : IRapidEntity
    2. {
    3.     public Guid Id { get; set; }

    4.     public string FirstName { get; set; }

    5.     public int Age { get; set; }

    6.     public Address Address { get; set; }
    7. }

    8. public class Address
    9. {
    10.     public int HouseNumber { get; set; }
    11.     public string PostCode { get; set; }
    12. }

    The main thing to notice is that the root entity ‘Customer’ implements the IRapidEntity interface.

    This interface enforces that you add the Guid Id property to the root entity.

    That is all you need to make an entity type ready for persistence with the Rapid Repository.

     

    Create the repository

    Creating the repository is just as simple, in this case we want to create a CustomerRepository.

    Customer Repository
    1. public class CustomerRepository : RapidRepository<Customer>
    2. {
    3. }

     

    Done – Setup Complete.

    Now that the set up has been completed, you can start working with the repository and entities as usual, the underlying library will handle all of the actual persistence.

     

    Example usage

    One last thing to note, similar to how the entity framework works, all Add, Update and Delete calls to the repository live as requests in a context, to actually persist these changes to the underlying database you need to call RapidContext.Current.SaveChanges() as per the code sample below.

    Adding an entity example
    1. public void AddNewCustomer(Customer customer)
    2. {
    3.     CustomerRepository repository = new CustomerRepository();
    4.     repository.Add(customer);

    5.     RapidContext.Current.SaveChanges();
    6. }

     

    The reason for this is if you want to add logic to analyse changes or create a Unit of Work pattern, you have full control over when the repository actually commits any changes.

     

    Of course if you do not need this functionality, you can simply alter how you create the CustomerRepository like below, then you would not need to worry about calling it.

     

    CustomerRepository
    1. public class CustomerRepository : RapidRepository<Customer>
    2. {
    3.     public override Customer Add(Customer entity)
    4.     {
    5.         var customer = base.Add(entity);
    6.         RapidContext.CurrentContext.SaveChanges();
    7.         return customer;
    8.     }

    9.     public override void Delete(System.Guid id)
    10.     {
    11.         base.Delete(id);
    12.         RapidContext.CurrentContext.SaveChanges();
    13.     }

    14.     public override Customer Update(Customer entity)
    15.     {
    16.         var customer = base.Update(entity);
    17.         RapidContext.CurrentContext.SaveChanges();
    18.         return customer;
    19.     }
    20. }

     

    Now you would only need code like the following to use the repository – i.e no need to call RapidContext.Current.SaveChanges().

    Updating an entity example
    1. public void UpdateCustomer(Customer customer)
    2. {
    3.     CustomerRepository repository = new CustomerRepository();
    4.     repository.Update(customer);
    5. }

     

    That’s all for this post – hopefully you can see that the Rapid Repository database is extremely easy to use and should take away any of the pain of dealing with isolated storage on Windows 7 Phone.

    I hope this is useful.

     

    Kind Regards,

     

    Sean McAlinden

    4 Comments

    Comments have been disabled for this content.