This is the second of three lectures on integrating domain-specific languages with general-purpose programming languages. In particular, SQL for database queries. This lecture presented LINQ, Language Integrated Query, together with some background on Microsoft’s .NET framework, and the language features added to C# to support LINQ.
The last lecture showed how established frameworks like JDBC for accessing databases from programs do this by building SQL queries through string manipulation. This fails to take advantage of the rich high-level structure of SQL as a language: missing opportunities for static checking, compile-time optimization. It is also the root cause of the security vulnerabilities by SQL injection, as sanitizing user input becomes essential but also difficult.
SQL queries are programs in a structured high-level language, but we treat them as unstructured text.
This lecture looked at LINQ, Language Integrated Query, a recent addition to the Microsoft .NET framework that begins to bridge this gap between languages. There was some background on the nature of .NET, and in particular its approach to supporting development in multiple programming languages. Treating LINQ in particular, there was a direct comparison of query construction in JDBC/ADO.NET (with strings) and in LINQ (with structured data). LINQ exposes queries to static checking, IDE support and compile-time optimization by making them part of the host language.
This language/query integration goes deep: queries become meaningful data structures in the host language, not just raw strings of syntax.
Creating LINQ, though, required various extensions to the C# language. We briefly looked through them: lambda expressions; free-standing method declarations; structural datatypes; anonymous record types; and type inference. These are new to C#, but based on well-established concepts from other existing languages. Moreover, all are useful in other contexts, and genuinely enrich the programming language. In particular, LINQ uses first-class expressions, which lead into structured reflection and metaprogramming, the topics of the next lecture.
Read the “Introduction to LINQ” piece from this MSDN Library text:
- Visual Studio 2010: LINQ (Language-Integrated Query)
Tuesday’s lecture will be about metaprogramming in F#:
- Find an online tutorial about F#
- Post its URL as a comment below
The final slide from the lecture contains additional references which you might find useful.