Upload
michael-heydt
View
196
Download
0
Embed Size (px)
Citation preview
www.sungard.com/consulting
C# 3.0 and LINQ: Tech Talk
Michael HeydtSenior Manager, NYC FSTSunGard Consulting ServicesOctober 2008
Agenda
New C# 3.0 Constructs What is and why use LINQ? LINQ “Flavors” Architecture Examples / Demos Adoption Strengths and Weaknesses Closing
C# 3.0 New Constructs
Automatic Properties Object and collection Initializers Inferred types Anonymous types Partial Methods Lambda methods Extension methods Query expressions Expression Trees
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
Automatic Properties
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
Object and Collection Initializers
Object and Collection Initializers
Collection Initialization– Could not do this before:
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
Inferred Types
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
Anonymous Types
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
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.
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.
Partial Methods
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 };
Lambda Methods
Lambda Methods in a Query
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
Extension Methods
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)
Query Expressions
Query Expressions - Example
Query Expressions - Example
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
Expression Trees
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
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
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
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
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
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
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
LINQ Ecosystem
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
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
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
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.
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
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
LInQ Query Structure
All the same Query
Comprehension Query
Lambda Queries
What’s Going On?
A progressive filtering of data through the parts of the queryTriggered on the enumerators
Local Query Execution and Deferral
The query becomes a set of enumeratorsdecorated by a different type of functionality and criteria
Subqueries
Subqueries
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
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
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
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
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
DBML
Generated Code (1)
Generated Code (2)
Using XML Configuration
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
LINQ to XML Example – DB to XML
Another
XML to Objects
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
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)
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
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
Futures
Parallel LInQ– http://channel9.msdn.com/shows/Going+Deep/Inside-
Parallel-Extensions-for-NET-2008-CTP-Part-1/
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
Tools
Visual LINQ: http://code.msdn.microsoft.com/vlinq LINQPad: http://www.linqpad.net/ LINQ Debug Visualizer:
– http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
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
Contact
Michael HeydtSenior Manager(610) 772-5556 [email protected]@heydt.orgwww.42spikes.com
CONFIDENTIALITY STATEMENT
Copyright © 2008 by SunGard Data Systems Inc. (or its subsidiaries, “SunGard”). All rights reserved. No parts of this document may be reproduced, transmitted or stored electronically without SunGard’s prior written permission.
This document contains SunGard's confidential or proprietary information. By accepting this document, you agree that: (A)(1) if a pre-existing contract containing disclosure and use restrictions exists between your company and SunGard, you and your company will use this information subject to the terms of the pre-existing contract; or (2) if no such pre-existing contract exists, you and your Company agree to protect this information and not reproduce or disclose the information in any way; and (B) SunGard makes no warranties, express or implied, in this document, and SunGard shall not be liable for damages of any kind arising out of use of this document.
About SunGard Consulting Services
SunGard Consulting Services’ deep understanding of business processes in financial services and energy, together with our expertise in information technology and knowledge of SunGard products, helps us mitigate project risk and provide a single point of accountability for SunGard projects that differentiates us from other consulting organizations.
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.
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.
Group By
Important to note that this really returns a dictionary
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