Windows Phone 7 Database Rapid Repository V2.0 Create Read Update and Delete

Rapid Repository is a Windows Phone 7 Database and Silverlight Isolated Storage Database.

Download Rapid Repository from Microsoft Codeplex

Blog Tutorials

The Windows 7 phone database Rapid Repository makes create, read, update and delete functionality extremely simple.

The following examples will show how to perform these operations including an extra helper method called Exists.

The following entity classes will be used throughout the examples.

Player
  1. public class Player : IRapidEntity
  2. {
  3.     public Guid Id { get; set; }
  4.     public string FirstName { get; set; }
  5.     public string LastName { get; set; }
  6.     public List<GameScore> Scores { get; set; }
  7.  
  8.     public Player()
  9.     {
  10.         this.Scores = new List<GameScore>();
  11.     }
  12. }

 

GameScore
  1. public class GameScore
  2. {
  3.     public double Score { get; set; }
  4.     public DateTime Date { get; set; }
  5. }

 

Create

As seen in my previous post, a create is performed by simply calling Add on the repository and saving the changes.

Save Player
  1. public void SavePlayer()
  2. {
  3.     Player newPlayer = new Player { FirstName = "Sean", LastName = "Mcalinden", Scores = new List<GameScore>() };
  4.     newPlayer.Scores.Add(new GameScore { Score = 123, Date = DateTime.Now });
  5.  
  6.     RapidRepository<Player> repository = new RapidRepository<Player>();
  7.     repository.Add(newPlayer);
  8.     RapidContext.CurrentContext.SaveChanges();
  9. }

 

Update

An entity is updated by simply passing the entity to the update method on the repository and saving the changes.

Update Player
  1. public void Update(Player player)
  2. {
  3.     player.Scores.Add(new GameScore { Score = 111, Date = DateTime.Now });
  4.  
  5.     RapidRepository<Player> repository = new RapidRepository<Player>();
  6.     repository.Update(player);
  7.     RapidContext.CurrentContext.SaveChanges();
  8. }

 

Delete

Delete operations can be achieved by either passing the Id of the entity or by passing in the entity itself.

Delete
  1. public void Delete(Guid Id)
  2. {
  3.     RapidRepository<Player> repository = new RapidRepository<Player>();
  4.     repository.Delete(Id);
  5.     RapidContext.CurrentContext.SaveChanges();
  6. }
  7.  
  8. public void Delete(Player player)
  9. {
  10.     RapidRepository<Player> repository = new RapidRepository<Player>();
  11.     repository.Delete(player);
  12.     RapidContext.CurrentContext.SaveChanges();
  13. }

 

Exists

The following shows an example of how to check whether an entity exists before performing an action.

Exists
  1. public void Exists(Guid id)
  2. {
  3.     RapidRepository<Player> repository = new RapidRepository<Player>();
  4.     if(repository.Exists(id))
  5.     {
  6.         // do something
  7.     }
  8. }

 

GetById

The following shows how to retrieve an entity by it’s Id.

GetById
  1. public void GetById(Guid id)
  2. {
  3.     RapidRepository<Player> repository = new RapidRepository<Player>();
  4.     Player entity = repository.GetById(id);
  5. }

 

GetAll

The following shows how to retrieve all entities of a given type, this returns a list which you can filter using linq or lambda.

GetAll
  1. public void GetAll()
  2. {
  3.     RapidRepository<Player> repository = new RapidRepository<Player>();
  4.     IList<Player> players = repository.GetAll();
  5. }

 

Linq & Lambda
  1. public void GetAll()
  2. {
  3.     RapidRepository<Player> repository = new RapidRepository<Player>();
  4.  
  5.     IEnumerable<Player> lambda = repository.GetAll()
  6.         .Where(x => x.LastName == "McAlinden");
  7.  
  8.     IEnumerable<Player> linq = from p in repository.GetAll()
  9.                          where p.LastName == "McAlinden"
  10.                          select p;
  11. }

 

Summary

This covers all of the main repository methods.

The next post will show a much better way to query the database using Views and Filters.

I Hope this is useful.

Kind Regards,

Sean McAlinden.

3 Comments

  • After testing practically all available options for data persistence on Windows Phone 7, Rapid Repository has become a solution of my choice. Support for transactions is really required and I am glad you're already working on it. Also, it would be good to have some sort of master clean or reset function that would delete all data. At the moment, I have a method where I manually delete all data from all repositories.

    Again, thanks for making Rapid Repository.

  • Hi George,
    Glad you like it, I've started work on the transactions bit so will definitely be in the next release and will be available pretty soon. I'll add some methods for deleting all data and maybe deleting all data for a specific entity type.
    Kind Regards,
    Sean McAlinden.

  • Hi Sean,

    I have started using Rapid Repository as DB, it seems this is best choice as of now. Thanks for making it.
    Any update on deleting all data for a specific entity?

    Thanks,
    Vinay

Comments have been disabled for this content.