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!
Leave a Reply
Categories
BrainDump (1)
Community Code (4)
Events (15)
F# (11)
General (50)
Lab Samples (2)
LightSpeed (249)
MegaPack (7)
News (68)
NHibernate Designer (18)
Nightly news (40)
Phone Elements (22)
Products (87)
Projects (5)
Screencast (6)
SharePoint (3)
Silverlight (14)
Silverlight Elements (59)
SimpleDB Management Tools (20)
Visual Studio (9)
VS File Explorer (7)
Web Workbench (20)
WPF (43)
WPF Diagrams (53)
WPF Elements (91)
WPF Property Grid (32)



Posted by Jeremy Boyd on 20 September 2007 


