450 likes | 605 Views
Developing Windows and Web Applications using Visual Studio.NET. Adam Stephensen. Session 1: Homework?. Session 1: Last week. What is .NET? What is .NET framework? What is the CLR? OOP Principles? Encapsulation, Abstraction, Inheritance, Polymorphism
E N D
Developing Windows and Web Applications using Visual Studio.NET Adam Stephensen
Session 1: Last week • What is .NET? • What is .NET framework? • What is the CLR? • OOP Principles? • Encapsulation, Abstraction, Inheritance, Polymorphism • Classes, Objects, Properties, Events, Methods?
Session 2: Overview Agenda • C# 3.0 & C# 4.0 Language Enhancements • What is LINQ? • LINQ Project Goals and Architecture • Writing LINQ • Data Binding with LINQ
C# 3 2008
C# 3.0 Language Enhancements • Local Variable Type Inference • Anonymous Type • Lambda Expressions • Extension methods • Object initializers • Query Expressions
Anonymous Type • Fits with complex temporarily variables • E.g. I want a temp variable to hold username & password
Extension methods Declaration • Extend existing classes by creating a static `helper` method Implementation
Object Initializers • Object initializers allow values to be assigned to fields and properties as an object is instantiated.
Query Expressions • Query expressions allow the querying of objects in a similar syntax to that of SQL
C# 4 April 2010
C# 4 Language Enhancements • The ‘dynamic‘ keyword • Optional parameters • Named parameters • Covariance and Contravariance
Dynamic Keyword • When the dynamic keyword is used the ‘type’ of a variable is determined at runtime
Optional Parameters • Optional parameters have a default specified and can be omitted when calling the method
Named Parameters • Named parameters allow assignment of a value with an argument based on its name rather than the order in which it is passed
.NET 3.5 • Auto Implemented Properties • Collection Initialisers (limited) • Named parameters • Optional parameters • Lambdas (limited) • Can span statements across multiple lines • Auto Implemented Properties • Collection Initialisers • Named parameters • Optional parameters • Lambdas • Can span statements across multiple lines
.NET 4 • Auto Implemented Properties • Collection Initialisers • Named parameters • Optional parameters • Lambdas • Can span statements across multiple lines • Auto Implemented Properties • Collection Initialisers • Named parameters • Optional parameters • Lambdas • Can span statements across multiple lines
Data access Show me the data!
What is LINQ? • First showcased at the American International Toy Fair in New York in 2005 • LINQ, a clever bluffing game
What is LINQ? • LanguageINtegratedQuery • Allows you to easily query information about an object • LINQ to SQL: An ORM made by Microsoft • LINQ to Entities: Another ORM made by Microsoft • LINQ to XML: Query over XML • ...
What is LINQ? • Language syntax • A library of extension methods • Lambda expressions • Anonymous types • A little magic
LINQ - How does it solve the problem? • LINQ is a natural language extension, part of .NET 3.5 • Different data sources are queried with the same syntax. • Queries are strongly typed • Queries benefit from “Intellisense” • Applications becomes less error prone
LINQ Project Goals • Unified programming model for any data type • Database Relational Data • XML Files • Collections & Arrays • Introduce more declarative syntax • Helps the system find the best execution strategy
C# VB Others... LINQ Project Architecture .NET Language Integrated Query (LINQ) LINQ Enabled Data Sources LINQ Enabled ADO.NET LINQ to Objects LINQ to ??? LINQ to XML LINQ to Datasets LINQ to SQL LINQ to Entities <book> <title> <author/> <price /> </title> </book> Objects Database XML
LINQ Query Syntax vs Method Syntax Query Syntax Extension Methods
Standard Data Access Code stringstrSQL = “SELECT custID FROM Customers“; SqlCommandcmd = new SqlCommand(strSQL, cnn); SqlDataAdapteradAdapter = new SqlDataAdapter(cmd); DataSetdst = new DataSet(); adAdapter.Fill(dst);
Code by LINQ NorthwindDataContext db = newNorthwindDataContext(); var customers = from c indb.Customers select c;
LINQ • Benefits from LINQ • LINQ data binding
LINQ 2 SQL • Stored procedures are easier to work with • Relationship editor UI is nicer (more like SQL Server) • Performance • In a recent benchmark LINQ to SQL performed 5 times faster than LINQ to Entities
LINQ 2 Entities • More refined ORM than LINQ to SQL • Many to Many relationships • Richer support for Inheritance • Validation • Update model from database • Mapping Editor
What to choose? Do you know why you should choose "LINQ to Entities" over "LINQ to SQL"? http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx#WhyLINQtoEntitiesNotSQL
Hot Tip • LINQ Pad • http://www.linqpad.net/
Summary • C# 3.0 & C# 4.0 Language Enhancements • Local Variable Type Inference (var) • Anonymous Types • Lambda Expressions • Extension methods and Query Expressions • Object initializers • The ‘dynamic‘ keyword, • Optional and Named parameters • What is LINQ? • LINQ Project Goals and Architecture • Writing LINQ • LINQ Query Syntax • LINQ Method Syntax • Data Binding with LINQ • LINQ to SQL • LINQ to Entities
Resources • Introduction to LINQhttp://msdn.microsoft.com/en-us/library/bb397897.aspx • Getting Started with LINQ in C#http://msdn.microsoft.com/en-us/library/bb397933.aspx • LINQ Term and more detailshttp://www.hookedonlinq.com/ • LinqPadhttp://www.linqpad.net/ • Linq tutorialshttp://language-integrated-query.com/
Resources • LINQ Query Syntax versus Method Syntax http://msdn.microsoft.com/en-us/library/bb397947.aspx • Object Initializershttp://msdn.microsoft.com/en-us/library/bb384062.aspx • Query Expression Basicshttp://msdn.microsoft.com/en-us/library/bb384065.aspx • Hooked in LINQHookedOnLINQ.com
3things… • AdamStephensen@ssw.com.au • http://adamstephensen.com/ • twitter.com/adamstephensen
Thank You! Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA ABN: 21 069 371 900 Phone: + 61 2 9953 3000 Fax: + 61 2 9953 3105 info@ssw.com.auwww.ssw.com.au