A few words on linq

❗🕜 Warning: this article may contain outdated information. Consider before using any descriptions/solutions, otherwise, it can still be helpful. Help: Synonyms and name changes

Since ECO (MDriven) 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;

MDriven 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 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 don't need to have or fetch ALL the stuff we look through to see if it satisfies the criteria.

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

However, in ECO, the persistence storage (the database) is wrapped by an object-oriented model-driven layer. Many benefits come 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 regarding its ability to navigate and compile results in an object-oriented world.

(When I say object-oriented, I mean when DATA is represented as objects rather than as untyped result sets – many people refer to these objects as entities. It is important to distinguish between objects that hold your data and those that provide infrastructure for your 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. Memory is the first and second is to instruct the persistence storage to fetch data that fulfills our criteria. We call these MEMORY and PS (for persistence storage).

What are the main benefits of Linq and OCL over SQL?

- Well, you can 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 because 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 expressions are more compact than the alternative nested loops and if-statements. More compact means less to maintain and that is better. The secondary reason for 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 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 do 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, and 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 this is pointless 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?

Yes, there are - in code. 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 – this costs 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, Associations, ViewModels, and Actions.

This page was edited 48 days ago on 02/10/2024. What links here