Windows 7 Phone Database Rapid Repository Create Read Update and Delete
Download the code and a fully working example application from http://rapidrepository.codeplex.com/
Tutorials
- Introduction
- Set up the Rapid Repository
- Create, Read, Update and Delete
- Eager Loading
- Examining Pending Changes
- Exclude entities from the cache
Once you have set up the Rapid Repository Windows 7 Phone Database, you can start using the repository for all of your persistence requirements.
This tutorial assumes you have created an entity called Customer and a Customer Repository – please see the tutorial (Set up the Rapid Repository) for the simple set up requirements.
The RapidRepository repository has a number of methods to aid in easy database access, this includes Full Linq Support for querying data.
Add
To add an entity to the database, you simply call Add on the repository.
As soon as SaveChanges() is called on the context, the entity is added to the database.
- public void AddCustomer(Customer customer)
- {
- CustomerRepository repository = new CustomerRepository();
- repository.Add(customer);
- RapidContext.CurrentContext.SaveChanges();
- }
How it works
When you call Add, a request is saved into the context, it will stay there until RapidContext.Current.SaveChanges() is called, another tutorial will cover in depth the RapidContext and the possibilities of using the context for more advanced requirements.
When SaveChanges() has completed, the entity will be given a Guid Id.
Update
To update an entity within the database, you simply call Update on the repository.
As soon as SaveChanges() is called on the context, the entity is updated within the database.
- public void UpdateCustomer(Customer customer)
- {
- CustomerRepository repository = new CustomerRepository();
- repository.Update(customer);
- RapidContext.CurrentContext.SaveChanges();
- }
How it works
When you call Update, a request is saved into the context, it will stay there until RapidContext.Current.SaveChanges() is called.
Delete
To delete an entity from the database, you simply call Delete on the repository.
As soon as SaveChanges() is called on the context, the entity is delete from the database.
- public void DeleteCustomer(Guid customerId)
- {
- CustomerRepository repository = new CustomerRepository();
- repository.Delete(customerId);
- RapidContext.CurrentContext.SaveChanges();
- }
How it works
When you call Delete, a request is saved into the context, it will stay there until RapidContext.Current.SaveChanges() is called.
You can either pass the Id of an entity to the delete method or the actual entity itself.
GetById
To load an entity from the database by its Id, you simply call GetById on the repository with the entity id as the parameter.
- public void GetCustomer(Guid customerId)
- {
- CustomerRepository repository = new CustomerRepository();
- var customer = repository.GetById(customerId);
- }
GetAll
To load all the entities of a specified type from the database, you simply call GetAll on the repository.
- public void GetCustomers()
- {
- CustomerRepository repository = new CustomerRepository();
- List<Customer> customers = repository.GetAll();
- }
Query
The first release will support linq to objects on the result of the GetAll() method from the repository.
Although this isn’t ideal, for most apps this will sufffice.
Later releases will implement a more full query and view model for apps that need much faster queries.
- public IList<Customer> GetCustomersInSouthWestLondon()
- {
- CustomerRepository customerRepository = new CustomerRepository();
- return customerRepository.GetAll().Where(x => x.Address.PostCode.Contains("SW")).ToList();
- }
Hopefully you can see how simple the Repository API is to use.
I hope this pattern fits how most people prefer to develop when using the repository pattern.
Kind Regards,
Sean McAlinden.
* the first release uses the GetAll to retrieve the entities which will change when a more advanced query solution is put in place, this however will soon 99% of all apps and the caching and eager loading combats any speed performance issues.