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

  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

    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.

    Add Customer
    1. public void AddCustomer(Customer customer)
    2. {
    3.     CustomerRepository repository = new CustomerRepository();
    4.     repository.Add(customer);

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

    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.

    Update Customer
    1. public void UpdateCustomer(Customer customer)
    2. {
    3.     CustomerRepository repository = new CustomerRepository();
    4.     repository.Update(customer);

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

    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.

    Delete Customer
    1. public void DeleteCustomer(Guid customerId)
    2. {
    3.     CustomerRepository repository = new CustomerRepository();
    4.     repository.Delete(customerId);

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

    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.

    Get Customer By Id
    1. public void GetCustomer(Guid customerId)
    2. {
    3.     CustomerRepository repository = new CustomerRepository();
    4.     var customer = repository.GetById(customerId);
    5. }

    GetAll

    To load all the entities of a specified type from the database, you simply call GetAll on the repository.

    Gett All Customers
    1. public void GetCustomers()
    2. {
    3.     CustomerRepository repository = new CustomerRepository();
    4.     List<Customer> customers = repository.GetAll();
    5. }

    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.

    Querying the repository
    1. public IList<Customer> GetCustomersInSouthWestLondon()
    2.     {
    3.         CustomerRepository customerRepository = new CustomerRepository();
    4.         return customerRepository.GetAll().Where(x => x.Address.PostCode.Contains("SW")).ToList();
    5.     }

    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.

    6 Comments

    Comments have been disabled for this content.