Windows Phone 7 Database Rapid Repository V2.0 File Helper

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

Download Rapid Repository from Microsoft Codeplex

Blog Tutorials

I have added some methods for saving, retrieving and deleting files which are not entities.

If you have a large amount of data such as the byte array of an image, storing the byte array directly in the entity could cause a significant performance problem as converting the byte[] to json can take quite a long time on a phone.

A better solution would be to store the byte array as a separate file and just hold a pointer to the file on the entity.

For example:

User Profile
  1. public class Profile : IRapidEntity
  2. {
  3.     public string UserName { get; set; }
  4.     public Guid Photo { get; set; }
  5. }

 

Using the above class, you could store a link to an image stored as a byte array string within the file system.

Save User Profile
  1. public void SavePhoto(string userName, Byte[] photo)
  2. {
  3.     RapidRepository<Profile> repository = new RapidRepository<Profile>();
  4.  
  5.     // convert the byte array to a string.
  6.     string photoString = Encoding.UTF8.GetString(photo, 0, photo.Length);
  7.  
  8.     Profile profile = new Profile();
  9.     profile.UserName = userName;
  10.     profile.Photo = RapidRepository.File.Save(photoString);
  11.  
  12.     repository.Add(profile);
  13.     RapidContext.CurrentContext.SaveChanges();    
  14. }

 

You can load the file back using the value stored in the entity.

Load Photo
  1. public byte[] LoadPhoto(Profile userProfile)
  2. {
  3.     string photoString = RapidRepository.File.Load(userProfile.Photo);
  4.     return Encoding.UTF8.GetBytes(photoString);
  5. }

 

You can also delete the file using the same Guid value.

Delete Photo
  1. public void DeletePhoto(Profile userProfile)
  2. {
  3.     RapidRepository.File.Delete(userProfile.Photo);
  4. }

 

Summary

I hope you find these helper methods useful.

Sean McAlinden.

No Comments