Using LinQ in your ECO Models

LinQ is much more than just being able to write database applications easier. People, who say that LinQ is limited to databases, should really have a look at it first. The strength of LinQ is that you can apply SQL-like queries to framework objects. Mostly, those are the same people who say that WCF is a new framework from Microsoft to force users to use new protocols invented by MS — which is just as wrong. But back to the topic.

Assume, I have a class named “Person” and a person can have write documents. Thus, I add an one-to-many association to the class “Document”. I also add a method named “GetDocument” to the class “Person”. Every document is identified by a name, which is unique. The previously mentioned method “GetDocument” has exactly one parameter so I can specify the name of the document I want to retrieve. I make sure that the name is unique by always using “GetDocument” which uses LinQ to get the document I want.

It becomes very simple as the class “Person” already offers a list of “Document” with all the linked documents. I just need to find a document with a certain name. LinQ can be used with list classes in the .NET framework that implement IEnumerable and thus, you can use the following code to find a document. Furthermore, the following method creates a new document and hooks it up to the instance of person if there is no document by the name one is looking for.

As my previous blog post indicates, you can use this comfort with VCL.NET and RAD Studio 2007 just as long as you build your models with the Visual Studio version of ECO. Sadly, there is no Delphi .NET support for LinQ yet.

For the people, who are 100% certain that LinQ is just something to make database access easier… where is the database in my example? :-)

blog comments powered by Disqus