Windows 7 Phone Database Rapid Repository – Examining Pending Changes

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

A possible useful feature of the RapidRepository is the ability to examine, add and remove pending changes.

Basically, before you call SaveChanges() on the context, every action on the repository is held in the context as a pending change called an Operation Request.

The OperationRequest class holds all of the properties required for saving the entity.

OperationRequest
  1. using System;
  2. using RapidRepository.DbC;
  3. namespace RapidRepository.Context
  4. {
  5.     /// <summary>
  6.     /// Stores data required for an operation.
  7.     /// </summary>
  8.     public class OperationRequest
  9.     {
  10.         #region Properties
  11.  
  12.         /// <summary>
  13.         /// Gets or sets the entity id.
  14.         /// </summary>
  15.         /// <value>The entity id.</value>
  16.         public Guid EntityId { get; private set; }
  17.  
  18.         /// <summary>
  19.         /// Gets or sets the entity.
  20.         /// </summary>
  21.         /// <value>The entity.</value>
  22.         public object Entity { get; private set; }
  23.         
  24.         /// <summary>
  25.         /// Gets or sets the type of the entity.
  26.         /// </summary>
  27.         /// <value>The type of the entity.</value>
  28.         public Type EntityType { get; private set; }
  29.  
  30.         /// <summary>
  31.         /// Gets or sets the type of the operation.
  32.         /// </summary>
  33.         /// <value>The type of the operation.</value>
  34.         public OperationRequestType OperationRequestType { get; private set; }
  35.  
  36.         #endregion
  37.  
  38.         #region Constructor
  39.  
  40.         /// <summary>
  41.         /// Initializes a new instance of the <see cref="OperationRequest"/> class.
  42.         /// </summary>
  43.         /// <param name="entity">The entity.</param>
  44.         /// <param name="entityType">Type of the entity.</param>
  45.         /// <param name="operationRequestType">Type of the operation request.</param>
  46.         public OperationRequest(object entity, Type entityType, OperationRequestType operationRequestType)
  47.         {
  48.             Contract.Requires("entity", entity != null);
  49.             Contract.Requires("entityType", entityType != null);
  50.  
  51.             if (operationRequestType != Context.OperationRequestType.Add)
  52.             {
  53.                 this.EntityId = ((IRapidEntity)entity).Id;
  54.             }
  55.  
  56.             this.Entity = entity;
  57.             this.EntityType = entityType;            
  58.             this.OperationRequestType = operationRequestType;
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Initializes a new instance of the <see cref="OperationRequest"/> class.
  63.         /// </summary>
  64.         /// <param name="entityId">The entity id.</param>
  65.         /// <param name="entityType">Type of the entity.</param>
  66.         /// <param name="operationRequestType">Type of the operation request.</param>
  67.         public OperationRequest(Guid entityId, Type entityType, OperationRequestType operationRequestType)
  68.         {
  69.             Contract.Requires("entityId", entityId != Guid.Empty);
  70.             Contract.Requires("entityType", entityType != null);
  71.  
  72.             this.EntityId = entityId;
  73.             this.EntityType = entityType;
  74.             this.OperationRequestType = operationRequestType;
  75.         }
  76.  
  77.         #endregion
  78.     }
  79. }

Each operation request has an operation type, indicated using the following enumeration:

OperationRequestType
  1. namespace RapidRepository.Context
  2. {
  3.     /// <summary>
  4.     /// The type of operation requested.
  5.     /// </summary>
  6.     public enum OperationRequestType
  7.     {
  8.         /// <summary>
  9.         /// Specifies an Add operation.
  10.         /// </summary>
  11.         Add = 0,
  12.  
  13.         /// <summary>
  14.         /// Specifies an Update operation.
  15.         /// </summary>
  16.         Update = 1,
  17.  
  18.         /// <summary>
  19.         /// Specifies an Delete operation.
  20.         /// </summary>
  21.         Delete = 2
  22.     }
  23. }

 

The methods for adding and removing OperationRequests reside on the RapidContext class, they are:

Add Operation Request
  1. public void AddOperationRequest(OperationRequest operationRequest)
Remove Operation Request
  1. public void DeleteOperationRequest(OperationRequest operationRequest)
Delete All Operation Requests
  1. public void DeleteAllOperationRequests()

 

The collection of pending OperationRequests live within a collection: RapidContext.CurrentContext.operationRequests

Example

So, the following is an example of how this can be used.

In this example, at the end of a game the users score is persisted to the database by default so it is added to the repository:

Add User Score
  1. ScoreRepository scoreRepository = new ScoreRepository();
  2.  
  3. Score score = new Score();
  4. score.Results.Add(new Result() { Score = 123, UserName = "Joe" });
  5.  
  6. scoreRepository.Add(score);

Now, before leaving the page and submitting the changes, you give the option of allowing the user to not save the score.

To be sure the score is not saved, you need to find it and remove it from the pending changes (OperationRequests).

Remove Add Operation Request
  1. OperationRequest addRequest = RapidContext.CurrentContext.operationRequests
  2.     .Where(x => x.OperationRequestType == OperationRequestType.Add).First();
  3.  
  4. RapidContext.CurrentContext.DeleteOperationRequest(addRequest);

 

Ok, even though this example is a little simplistic and contrived, you can see that you have a fair amount of control over the context and you can dig deeper into the operations of the database if required.

You can download Release V1.0, the source code and an example phone app from the following http://rapidrepository.codeplex.com/

 

I hope this  is useful.

Kind Regards,

Sean McAlinden.

No Comments