Opf3 and LINQ support
Opf3 is my little baby; speaking of this is the O/RM framework that I have created and that I love to tweak and add features to. As latest addition I have added LINQ support to it!
What are the steps to enable LINQ in Opf3?
First, you need to download the Demo or request the Express Edition of the framework. You could also buy it because you think the framework rocks or even I rock
- Buying means that you get the FULL source code of the framework and all updates for free!
Next, you need to create a Console Application (or Windows Forms Application, or even ASP.NET project) where you need to reference the Chili.Opf3 assembly and the new Chili.Opf3.Linq assembly. I have implemented all the LINQ features in an own assembly. It’s just an extension to the framework, which means you still can use the Opf3 “Core” in your .NET 2.0 projects (in Visual Studio 2005); without modifying one line of code.
Skip this step if you have already used Opf3: Then, you might read the beginner tutorials and guidelines for Opf3. Just to get you started with the framework.
Next comes the LINQ fun! To enabled LINQ you need to reference the “Chili.Opf3.Linq” namespace in the files where you want to use it. That’s because LINQ has been implemented by creating an extension method for the ObjectContext class and you won’t see the extension method without adding the namespace reference to the file!
You also need to register the storage class (that is the class that encapsulates the database for Opf3) with the so called “LinqQueryCommandBuilder”. This class is then used by the storage class to convert the Linq expression to an SQL statement (when a query comes to the storage all registered command builder are asked if they can handle the query. If none can you get an exception). This looks like this:
ObjectContext context =
// specify the command builder to have LinqQueries translated into SQL.
context.Storage.StorageCommandBuilders.Add(
Making your first query looks then like this:
var query = from c in context.GetPersistents<Contact>()
orderby c.ContactID
select c;
LINQ for Opf3 supports also anonymous types as return values …
where c.ContactType.Type.ToUpper() == “BUYER”
orderby c.LastName
select
, LastName = c.LastName
, Firstname = c.FirstName };
… and joins are also supported:
join u in context.GetPersistents() on c.ID equals u.ID
select u;
The LINQ extension to the Opf3 framework comes also with one helper class that holds some useful methods, like “Overlaps” (allows to check for overlapping date/time ranges), In (allows to specify a list of items and it checks the database values against them), Min, Max, Field<T> (allows to specify a field that is not found as property, but only present in the database).
You might also extend the Opf3 LINQ extension by adding own utility classes that implement the IMethodHandler interface. Such an extension would result in two new classes: one that holds the utility methods and the other that implements the IMethodHandler interface. The class that implements the IMethodHandler interface needs then to be registered with the LinqQueryCommandBuilder. The example shows the registering of a “RandomMethodHandler” that would handle the methods of a utility class that might be named “Randomizer”:
// create the linq query command builder.
var builder =
// register the method builder for the random class.
builder.MethodHandlers.Add(
// register the query command builder with the storage
// of the ObjectContext.
context.Storage.StorageCommandBuilders.Add(builder);
The usage of the “Randomizer” looks like this:
where i.ID == Randomizer.GetNextRandom(1, 100)
select i;
Now each time a method is encountered the LinqQueryCommandBuilder is going to ask all the registered MethodHandler if they can handle the method. In our case the RandomMethodHandler (since registered) can handle the method and will do that and return the result.
For an example on how to implement something like this check out the QueryUtility and QueryUtilityMethodsHandler classes in the Chili.Opf3.Linq project.
Thanks to Alfred Ortega, who already created a short intro to LINQ for Opf3 in the Opf3 forums.
Edit: Btw, if you are interested in what LINQ and lambda expressions in .NET are, you cold have a look at one of my older posts.






Published on Feb 18th, 2008 —
Tags:
[...] LINQ to ofp3 [...]
Pingback by Charlie Calvert's Community Blog : Link to Everything: A List of LINQ Providers — March 18, 2008 @ 10:12 pm
[...] LINQ to ofp3 [...]
Pingback by Charlie Calvert's Community Blog : Links to LINQ — March 18, 2008 @ 10:14 pm
[...] LINQ to ofp3 [...]
Pingback by Jacques Snyman » LINQ To … — March 19, 2008 @ 11:07 am
[...] LINQ to Opf3 [...]
Pingback by A List of LINQ Providers « vincenthome’s Tech Clips — November 29, 2008 @ 10:28 am
[...] LINQ to Opf3 [...]
Pingback by knom's developer corner : LINQ-To-Everywhere – List of LINQ Providers — April 27, 2009 @ 2:24 am
[...] LINQ to Opf3 [...]
Pingback by Tune Up Your PC » Post Topic » LINQ-To-Everywhere – List of LINQ Providers — April 27, 2009 @ 4:14 am