Archive for September, 2007
Ordering and Paging using LightSpeed
Tagged as LightSpeedEarlier we talked about how LightSpeed makes querying easy by using the Query object and Query Expressions. There are two other concerns which are quite important when querying which I thought we should mention and can be managed through the Query object, Ordering and Paging of your result sets.
Ordering
Ordering helps you sort results for use and this is a standard aspect in any database SQL. LightSpeed lets you specify one or more ordering clauses through the Query object.
To apply an Ordering, you use an instance of the Order object. We have provided an easy way to construct these in an expressive fashion through use of the static By method which will construct a new Order instance configured for use. This can be expanding on by successive calls to the AndBy method.
e.g.
Query.Order = Order.By(“Name”); Query.Order = Order.By(“Name”).AndBy(“Description”);
The default sort order is Ascending which will be applied based on the type of the property being sorted, however you can change this to descending order by using the Descending() method as part of expressing your ordering.
e.g.
Query.Order = Order.By(“Name”).Descending();
Paging
Paging is an important aspect of data management, particularly when you are dealing with stateless applications such as web apps where the data context needs to be reconstructed for every query.
Paging lets you manage how much data is being returned from the database to optimize it for display where typically when faced with large data volumes we break up the results into pages of data. Paging can be performed either client side (once you know how much data you have and how you want to display it), or server side (if you know your display parameters in advance). The benefit of server side paging is that only the requested page of data is returned over the wire between your database and your application – so we should always try to do this :)
As part of the Query object, you can specify paging details for LightSpeed, and it will work to perform server side paging as supported by the query provider. Currently all providers we support handle this!
To specify paging, you need to provide an instance of the Page class configured either with an offset (Skip) or a limit for the number of records to be returned (Limit) or both (At).
e.g.
Query.Page = Page.Skip(10); Query.Page = Page.Limit(5); Query.Page = Page.At(10,5);
So now we know all about querying in LightSpeed and how to customize the behavior of our result sets.
Remember to check the query samples which are installed along with LightSpeed for many more examples of querying :)
Querying with LightSpeed
Tagged as LightSpeedWhen working with LightSpeed and your Domain Model, one of the main tasks you will have to perform is to query for objects which match a specific criteria.
LightSpeed provides several methods for querying against the object Repository to allow you to fetch single instance objects or sets of objects matching a specific criteria. In LightSpeed, the object Repository exposes two methods which allow us to query for objects – Find and FindOne.
Find
Find allows us to return a set of objects which match a given expression. There are two main overloads for this method which you can use;
Repository.Find<T>()
This returns all objects of the entity type T
Repository.Find<T>(Query query)
This returns all objects of the entity type T which matches the expression specified in the Query object.
FindOne
FindOne allows us to return a single object matching a given expression. Again there are two main overloads we use;
Repository.FindOne<T>(object id)
This returns the entity of type T which has the key value of id. The type for the id needs to match the identity type you are using with your entity objects.
Repository.FindOne<T>(Query query)
This returns the entity of type T which matches the expression specified in the Query object.
Query Object
In the examples above, we have refered to specifing our expressions inside a Query object. The role of this class is to set up the context associated with a query in order to minimise the number of arguments that need to be passed to the Find and FindOne methods. The primary argument we pass to a Query is a QueryExpression which specifies the criteria we are searching on.
Query Expressions
Rather than writing in the query language understood by your database provider (SQL), LightSpeed query expressions use an specification syntax which uses the structure of your domain model. LightSpeed manages translating these queries down to the native SQL which will be executed against the data provider.
Here is an 2 examples of how this works:
// find all Contributions with a title of "Dolphins" Query query = new Query(Entity.Attribute("Title") == "Dolphins"); Repository.Find<Contribution>(query);
// find all Contributions with a title of "Dolphins" // that were added after 01-01-2007 Query query = new Query(Entity.Attribute("Title") == "Dolphins" && Entity.Attribute("AddedOn") > new DateTime(2007, 01, 01)); Repository.Find<Contribution>(query);
Both Repository.Find and Repository.FindOne also support overloads which take in a QueryExpression directly, e.g.
Repository.Find<Contribution>(Entity.Attribute("Title") == "Dolphins");
When you install LightSpeed, one of the sample projects contains an extensive set of queries to help show you the full range of capabilities in LightSpeed for querying. If you think something is missing let us know by dropping in to the Forums :)
Happy querying!
![]()
BrainDump (1)
Community Code (1)
Events (7)
General (34)
Lab Samples (2)
LightSpeed (144)
MegaPack (3)
News (52)
NHibernate Designer (4)
Products (68)
Projects (4)
Screencast (6)
SharePoint (2)
Silverlight (7)
Silverlight Elements (14)
SimpleDB Management Tools (12)
Visual Studio (4)
VS File Explorer (6)
WPF (34)
WPF Diagramming (17)
WPF Elements (30)
WPF Property Grid (26)
![]()
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007





Posted by Jeremy Boyd on 25 September 2007

