Luca, Daniel,You are mostly correct about LINQ to Objects.LINQ is just some kind of syntax for some functional
programming aspects. It's about to be a 1:1 mapping with
filter-map, also fold and scan..
In this case the .net designers capitalized from the popularity of LINQ to SQL and used the same syntax and naming to add this support to .Net.
I also agree that is not necessary to force the language syntax to get the desired results.
But LINQ to Entities and LINQ to SQL are different beasts:
Quoting [1] :LINQ-to-Objects is a set of extension methods on
IEnumerable<T>
that allow you to perform in-memory query operations on arbitrary sequences of objects. The methods accept simple delegates when necessary.LINQ-to-Entities is a LINQ provider that has a set of extension methods on
IQueryable<T>
. The methods build up an _expression_ tree (which is why delegates are actually passed as_expression_<>
s), and the provider will build up a SQL query based on its parsing of that _expression_ tree.The L2E implements a ORM that executes the following workflow:Language syntax -> _expression_ Tree -> Query caching -> SQL text -> database -> serialization -> collection objects in languge.
For example, in this code:
var db = new MyDatabaseContext();
var query =
from c in db.Contacts
join o in db.Orders
on c.ContactId == o.ContactId
join p in db.Payments
on p.Number == p.OrderNumber
where p.DueDate < DateTime.Today()
&& p.PaidValue < o.Amount
select new
{
ContactName = c.Name,
ContactPhone = c.Phone,
OrderNumber = o.Number,
o.Amount,
o.DueDate,
AmountPaid = p.PaidValue
};
foreach(var contact in query)
{
print("%30s %10s %8i %10f %10d %10f",
contact.ContactName,
contact.ContactPhone,
contact.Amount,
contact.DueDate,
contact.AmountPaid
);
}What are the language features needed that I still haven't found in Vala:1) _expression_ Support, type instrospection support
The whole query statement above is not evaluated locally.Instead it builds a _expression_ Tree (AST) and when invocated in foreach loop it builds a SQL Query and execute against a database, object repository, etc...
I couldn't find in vala how to store the _expression_ where p.DueDate < DateTime.Today() && p.PaidValue < o.Amount in a _expression_ tree for using it later for building a SQL query.
2) Type AliasingThe classes db.Contacts, db.Orders and db.Payments are aliased to c, o and p and used after to indicate what class you are refering.
Classes could be refered twice like in Person as teacher and Person as student.Why this is important:
Because it binds the language type safety to the data model you have. Database applications are just boring repetitions of user input forms, reports and data procedures. When you change a table/class the compiler will help you check when the name/type change will break your code. Database aplications usually have hundreds of tables/classes and thousand of queries and type safety cut the much of the maintanance burden.3) Anonymous objectsAfte the query get back the results are materialized/serialized to a new anonymous type with local scope.
In this example the select new statement creates a collection of anonymous objects. The var query gives to the variable this anonymous type.
Why this is important:Because it saves the cuts a lot of code for declaring a new class that will be used once.I hope this could help understand what the benefits of a language integrated ORM for data intensive applications. Some things could add flexibility to language itself.
Other features of LINQ to Entities:a) Lazy loadingpublic class Person
{string name { get; set; }Address address { get; set; }}// get a record from Person tablePerson friend =
from f in db.Personswhere f.Name = "Luca"select f;// Get a record from address table
print(friend.address.street);b) Inheritancepublic class Customer : Person { ... }[1] http://stackoverflow.com/questions/7192040/linq-to-entities-vs-linq-to-objects-are-they-the-same
[2] http://maanshah.wordpress.com/2012/03/02/linq-to-sql-vs-linq-to-objects-vs-linq-to-entities/
2013/12/4 Daniel Espinosa <esodan gmail com>2013/12/3 Juarez Rudsatz <juarezr gmail com>
I checked the samples and some parts of the code.
Nice work. :-)Regarding language support, it seems to remain a good amount of work to be done yet.
Do you mean Vala features right?I'm counting the language features that will be necessary:
- Object serializationAre you mean write some where. For XML serialization we have Vala's GXml project[1]. On GXml's serialization branch, you'll find my work on GObject serialization to XML, comparable with .NET but, I think more flexible[2].
- Anonymous types
- _expression_ and type introspection
- Type aliasing...
- Syntax changes
At the end a Collection have objects with a set of properties, a SELECT _expression_ is a filter of that set of objects and a set of properties. They could be stored in a DbRecord (as for libgda's GdaData [3] ) to save memory and adding something like "Methods_With_Syntax_Support" [4] , Vala compiler could transform:
var selection = select name, salary from employees where id = "123";stdout.printf (@"$(selection.name) / $(selection.salary)");
to:var _tmp_ = new ArrayList<Employee> ();
_tmp_.add_all ( ((Traversable) employees).filter(
(g)=>{ return id.equals (g) true : false; });
var selection = ((Selectable) _tmp_).get_fields ("name", "salary");if (selection.size != 1)throw new SelectableError.MULTIPLE_VALUES_TO_SINGLE ("...");
var _tmp2_ = selection[0].get_field("name");var _tmp3_ = selection[0].get_field("salary");stdout.printf (@"$(_tmp2_) / $(_tmp3_)");
What level of support you are planning?
Because I haven't used LINQ before, I don't know most of its features. Just think about to allow:
a) Use a SQL like syntax to filter objects and properties, by adding support to Vala compiler to generate corresponding Vala or C codeb) Object collection must implement at least Gee.Collection and its items must be Gee.Comparable and Selectable
c) Add a Selectable interface to Gee to allow introspect objects properties and create a Collection with desired DbRecord set of values.I don't think mimic LINQ is correct. May we can do better, but is necessary to define use cases.
The only one I have in mind is:a) select {list-of-fields} from {collection-object} where {condition}For this simple _expression_ Vala's compiler should generate required code to:
a) Check for collection-object is a GObject implementing Gee.Collection and its elements must be Gee.Comparable and Selectable
b) Check for list-of-fields are public properties in collection-objectc) Check for condition is using collection-object 's public propertiesd) Generate a Predicate<G> delegate function to find object required objectse) Fill a Gee.Collection with filtered objects
f) Generate a Gee.Collection from Selectable, that produce rows and values/columns (with name=properties' name)It would be well received by upstream developers?
Really don't know. I'm CC to Vala and Gee lists to hear them about this idea.I'm planning a new architecture for ouro next applications and considering vala. But the type safety and language support are the strengths of linq we really appreciate.
I'm planning to help someway, but I have a long way yet to be produtive.
Regards
Juarez
Em 03/12/2013 16:35, "Daniel Espinosa" <esodan gmail com> escreveu:You can see a preview of my work on current stable libgda.
I've finished interfaces and implementations under GdaData name space.
I've added some documentation just use --enable-vala-extensions --gtk-doc (some other switches are required) to see it installed.
Missed Selectable interface or implementation.
I would like to add syntax support on Vala compiler in order to have LINQ like one.
For now is very easy to access to data using GdaData objects, see unit tests for more examples.
El dic 3, 2013 10:03 a.m., "Juarez Rudsatz" <juarezr gmail com> escribió:What's missing yet? How is the work for getting basic support ?I see that you have made good progress regarding this support.Hi,I found that you have been working in a possible implementation of LINQ in vala [1].
I want to know what is the current status.
--
Trabajar, la mejor arma para tu superación
"de grano en grano, se hace la arena" (R) (en trámite, pero para los cuates: LIBRE)