75
www.sungard.com/consulting C# 3.0 and LINQ: Tech Talk Michael Heydt Senior Manager, NYC FST SunGard Consulting Services October 2008

C# 3.0 and LINQ Tech Talk

Embed Size (px)

Citation preview

Page 1: C# 3.0 and LINQ Tech Talk

www.sungard.com/consulting

C# 3.0 and LINQ: Tech Talk

Michael HeydtSenior Manager, NYC FSTSunGard Consulting ServicesOctober 2008

Page 2: C# 3.0 and LINQ Tech Talk

Agenda

New C# 3.0 Constructs What is and why use LINQ? LINQ “Flavors” Architecture Examples / Demos Adoption Strengths and Weaknesses Closing

Page 3: C# 3.0 and LINQ Tech Talk

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

Page 4: C# 3.0 and LINQ Tech Talk

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

Page 5: C# 3.0 and LINQ Tech Talk

Automatic Properties

Page 6: C# 3.0 and LINQ Tech Talk

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

Page 7: C# 3.0 and LINQ Tech Talk

Object and Collection Initializers

Page 8: C# 3.0 and LINQ Tech Talk

Object and Collection Initializers

Collection Initialization– Could not do this before:

Page 9: C# 3.0 and LINQ Tech Talk

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

Page 10: C# 3.0 and LINQ Tech Talk

Inferred Types

Page 11: C# 3.0 and LINQ Tech Talk

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

Page 12: C# 3.0 and LINQ Tech Talk

Anonymous Types

Page 13: C# 3.0 and LINQ Tech Talk

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

Page 14: C# 3.0 and LINQ Tech Talk

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.

Page 15: C# 3.0 and LINQ Tech Talk

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.

Page 16: C# 3.0 and LINQ Tech Talk

Partial Methods

Page 17: C# 3.0 and LINQ Tech Talk

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 };

Page 18: C# 3.0 and LINQ Tech Talk

Lambda Methods

Page 19: C# 3.0 and LINQ Tech Talk

Lambda Methods in a Query

Page 20: C# 3.0 and LINQ Tech Talk

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

Page 21: C# 3.0 and LINQ Tech Talk

Extension Methods

Page 22: C# 3.0 and LINQ Tech Talk

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)

Page 23: C# 3.0 and LINQ Tech Talk

Query Expressions

Page 24: C# 3.0 and LINQ Tech Talk

Query Expressions - Example

Page 25: C# 3.0 and LINQ Tech Talk

Query Expressions - Example

Page 26: C# 3.0 and LINQ Tech Talk

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

Page 27: C# 3.0 and LINQ Tech Talk

Expression Trees

Page 28: C# 3.0 and LINQ Tech Talk

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

Page 29: C# 3.0 and LINQ Tech Talk

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

Page 30: C# 3.0 and LINQ Tech Talk

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

Page 31: C# 3.0 and LINQ Tech Talk

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

Page 32: C# 3.0 and LINQ Tech Talk

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

Page 33: C# 3.0 and LINQ Tech Talk

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

Page 34: C# 3.0 and LINQ Tech Talk

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

Page 35: C# 3.0 and LINQ Tech Talk

LINQ Ecosystem

Page 36: C# 3.0 and LINQ Tech Talk

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

Page 37: C# 3.0 and LINQ Tech Talk

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

Page 38: C# 3.0 and LINQ Tech Talk

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

Page 39: C# 3.0 and LINQ Tech Talk

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.

Page 40: C# 3.0 and LINQ Tech Talk

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

Page 41: C# 3.0 and LINQ Tech Talk

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

Page 42: C# 3.0 and LINQ Tech Talk

LInQ Query Structure

Page 43: C# 3.0 and LINQ Tech Talk

All the same Query

Comprehension Query

Lambda Queries

Page 44: C# 3.0 and LINQ Tech Talk

What’s Going On?

A progressive filtering of data through the parts of the queryTriggered on the enumerators

Page 45: C# 3.0 and LINQ Tech Talk

Local Query Execution and Deferral

The query becomes a set of enumeratorsdecorated by a different type of functionality and criteria

Page 46: C# 3.0 and LINQ Tech Talk

Subqueries

Page 47: C# 3.0 and LINQ Tech Talk

Subqueries

Page 48: C# 3.0 and LINQ Tech Talk

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

Page 49: C# 3.0 and LINQ Tech Talk

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

Page 50: C# 3.0 and LINQ Tech Talk

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

Page 51: C# 3.0 and LINQ Tech Talk

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

Page 52: C# 3.0 and LINQ Tech Talk

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

Page 53: C# 3.0 and LINQ Tech Talk

DBML

Page 54: C# 3.0 and LINQ Tech Talk

Generated Code (1)

Page 55: C# 3.0 and LINQ Tech Talk

Generated Code (2)

Page 56: C# 3.0 and LINQ Tech Talk

Using XML Configuration

Page 57: C# 3.0 and LINQ Tech Talk
Page 58: C# 3.0 and LINQ Tech Talk

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

Page 59: C# 3.0 and LINQ Tech Talk

LINQ to XML Example – DB to XML

Page 60: C# 3.0 and LINQ Tech Talk

Another

Page 61: C# 3.0 and LINQ Tech Talk

XML to Objects

Page 62: C# 3.0 and LINQ Tech Talk

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

Page 63: C# 3.0 and LINQ Tech Talk

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)

Page 64: C# 3.0 and LINQ Tech Talk

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

Page 65: C# 3.0 and LINQ Tech Talk

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

Page 66: C# 3.0 and LINQ Tech Talk

Futures

Parallel LInQ– http://channel9.msdn.com/shows/Going+Deep/Inside-

Parallel-Extensions-for-NET-2008-CTP-Part-1/

Page 67: C# 3.0 and LINQ Tech Talk

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

Page 68: C# 3.0 and LINQ Tech Talk

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

Page 69: C# 3.0 and LINQ Tech Talk

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

Page 70: C# 3.0 and LINQ Tech Talk

Contact

Michael HeydtSenior Manager(610) 772-5556 [email protected]@heydt.orgwww.42spikes.com

Page 71: C# 3.0 and LINQ Tech Talk

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.

Page 72: C# 3.0 and LINQ Tech Talk

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.

Page 73: C# 3.0 and LINQ Tech Talk

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.

Page 74: C# 3.0 and LINQ Tech Talk

Group By

Important to note that this really returns a dictionary

Page 75: C# 3.0 and LINQ Tech Talk

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