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
- Release Overview
- Creating and saving entities
- Create, read, update and delete operations
- Querying with views and filters
- Caching
- File Helper
- Examining Operation Requests
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.
- public class Player : IRapidEntity
- {
- public Guid Id { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public List<GameScore> Scores { get; set; }
- public Player()
- {
- this.Scores = new List<GameScore>();
- }
- }
- public class GameScore
- {
- public double Score { get; set; }
- public DateTime Date { get; set; }
- }
Create
As seen in my previous post, a create is performed by simply calling Add on the repository and saving the changes.
- public void SavePlayer()
- {
- Player newPlayer = new Player { FirstName = "Sean", LastName = "Mcalinden", Scores = new List<GameScore>() };
- newPlayer.Scores.Add(new GameScore { Score = 123, Date = DateTime.Now });
- RapidRepository<Player> repository = new RapidRepository<Player>();
- repository.Add(newPlayer);
- RapidContext.CurrentContext.SaveChanges();
- }
Update
An entity is updated by simply passing the entity to the update method on the repository and saving the changes.
- public void Update(Player player)
- {
- player.Scores.Add(new GameScore { Score = 111, Date = DateTime.Now });
- RapidRepository<Player> repository = new RapidRepository<Player>();
- repository.Update(player);
- RapidContext.CurrentContext.SaveChanges();
- }
Delete
Delete operations can be achieved by either passing the Id of the entity or by passing in the entity itself.
- public void Delete(Guid Id)
- {
- RapidRepository<Player> repository = new RapidRepository<Player>();
- repository.Delete(Id);
- RapidContext.CurrentContext.SaveChanges();
- }
- public void Delete(Player player)
- {
- RapidRepository<Player> repository = new RapidRepository<Player>();
- repository.Delete(player);
- RapidContext.CurrentContext.SaveChanges();
- }
Exists
The following shows an example of how to check whether an entity exists before performing an action.
- public void Exists(Guid id)
- {
- RapidRepository<Player> repository = new RapidRepository<Player>();
- if(repository.Exists(id))
- {
- // do something
- }
- }
GetById
The following shows how to retrieve an entity by it’s Id.
- public void GetById(Guid id)
- {
- RapidRepository<Player> repository = new RapidRepository<Player>();
- Player entity = repository.GetById(id);
- }
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.
- public void GetAll()
- {
- RapidRepository<Player> repository = new RapidRepository<Player>();
- IList<Player> players = repository.GetAll();
- }
- public void GetAll()
- {
- RapidRepository<Player> repository = new RapidRepository<Player>();
- IEnumerable<Player> lambda = repository.GetAll()
- .Where(x => x.LastName == "McAlinden");
- IEnumerable<Player> linq = from p in repository.GetAll()
- where p.LastName == "McAlinden"
- select p;
- }
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.