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:

// set the ObjectContext up, by using a SQL Server storage class instance.
ObjectContext context = new ObjectContext(
    new MsSqlStorage(“sa”, “password”, “localhost\\sqlexpress”, “Test”))
// specify the command builder to have LinqQueries translated into SQL. 
    new LinqQueryCommandBuilder());

Making your first query looks then like this:

// get all contacts ordered by the ID.
var query = from c in context.GetPersistents<Contact>()
                  orderby c.ContactID
                  select c;

LINQ for Opf3 supports also anonymous types as return values …

var anonquery = from c in contacts
                where c.ContactType.Type.ToUpper() == “BUYER”
                orderby c.LastName
                select new { ContactID = c.ContactID
                                 , LastName = c.LastName
                                 , Firstname = c.FirstName };

… and joins are also supported:

var query = from c in context.GetPersistents()             
            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&gt (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”:

ObjectContext context = new ObjectContext(
     new MsSqlStorage(“sa”, “password”, “localhost\\sqlexpress”, “Test”));
// create the linq query command builder.
var builder = new LinqQueryCommandBuilder();
// register the method builder for the random class.
builder.MethodHandlers.Add(new RandomMethodHandler());

// register the query command builder with the storage
// of the ObjectContext.

The usage of the “Randomizer” looks like this:

var query = from i in context.GetPersistents<Foo>()
                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: , ,
   digg it!    kick it   

11 Comments ( Comments RSS TrackBack )

  1. [...] LINQ to ofp3 [...]

    Pingback by Charlie Calvert's Community Blog : Link to Everything: A List of LINQ Providers — March 18, 2008 @ 10:12 pm

  2. [...] LINQ to ofp3 [...]

    Pingback by Charlie Calvert's Community Blog : Links to LINQ — March 18, 2008 @ 10:14 pm

  3. [...] LINQ to ofp3 [...]

    Pingback by Jacques Snyman » LINQ To … — March 19, 2008 @ 11:07 am

  4. [...] LINQ to Opf3 [...]

    Pingback by A List of LINQ Providers « vincenthome’s Tech Clips — November 29, 2008 @ 10:28 am

  5. [...] LINQ to Opf3 [...]

    Pingback by knom's developer corner : LINQ-To-Everywhere – List of LINQ Providers — April 27, 2009 @ 2:24 am

  6. [...] LINQ to Opf3 [...]

    Pingback by Tune Up Your PC » Post Topic » LINQ-To-Everywhere – List of LINQ Providers — April 27, 2009 @ 4:14 am

  7. [...] LINQ to Opf3 [...]

    Pingback by LINQ Providers | Mohamed Abdeen — September 16, 2010 @ 3:33 pm

  8. Hi Christian,

    Do you have a newer version of opf3 than the one available on open source, or do you intend to update the old version to .NET 4.0.? Are you still using opf3? If not, what are you using instead?

    Is there someone out there still using opf3?



    Comment by Henk Havenga — March 20, 2011 @ 5:13 am

  9. Hello,

    Do OPF3 & LINQ work on an Oracle database ?


    Comment by Mickael — July 21, 2011 @ 7:26 am

  10. There exist a couple of numerous different distinct levels among the California Weight loss program and each and every a person is pretty essential. You?re procedure stands out as the the actual giving up with all the power. weight loss 632091

    Comment by Homepage — December 8, 2011 @ 8:12 pm

  11. [...] LINQ to Opf3 [...]

    Pingback by LINQ to X | BrunoRodrigues.info — June 27, 2012 @ 8:04 pm

Leave a comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> [code][/code] [code lang="csharp"][/code].