TechHui

Hawaii's Science, Technology and New Media Community

Microsoft released VisualStudio 2008 last week. We now have a production version of C# 3.0 with LINQ. Using LINQ is much nicer than dealing with queries embedded in Strings. I really think Java is in dire need of something like LINQ. Thoughts?

Tags: c#, java, linq

Reply to This

Replies to This Discussion

I'd never heard of LINQ before, but it does look like something Java needs. Thanks for pointing it out. It reminds me of "list comprehensions" that I've seen in some examples of more functional languages. (I don't recall off-hand which language it was, though; Scala? JavaFx? JavaScript 1.7? Ruby?) But maybe the LINQ join goes beyond that?

Nice how they added it to their ORM framework too. I think IntelliJ has a plug-in that adds some language support for SQL strings, altho I'm not using SQL now and it sounds clunky so I haven't tried it. IntelliJ has some Hibernate support now too, but I haven't noticed any language help on the HQL strings.

Reply to This

> But maybe the LINQ join goes beyond that?

It does. It provides a generic way of performing queries over any datastructure including databases, in-memory collections, XML documents etc. I really like the way its integrated with the C# language.

> IntelliJ has some Hibernate support now too

I haven't looked at IDEA in a while. I'll have to give the latest release a spin.

Reply to This

Does LINQ provide static typing for those queries, e.g., based on database or XML schema? If not, then I wonder how much nicer it is than the stringy query DSLs like SQL or HQL.

Reply to This

Yes, LINQ produces strongly typed results when the type is knowable.
Example:

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
IEnumerable<int> lowNums = from n in numbers
    where n < 5 select n;

Using type inference I could also simply write this:

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = from n in numbers
    where n < 5 select n;

Note this is not loose typing. Low nums is a typed enumeration in both cases.

Reply to This

That is cool, but I was wondering in particular whether and how LINQ knows the static types of the symbols in a database or XML query (such as SQL or XPath).

Reply to This

For databases it can simply use the column type. Lots of classes in .NET do this including the strongly typed DataTable.

LINQ to XML is not strongly typed, but there is a LINQ to XSD in beta which allows you to query XML with strong typing.

Reply to This

I sent this email to Dan and he asked that I post it here so here you go ...

I was a co-inventor of LINQ to XML and was involved with Anders Hejlsberg in the LINQ project itself. I also created LINQ to XSD with another guy Ralf Lammel along with a few developers. LINQ to XSD was highly influenced by a technology I worked on at BEA that you may have heard of called XMLBeans which was donated to Apache. The main differentiator between the XMLBeans and the LINQ to XSD approach versus other OXM technologies is that it does not shred the XML into domain objects (e.g., JAXB, Microsoft’s xsd.exe) which is typically lossy (at least without a lot of complexity) but it is a typed layer that sits on top of the XML store. LINQ to XSD sits on top of LINQ to XML (which shows you how poorly LINQ to XML is named*). XMLBeans also sits on top of a store (XmlStore I think it is called) and implements the DOM API. In either at any time if you need to you can drop down to the raw XML (in LINQ to XSD using the LINQ to XML API, in XMLBeans using the DOM or XmlCursor API) and do whatever you need to do. There are pros and cons to this approach but the big pro is that you can handle 100% XML Schema features (at least conceptually) and be completely non-lossy (which enables round-tripping scenarios like UIs over config files). It is also particularly well suited for message oriented web services scenarios where an XML Schema defines the message structure rather than .NET or Java generated WSDL.

I just started over as CTO at UBoost but the year before that I worked at Microsoft as a developer I got the opportunity to work with LINQ and LINQ to XML on a new XML Schema Designer that Microsoft is planning in Visual Studio. We found that LINQ has a significant positive impact in the way you write code. Many methods involve taking in data from a variety of sources, transforming it in some way, and then outputting in some way. When you write code in LINQ you can sort of outline your intent in a query and then make method calls to handle specific, arbitrarily complex, logic. It makes the code both more readable and more concise.

The other thing about LINQ that is interesting is that, in order to do LINQ, as set of underlying capabilities had to be added to the .NET languages. These capabilities by themselves probably have as much or bigger impact than LINQ. I think there are 5 of them that LINQ was built off of ... 1) anonymous classes (local scoped classes can be newed up without a name - needed to easily do a select in a query), 2) lambdas (essentially a much easier to use delegate used for predicates in queries and other), 3) extension methods (allows you to add instance methods to an existing class from outside the class itself, this was needed to extend IEnumerable which allowed a ton of existing APIs to immediately be queryable and still not be backwards breaking), 4) object intializers (ability to new up a class and immediately assign values to properties e.g., Customer c = new Customer() {name = "Fred"} 5) The var type holder which allows you to put var on the left hand side as the type when declaring a variable (e.g., var c = new Customer, or more importantly var = from c in Customers/where c.name == "Fred"/select new {c.name.reverse} - var allows was needed to hold an anonymous class). Of course causes no end to confusion since, as you mention in your post, everything is still strongly typed it is just the inference is being used to determine the left hand side declaration type.

I think the LINQ and these 5 new language features are a challenge that Java needs to respond to. I love Java and the Java platform which I was involved a lot longer than .NET but I was also involved in the JCP process when I was at BEA and I worry about whether Java is going to be able to respond to the times. At UBoost we are moving over to Ruby, most likely JRuby to take advantage of the Java platform. Ruby has many of the capabilities that .NET 3.5 is coming out with. Perhaps that's where the language innovation will come from with the Java becoming more of a platform? It will be interesting to see.

Reply to This

Thanks for that great explanation, Dave! It really helped me understand LINQ to XSD. I found XmlBeans and used it at a startup 4 years ago. I liked it so much that I gave a presentation about it to the Honolulu Java User's Group. So, I can imagine how typing works in LINQ and how it helps development.

Those 5 supporting features added to .NET sound compelling, too. On the JVM, I wonder whether Java will keep up or some other language will overtake it. Are you moving to Ruby because of Rails? I'm trying to decide which JVM language to learn now.

Reply to This

Yep, we are moving to Ruby because of Rails. I like Rails a lot for web development. It has built in ORM, MVC, Ajax integration, Unit testing, Integration testing, even dependency injection type patterns that Spring supports (although done quite a bit differently). There is a great podcast that David Koontz did at the Phoenix Java Users group about Ruby on Java here. He makes the argument that Java will continue to move slowly and that the recent changes in Java 6 supporting dynamic languages will result in Java being more of a platform and JRuby becoming a significant player in the Java (as a platform) space.

Reply to This

Thank you for the great summary of LINQ to XSD.

> The other thing about LINQ that is interesting is that, in order to do LINQ,
> as set of underlying capabilities had to be added to the .NET languages.

Exactly. One of the reasons I would really like to see LINQ in Java is the need for these language features. Recently I have been working with Groovy, Ruby, Python and C#. No when I am coding in Java I really miss things like object initializers.

Reply to This

Groovy appears to have similar features. The GORM library (part of Grails, but can be used without Grails) is Groovy's/Grails' object relational mapping (ORM) implementation.

Reply to This

For a typesafe LINQ to Java approach consider Querydsl : http://source.mysema.com/display/querydsl/Querydsl

Querydsl supports JPA/Hibernate, JDO, SQL and Java Collections.

Reply to This

Reply to This

RSS

Sponsors


web design, web development, localization


Spread Firefox Affiliate Button

© 2010   Created by Daniel Leuck

Badges  |  Report an Issue  |  Privacy  |  Terms of Service