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
- Release Overview
- Creating and saving entities
- Create, read, update and delete operations
- Querying with views and filters
- Caching
- File Helper
- Examining Operation Requests
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:
- public class Profile : IRapidEntity
- {
- public string UserName { get; set; }
- public Guid Photo { get; set; }
- }
Using the above class, you could store a link to an image stored as a byte array string within the file system.
- public void SavePhoto(string userName, Byte[] photo)
- {
- RapidRepository<Profile> repository = new RapidRepository<Profile>();
- // convert the byte array to a string.
- string photoString = Encoding.UTF8.GetString(photo, 0, photo.Length);
- Profile profile = new Profile();
- profile.UserName = userName;
- profile.Photo = RapidRepository.File.Save(photoString);
- repository.Add(profile);
- RapidContext.CurrentContext.SaveChanges();
- }
You can load the file back using the value stored in the entity.
- public byte[] LoadPhoto(Profile userProfile)
- {
- string photoString = RapidRepository.File.Load(userProfile.Photo);
- return Encoding.UTF8.GetBytes(photoString);
- }
You can also delete the file using the same Guid value.
- public void DeletePhoto(Profile userProfile)
- {
- RapidRepository.File.Delete(userProfile.Photo);
- }
Summary
I hope you find these helper methods useful.
Sean McAlinden.