A few words on linq

Since Eco objects are normal .net objects you can use standard in memory linq.

Like this:

var standardlinq = from f in EcoSpace.Extents.AllInstances<Class1>() where (f.Attribute1 == "1") select f.Attribute1;


CapableObjects also offers the EcoQuery – the EcoQuery implements IQuerable – that is the interface behind linq.

Why do we do that when standard linq is just fine?
– The main reason is of course to interpret the linq expression and send the expression out to the persistence storage (like the database) and have that retrieve the result – so that we need not have or fetch ALL the stuff we look thru to see if it satisfies the criteria.

This is commonly referred to as LinqToSql – because most persistence storages seems to be made up of SQL-databases. Fair enough.

But in Eco the persistence storage (the database) is wrapped by an object oriented model driven layer. Many benefits comes from this – like the ability to have any flavor of database brand. Or to use something completely different as persistence storage – like an XmlFile – or just some Ram somewhere (like when doing mock tests).

The object oriented model driven layer in Eco uses the language OCL – object constraint language – as specified by the OMG standard.

This language gives many advantages over sql – and is equivalent to linq when it comes to ability to navigate and compile results in an object oriented world.

(When I say object oriented I mean as when DATA is represented as objects rather than as untyped result sets – many people call these objects for entities. Anyway – it is important to distinguish between objects that holds your data and objects that just provide infrastructure for you data – as in ADO – ActiveXDataObjects – ADO is just infrastructure objects – data is not uniquely typed as  entities )

Note that there are two different uses of linq and ocl. In memory is the first and the second is to instruct the persistence storage to fetch data that fulfills our criteria. We call these for MEMORY and PS (for persistence storage).

What are the main benefits for linq and ocl over sql?
– Well – you can of course construct an infinite amount of bugs with all three languages – but the number of bugs produced will be fewer and less severe in ocl and linq compared to sql. This is due to the fact that linq and ocl are more to the point on what you normally want to do when dealing with object oriented matter.

Why use ocl and linq in code instead of traditional memory access code ?
– The main reason is that ocl and linq expression are more compact than the alternative nested loops and if-statements. More compact means less to maintain and that is better. The secondary reason with using a declarative approach as linq and ocl (declarative = you state what you want – without much detail on how to do it) over the traditional imperative approach as standard c# code (imperative = you focus on HOW to reach the result) is that the engine behind the declarative expression might be able to optimize the execution for you. This is the case in Eco when utilizing lazy fetch – the engine can understand what you want in the end and be smart to fetch data in just the right sized chunks from PS – and this is much faster than fetching things one by one. Compare this to running to the store to buy 1 cracker. If you are in a loop you may need to run to the store thousands of times – taking one step back and analyzing the situation tells you that it would be better to go to the store once and buy all the needed crackers.

How do you use linq in persistence storage in Eco?
– You go like this:

      var z = from x in EcoSpace.PSQuery<Class1>() where (x.Attribute1 == "1") select x;
      foreach (Class1 y in z)
        // do somthing on each fecthed
      int c = z.ToArray().Count();

There are limits to what you can do with PSQuery – this is mainly because we fetch whole objects from PS – not data. You must select objects. You cannot create tuples. You cannot do many sql functions.

The linq is really translated to OCL that is sent to PS (so that it will work with all persistence mappers like xml, memory, sql ).

You can access the OCL:

(z as EcoQuery<Class1>).GetResultingOclExpression()
"Class1.allInstances.select(GenSym_0|GenSym_0.Attribute1 = '1')"

You can also use the same logic in memory – but there is not very much point since the standard in memory linq works just fine:

var ecolinq = from x2x in EcoSpace.MemQuery<Class1>() where (x2x.Attribute1 == "1") select x2x; // rather use standard memory linq than this

Are there benefits to linq over ocl?
In code – YES. Linq is compiled and strongly typed – while ocl is treated as strings. Refrain from writing ocl in your c# code – use linq. You can and should however use ocl in the model – so that you need not write so much code – less to maintain. OCL in the model is checked with the CheckModel tool in modlr. Ocl in the model is recommended for Constraints, Derived attributes and associations, ViewModels and Actions.

This entry was posted in Linq, OCL. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *