Older (2008) presentation I gave internally to SunGard to educate developers on C# and LINQ. LINQ still rocks, and the concepts I cover are important language features while C# developers should be asked in interviews event today.
4. Automatic Properties
Reduces coding on most property declarations
Used by LINQ to handle projection into anonymous
classes
As a matter of fact, this is critical to anonymous class
definition
6. Object and Collection Initializers
Allow assignment of fields or properties of an object
at creation time without having to explicitly invoke a
constructor
Used to define and initialize properties in anonymous
classes by LINQ
– Anonymous types are difficult, if not impossible, to initialize
without them
– Potential or exponential combinations of constructors
Is very handy for collection initialization
8. Object and Collection Initializers
Collection Initialization
– Could not do this before:
9. Inferred Types
The type of the variables is inferred from the data
assigned to it
Utilizes the “var” keyword
Strongly typed, not like in JavaScript
Required to support LINQ creating anonymous types
as query results using automatic property initialization
Can greatly simplify code as query results fields can
be changed without having to recode an intermediate
query results class
Local only
– Cannot be a member or returned from a method
– Compiler cannot infer outside of local context
11. Anonymous Types
A type automatically inferred and created from object
initializers
Very useful for projections in query results
Requires inferred types
Compiler generates a class under the covers
– And default equivalence methods
– Types are considered the same if there are the same
properties and types
Class has no methods
13. Partial Methods
Allows definition of a method, and someone else to
implement
If not implemented, no IL is generated
Useful for customizing generated code
A good example is pre and post validation calls
Used extensively in the LINQ to SQL designer for
property changing and data validation events
14. Partial Methods
Partial method declarations must begin with the
contextual keyword partial and the method must
return void.
Partial methods can have ref but not out parameters.
Partial methods are implicitly private, and therefore
they cannot be virtual.
Partial methods cannot be extern, because the
presence of the body determines whether they are
defining or implementing.
Partial methods can have static and unsafe modifiers.
15. Partial Methods
Partial methods can be generic. Constraints are put
on the defining partial method declaration, and may
optionally be repeated on the implementing one.
Parameter and type parameter names do not have to
be the same in the implementing declaration as in the
defining one.
You cannot make a delegate to a partial method.
17. Lambda Methods
Integral part of LINQ for specifying queries
Replace C# 2.0 anonymous delegates
Gives cleaner syntax for defining delegates
– No need to declare a delegate
– Just define the anonymous method at the point you want to
use it
Leads to great reductions in code complexity
Format:
– ( parameters ) => { code };
20. Extension Methods
A means of defining extensions to existing types
Allows the appearance of adding new methods to
classes which you don’t have access to the source
Also used extensively for code generation of
extensibility points
Used by LINQ to add query methods to existing
framework classes via System.Link assembly
Declared within static classes using the this keyword
Can be used to extend sealed classes
22. Query Expressions
Query expressions add SQL like syntax to the
language
Allows specification of queries without using external
tools or syntax
Components
– From: Defines what data to start with
– Where: Conditional selection
– Select: Projects the result set
Supports concepts of joins
– Use multiple “from”’s
– Basically creates nested loops (but a little more elaborate)
26. Expression Trees
Query expressions are converted into expression
trees
An expression tree is a data structure representing
an expression
You can also build expressions trees dynamically
28. What is LInQ?
LInQ = Language Integrated Query
Internal DSL to .NET Languages for querying objects
Many consider it an ORM. It is not an ORM
– But LINQ 2 SQL is an extension that provides ORM
capabilities
It is much more than an ORM
– Integrated query language: runtime and compiler
– Query Optimization
– Object “projection”
LINQ syntax looks like SQL, but it is much different
29. What is LINQ?
It provides for allows query across any enumerable
object collections
– Just about any IEnumerable<T> can be used in queries
– If the backing store is not understood by the compiler, you
can build a “provider” to explain it.
Provides a standard means of querying collections of
CLR objects, databases (RDBMS) and XML
Provides for compile-time query development and
syntax checking which leads to less errors
30. What is LInQ’s Value Proposition?
No context shift when needing to query collections of
objects to find the object that you want
Not just an ORM
– LInQ to SQL does do that, but it really is about managing
query, construction and update of objects independent of
storage (memory, document, file or RDBMS)
An attempt to provide a single means of query,
instead of:
– SQL for RDBMS
– XPath and XQuery for XML
– Custom code for objects
Identification of query errors at compile time instead
of run-time
Rapid development of queries through tool support
31. How can LInQ Make you more Effective?
Quicker time to deployment
– Rapid development
Less errors
– Type safe query based upon object and database metadata
Performance
– Optimized at the provider level
– Integrated into the language and runtime, so no context shift
at runtime
Single Model for Query
– Similar query structure for all data types
Less code
– Write less query code
– Also can be used to facilitate object construction which can
greatly reduce code base
32. Competition (1)
Nhibernate
– Open source ORM tool, very popular, based on Hibernate
– Proprietary query language, no language integration
– Unweildy XML configuration files
– Realy only competes with LINQ to SQL
CSLA
– .NET framework for object distribution and database
mapping offered by Rocky Lhotka
– Has some traction, but tries to solve many issues and is
relatively weak in ORM capabilities
– Kind of an ORM, but no real built in query
33. Competition (2)
ActiveRecord
– Design pattern for representing rows of tables in and ORM
– Comes from Fowler, very popular in Ruby on Rails
– Limited to operations on single tables
– Primary implementation is Castle ActiveRecord
– No integrated query
Rhino-Commons
– Open source extensions to Castle ActiveRecord (and Rhino
Mocks)
– Big part of the Alt.NET toolset
– Makes ActiveRecord even easier
– Also no query
34. Competition (3)
Microsoft Entity Framework
– Microsoft’s higher level data abstraction model
– Uses LInQ under the covers for query and physical table
mapping
– First Generation
– Uses LINQ for query, but real functionality is in providing
multi-database and object partitioning across multiple
physical sources
36. Flavors of LInQ
LInQ to Objects
– Can query any IEnumerable set of object(s)
– Uses runtime metadata
LInQ to SQL
– Requires adornment of objects with attributes to provide
mapping to RDBMS
LInQ to DataTable
– Provides ability to query data returned from ADO.NET via
DataTable object without having to use the built in and
difficult to use query constructs
LInQ to XML
– XDocument class is LInQ aware, allowing query of XML with
LInQ syntax instead of Xpath
LInQ to Entities
– Provides query across Entity Framework Objects
37. LInQ Architecture (1)
Required extensions to the CLR languages for:
– Lambda functions
– Partial Methods
– Extension methods
– Collection initializers
– Type inference
– Object Initializers
– Automatic Properties
– Anonymous Types
– Expression Trees
38. LInQ Architecture (2)
Query structure - Comprehension Model
– From (where is the data coming from)
– Where (filter out on a criteria)
– Select (and select only the data you need)
– Join, group, orderby, …
This is an internal DSL that is translated into method
calls
39. LInQ Architecture (3)
Provider model (the context)
– Can root queries and be used to
Track object changes
Execute transactions
Generate native queries
Defer execution or eager load data
– Important note:
Change tracking is optimistic, and
Requires projection of the full table objects (all
columns). Partial classes are not supported as
missing data may be changed by a third party.
40. LInQ Architecture (4)
Lambda Expressions
– Comprehension is converted to the compiler to this format
Query is typically not executed until…
– LInQ query IS NOT executed at declaration
– The variable is simply a holder for the expression
– Query IS started when the variable is iterated
This is very important!
– It can be confusing at first
41. How do you use LInQ?
Typically, you write a query in comprehension mode
– The compiler converts it to Lamba format
In LinQ to objects
– A series of decorators is created that feeds… data from one
to the next, each triggered by enumeration
In LInQ to SQL
– SQL is created and data is mapped to CLR objects
– Chain of decorators may be handled explicitly by the SQL
In LInQ to XML
– XPath is generated to manipulate the DOM
Objects are then “Projected” into results
– Can be a strongly typed class, or an anonymous class
– Can contain all properties of the selected objects, or just a
subset
48. LINQ to SQL
Operates using a data context object
– Entry point to the database
– Performs change tracking
Attribute base declaration of mapping of classes to
database tables
Automatic identification of primary/foreign keys for
relationships
49. LINQ to SQL
If using the designer, you get:
– automatic creation of extension point for data validation and
property change
– Ability to map insert/delete/update/query to stored
procedures
– Automatic handling of updates of key relationships
– Automatic transaction processing
– Server side paging
50. If you don’t use the designer
You can manually annotate classes
– And you must implement the other features like property
change notification yourself
Or you can also use XML configuration files
– But there is not a “fluent” interface at this point
51. LINQ to SQL
DataContext class can be extended with partial
classes to:
– Override default generation of SQL into stored procs
– Override default generation of SQL into custom SQL
– Objects returned by either of these will also participate in
change management
This is very powerful as it allows:
– Rapid development of queries first using comprehension
queries
– Later migration of queries into stored procs
– Data access code will remain the same
– Data validation code will still be valid in either context
52. LINQ to SQL
Other items of importance
– AsQueryable vs AsEnumerable
– Optimistic locking / ChangeConflicts
Coupling
– DBML tight couples to database, but is easy
– XML loose couples, but you have to code more
Stored procedures?
– Can be used for CRUD on DBML objects
– Or called directly as methods on the data context
58. LINQ to XML
System.Xml.Linq namespace
XDocument class
– Provides new XML API that is LINQ aware
– Other classes: XElement, XAttribute, XNode
You can:
– Create XML documents from the results of queries
– Query XML documents and return XML or objects
Can be used to provide a natural query language to
XML documents instead of Xpath/Xquery
62. LInQ Demonstration(s)
A basic query structure on strings (linq to objects)
Query across objects
– summing data
– grouping and projecting
Mapping objects to RDBMS
RDBMS query
RDBMS relationships (lazy and eager load)
RDBMS update
Multi-result / multi-shape query
Data binding to anonymous query results
Simplification of XML manipulation
– Query data
– Create documents
63. Maturity
Available for 3+ years in CTP form
Released in .NET 3.0 (~ fall 2007)
Rock solid with Linq to Objects, SQL Server
No current support for Oracle from either MS or
Oracle (there is a codeplex project)
64. Strengths and Weaknesses
Strengths
Standard object query
language
Tool integration
Pervasive
Deferred execution
Great model for
manipulating objects
instead of explicit code
Easier configuration
compared to other tools
Weaknesses
Not good support for ad-
hoc queries
Currently only supports
SQL Server
It is a new tool to learn
Different from SQL
Tight coupling to SQL
database
65. Adopting LInQ
Start with query over objects
Move to XML query / creating XML objects
Use for rapid development, query development, unit
testing (agile development)
Teach to developers and data analysts
Create role of “business object designer” to work with
DBA’s to build data access layers in LInQ combined
with the efforts of the DBA’s.
Release dal’s to programmers to use as a unified
object model.
Migrate queries to CRUD stored procedures
67. Learning LInQ
Books
– Apress: Pro LINQ: Language Integrated Query in C# 2008
– Apress: Pro LINQ Object Relational Mapping in C# 2008
– Oreilly: LINQ: The Future of Data Access in C# 3.0
Web sites
– Scott Guthrie: http://weblogs.asp.net/scottgu
– Bill Wagner: http://srtsolutions.com/blogs/billwagner/
– www.hookedonlinq.com
Podcasts
– Dotnetrocks.com
– DNRTV.com
69. Q&A
Solicit general customer questions about LInQ (be
prepared for these – think ahead of them)
– I’m scared of not using stored procs…
– I’m not using that level of .NET yet…
– How does this fit with legacy systems?
– What about my DBA’s?
– Who else is using LInQ? What have their experiences
been?
– How does LInQ fit into agile?
Proactively solicit information on LInQ from customer
about how they can use LInQ
– Look for a few strategic inroads to follow… Such as:
– Training / mentoring
– Proof of concepts
– Ask them how they think they can use it
72. Elevators
The LINQ Project is a codename for a set of
extensions to the .NET Framework that encompass
language-integrated query, set, and transform
operations. It extends C# and Visual Basic with native
language syntax for queries and provides class
libraries to take advantage of these capabilities.
73. LINQ defines a set of query operators that can be used to query,
project and filter data in arrays, enumerable classes, XML,
relational database, and third party data sources. While it allows
any data source to be queried, it requires that the data be
encapsulated as objects. So, if the data source does not
natively store data as objects, the data must be mapped to the
object domain. Queries written using the query operators are
executed either by the LINQ query processing engine or, via an
extension mechanism, handed over to LINQ providers which
either implement a separate query processing engine or
translate to a different format to be executed on a separate data
store (such as on a database server as SQL queries). The
results of a query are returned as a collection of in-memory
objects that can be enumerated.
75. Notes
Lazy load required EntityRef
XML Mapping files can be used
LINQ grammer is not the same as SQL
Right outer joins must be flipped to left