Windows 7 Phone Database Rapid Repository – Queries and Views Proposal
Hi All,
I’m about to start working on the query side of the Rapid Repository as I think this will be the most important requirement for the next release (which I am hoping will come pretty soon).
My current plan is to do something fairly simple but I thought I’d publish the idea first in case any would prefer a different implementation.
Proposed implementation
Setup
Queries will be done against View Models.
A view model will be a simple class which implements an interface IRapidView (this will enforce that the entity id is captured within the view model class).
The view model will or should be a flattened subset of an entity, perfect for displaying on a page.
An example could be something like the following:
- public class CustomerView : IRapidView
- {
- public Guid Id { get; set; }
- public string FirstName { get; set; }
- public string PostCode { get; set; }
- }
To setup the view, at application start up there will be a method similar to the following:
- RapidRepository.AddView<Customer, CustomerView>(c =>
- {
- return new CustomerView { FirstName = c.FirstName };
- })
Also, I am proposing a filtering feature during setup where linq / lambda can be used to ensure the view filters out any unwanted data – it will look similar to the following:
- RapidRepository.AddView<Customer, CustomerView>(c =>
- {
- return new CustomerView { FirstName = c.FirstName };
- })
- .Filter<CustomerView>(cv => cv.FirstName != "Sean" && cv.PostCode.Contains("SW1"));
Querying the View
Querying the View will be achieved through a method on the repository that will take the type of view as a generic parameter, this will return a (possibly cached) List<TViewType> that can be queried against using full Linq to Objects.
This will look similar to the following (lambda):
- Repository<Customer> customerRepository = new Repository<Customer>();
- List<CustomerView> results = customerRepository.Query<CustomerView>().Where(x => x.PostCode.Contains("SW1")).ToList();
or Linq
- List<CustomerView> results = (from c in customerRepository.Query<CustomerView>()
- where c.PostCode.Contains("SW1")
- select c).ToList();
Why will this be any better than just loading all the entities?
There are a number of reasons why this would be better than getting all on an entity type (how the current release works).
The first being the fact that the view model will contain only the data required for the view, this immediately means less data in memory and less to de-serialise.
The other reasons will include how i intend to store, cache and load the View.
As a basic idea, I’m going to look at storing a view in one view file per type, and have that (optionally) load into the application cache on start up.
The view will then be synchronously managed in memory whilst asynchronously managed within isolated storage.
Conclusion
Anyway, as a really basic overview, that is my current plan, any thoughts, criticisms or ideas would be most welcome.
You can download the current release, source code and test windows 7 phone application from http://rapidrepository.codeplex.com/.
Kind Regards,
Sean McAlinden.