Windows Phone 7 Database Rapid Repository V2.0 Creating and Saving Entities

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

Download Rapid Repository from Microsoft Codeplex

Blog Tutorials

    Creating and saving entities to the Windows 7 Phone is really simple when using the Rapid Repository.

    For the following examples, my entity is called Player which among it’s properties contains a list of entities called GameScore.

    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. }

     

    As you can see, the Player class is the root entity or root aggregate, you will notice that it implement the IRapidEntity interface.

    This interface enforces the implementation of a Guid Id property.

     

    Simple Example

    The following example shows how to save an entity to the database.

    There is no setup at all, the following code is all you need to save a valid entity.

    Saving a Player instance
    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. }

    As you can see, once the player is created, a repository is created, the player is passed to the Add method and finally SaveChanges() is called on the current context.

    The newPlayer instance passed in will now have it’s Id property populated (Do not change this Id).

    *Note*

    The reason you call SaveChanges() is because when Add is called on the repository, it is just creating a request to save the entity, it is not actually saved until SaveChanges() is called.

     

    Simple Example with Pre-Populated Id

    Similar to the previous example, if for any reason you need to create your own Id rather than have the framework create it, you simple set the Id property before SaveChanges() is called on the context.

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

     

    Derived Repository

    If you want to add extra functionality such as logging or you want to hide away the call to Savechanges() on the context, you can derive from RapidRepository<>, all the relevant methods can be overridden.

    Player Repository
    1. public class PlayerRepository : RapidRepository<Player>
    2. {
    3.     public override Player Add(Player entity)
    4.     {
    5.         base.Add(entity);
    6.         RapidContext.CurrentContext.SaveChanges();
    7.         return entity;
    8.     }
    9.  
    10.     public override Player Update(Player entity)
    11.     {
    12.         base.Update(entity);
    13.         RapidContext.CurrentContext.SaveChanges();
    14.         return entity;
    15.     }
    16.  
    17.     public override void Delete(Player entity)
    18.     {           
    19.         base.Delete(entity);
    20.         RapidContext.CurrentContext.SaveChanges();
    21.     }
    22.  
    23.     public override void Delete(System.Guid id)
    24.     {
    25.         base.Delete(id);
    26.         RapidContext.CurrentContext.SaveChanges();

     

    Now the above SavePlayer() method looks like the following:

    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.     PlayerRepository repository = new PlayerRepository();
    7.     repository.Add(newPlayer);
    8. }

     

    Summary

    That’s about it for saving entities, the next post will cover all of the Create, Read, Update and Delete functionality.

    I hope you find this useful.

    Kind Regards,

    Sean McAlinden.

    2 Comments

    Comments have been disabled for this content.