Developing Windows and Web Applications using Visual Studio.NET<br />Peter Gfader<br />
Who did the C# tutorial?<br />What is .NET?<br />What is the .NET framework?<br />What is the CLR?<br />What is OOP?<br />...
Agenda<br />C# 3.0 Language Enhancement<br />C# 4.0 Language Enhancement<br />C# 5<br />What is LINQ?<br />LINQ Project Go...
C# 3<br />2008<br />
Local Variable Type Interface<br />Anonymous Type<br />Lambda Expressions<br />Extension methods<br />Object initializers<...
Local Variable Type Inference<br />
Fits with complex temporarily variables<br />E.g. I want a temp variable to hold username & password<br />Anonymous Type<b...
Anonymous methods in C#2<br />NumericalOperationmul1 = (x, y) => x * y; <br />Console.WriteLine( mul1(4, 5) ); <br />Lambd...
Extend every class with a static „helper“ method<br />Extension methods<br />
More<br />Object initializer’s<br />Query Expressions<br />Please visit:<br />HookedOnLINQ.com<br />
C# 4<br />April 2010<br />
„dynamic“ keyword<br />Optional parameters<br />Named parameters<br />Co and Contra variance<br />C# 4 Language Enhancemen...
Co and Contra variance<br />How are generic types “broken” today?<br /> class Animal { }<br /> class Sheep : Animal { }<br...
Covariance – think “out”<br />Contravariance – think “in”<br />Break it down…<br />
C# 5?<br />???? 2013 ????<br />
„Meta programming is painful”<br />	 Compiler as a service<br />C# 5 ?  - Release date?<br />
Auto Implemented Properties<br />Collection Initializers<br />Named parameters<br />Optional parameters<br />Lambdas<br />...
Auto Implemented Properties<br />Collection Initializers<br />Named parameters<br />Optional parameters<br />Lambdas<br />...
Data access<br />Show me the data!<br />
The problem<br />
Problem<br />
First showcased at the American International Toy Fair in New York in 2005<br />LINQ, a clever bluffing game<br />What is ...
LanguageINtegratedQuery<br />Allows you to easily query information about an object<br />LINQ to SQL: An ORM made by Micro...
   Language syntax+ a library of extension methods+ lambda expressions+ anonymous types+ a little magic<br />What is LINQ?...
.NET Framework 3.5<br />
LINQ is a natural language extension, part of .NET 3.5<br />Different data sources are queried with the same syntax. <br /...
Unified programming model for any data type<br />Database Relational Data<br />XML Files<br />Collections & Arrays<br />In...
LINQ Project Architecture<br />C#<br />VB<br />Others...<br />.NET Language Integrated Query (LINQ)<br />LINQ Enabled Data...
LINQ Query<br />
stringstrSQL = “SELECT custID FROM Customers“;<br />SqlCommandcmd = new SqlCommand(strSQL, cnn);<br />SqlDataAdapteradAdap...
NorthwindDataContext db = newNorthwindDataContext();<br />var customers = from c indb.Customers<br />			select c;<br />   ...
How to use LINQ<br />
Benefits from LINQ<br />LINQ data binding<br />LINQ<br />
Stored procedures are easier to work with<br />Relationship editor UI is nicer (more like SQL Server)<br />Performance<br ...
More refined ORM than LINQ to SQL<br />Many to Many relationships<br />Richer support for Inheritance<br />Validation<br /...
Do you know why you should choose "LINQ to Entities" over "LINQ to SQL"?<br />http://www.ssw.com.au/ssw/Standards/Rules/Ru...
LINQ Pad<br />http://www.linqpad.net/<br />Hot Tip<br />
New features in C# 3.0<br />New features in C# 4.0<br />What is LINQ and Why LINQ<br />LINQ data binding<br />Summary<br />
Introduction to LINQhttp://msdn.microsoft.com/en-us/library/bb397897.aspx<br />Getting Started with LINQ in C#http://msdn....
What's next?<br />
Thank You!<br />Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA <br />ABN: 21 069 371 ...
Upcoming SlideShare
Loading in …5
×

C# advanced topics and future - C#5

9,931 views

Published on

C# 3.0 Language Enhancement
C# 4.0 Language Enhancement
C# 5
What is LINQ?
LINQ Project Goals
LINQ Project Architecture
Data Binding via LINQ

Published in: Education
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
9,931
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
285
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide
  • Click to add notesPeter Gfader Developing Windows and Web applications
  • Who did the C# tutorial?What is .NET?What is .NET framework?What is the CLR?What is OOP?Classes, Objects, Properties, Events, MethodsEncapsulation, Abstraction, Inheritance, Polymorphism
  • Ask student questions. Who use c# 3.0 before, what is linq
  • Some people think that is a kind of backward. But that is not really the case… then….With this feature, the type of the local variable being declared is inferred from the expression used to initialize the variable. This is achieved using the var keyword (familiar to those who work with scripting languages, but actually it is quite different). It allows us to write the following code:The compiler would generate the same IL as if we compiled:Note that there is no un-typed variable reference nor late-binding happening, instead the compiler is inferring and declaring the type of the variable from the right-hand side of the assignment. As a result, the var keyword is generating a strongly typed variable reference.Help developers lazy
  • Anonyous typesThis feature is simply, creating a complex type on the fly, which helps sometimes when you want a temp variable of complex type, that you&apos;ll never use again, so you don&apos;t need to write a class for it, so you just tell the compiler to write it for you, as you can see in the slide, you use the varkeyword to refer to the variable, and initailize by specifying the properties of the complex type and in the same time initializing them, so the compiler can also infere the type of the properties so the compiler can generate the class for you.Create a small demo. Live tmpUser.Username …
  • Lambda Expressions is one of most common features between dynamic and functional languages such as Lisp &amp; Python .. etc.Lambda Expressions is an improvement of the delegate idea, which improved in C# 2.0 as Anonymous Methods, and now in C# 3.0 it is Lambda Expressions.Consider the following code, we declared a delegate called NumericalOperation which refers to a function that accepts two inputs of type int, and returns output of type int, and int first lines in the main, we are using C# 1.x code to use this delegate, so we create an object of the NumericalOperation that refers to the Add function, and then call the function using the delegate.then in the next lines we are using C# 2.0 code to declare the object and in the same time declare the function using the anonymous method style, so we created a function that subtracts two integers, and use it .then in the next lines we are using C# 3.0 code to declare the object and in the same time declaring the function using the lambda expressions code, which is somehow looks weird in the first time, but it is really easy to understandfirst we write the delegate type followed by the object name on the left hand side, then on the right hand side we list all the input parameters that will be used in the expression in a coma list, then the arrow operator =&gt; followed by the expression that evaluates the return value.so if you want to write a lambda expression that adds two numbers, so you can say it like thisNumericalOperation op = (x, y) =&gt; x + y;delegate intNumericalOperation(int x, int y);internal static void Main(){// C# 1.0 NumericalOperation add = new NumericalOperation(Add);Console.WriteLine(&quot;C# 1.0: delegates {0,28}&quot;, add(1, 2));//C# 2.0 NumericalOperation sub = delegate(int x, int y){return x - y;};Console.WriteLine(&quot;C# 2.0: Anonymous Methods {0,20}&quot;, sub(2, 1));//C# 3.0 NumericalOperation mul1 = ( x, y)=&gt; x * y; Console.WriteLine(&quot;C# 3.0: Lambda Expressions {0,20}&quot;, mul1(4, 5));//Another Approach without the need to write the delegate Func&lt;int, int, int&gt; mul2 = (x, y)=&gt;x*y;Console.WriteLine(&quot;C# 3.0: Lambda Expressions {0,20}&quot;, mul2(4, 5));}internal static int Add(int x, int y){return x + y;}
  • Open the site, show few points
  • http://blogs.msdn.com/somasegar/archive/2009/08/19/vb-and-c-enhancements-in-vs-2010.aspx
  • „Meta programming is painful”Dynamic code generationEFLINQ2SQLReflection.EmitC# compiler in managed code“Compiler as a service”Neuer C#-Compiler, geschrieben in C# - schonseit 16 Monaten in Arbeit  ImplementiertalswiederverwendbaresModul  Verwendung des Compilers auseigenenProgrammenherauswirdeinfacher  Vorteile  Eval  Embedding, DSLs, …   Meta-programming (etwasunklar)   Language Object Model
  • Show the data access problem!!SQL Server: tables and rowsC#: Objects and propertiesRelational vs. object oriented
  • As a good start, let&apos;s see the problem we&apos;ve been dealing with for the past decades. Both data manipulation and logic implementation improved in different direction, while the data manipulation were treated declaratively using languages like SQL, on the other hand, logic implementation treated imperatively using Object oriented programming concepts and also standard control flow constructs like conditions &amp; loops.This small problem introduced a bigger problem which is making the application always divided into two different contextThe Data context Relational Databases Structured Documents (XML) Any other data store (Excel sheets, ... etc)The Logic contextand also another problem comes from dealing with different context, which is dealing with every context in a different way, so you can&apos;t easily have a join between a relational database table and an object-in-memory collection or XML document, several solutions were provided for this, but they were either too hard or too complexHistory:RecordsetDatasetlinq
  • The platform provides the basis. 3.5 new technology ---- LINQ
  • Object-relational mappingLanguage syntax+ a library of extension methods+ lambda expressions+ anonymous types+ a little magic= LINQ
  • Object-relational mapping
  • LINQ (Language-Integrated Query) Have more details in the following slides
  • LINQ is a natural language extension, part of .NET 3.5Different data sources are queried with the same syntax. Queries are strongly typedQueries benefit from intellisenseApplications becomes less error proneLinq is mainly help for developerOptimize
  • So from understanding the problem, LINQ provides the solution, first it introduce a unified model of dealing with data, which all data can be treated similarly in memory, so you can have something like joining data records coming from a database table with a collection in memory then saving the results in a XML document in the same instruction.Also LINQ is using SQL-like instructions, which are very familiar to developers, and also providing a good level of abstraction using more declarative syntax, which gives the system more space for decisions
  • LINQ Architecture is very elegant and simple, as shown in the slide it provides queries for both C# 3 &amp; VB9 as a first class citizen, and also it provides an abstraction for different implementations of LINQ like LINQ-To-Objects, LINQ-To-SQL (DLINQ) and LINQ-To-XML (XLINQ).In addition it provides a good extensibility model to implement LINQ on different data stores.LINQ – SQLLINQ - Object
  • The only difference is select goes last. For the intelence purpose…Interactive with people, before show the LINQ stuff.Write in console application.
  • String strSQL = &quot;SELECT custID FROM Customers“;SqlCommandcmd = New SqlCommand(strSQL, cnn);SqlDataAdapteradAdapter = New SqlDataAdapter(cmd);DataSetdst = New DataSet();adAdapter.Fill(dst);
  • NorthwindDataClassesDataContext db = newNorthwindDataClassesDataContext();var customers = from c indb.Customers select c;
  • Eric’s demo
  • Dbml – database modelExplain to people the code behind for the layout of that designer.csRun sqlproflier . See LINQ is very smart, can optimize the query for you
  • # Many to Many relationships * In LINQ to SQL when you model a many to many relationship (e.g. Employees and Territories from Northwind) it involved 3 tables: Employee, EmployeeTerritories and Territory * In LINQ to Entities it only uses the Employee and Territory tables and does not need the associative table EmployeeTerrirories# Richer support for Inheritance * Table per Hierarchy - store all the data from the inherited types into the same table e.g. Employee, Customer classes will store data into the Person table * Table per Type - each type is stored in a separate tables# Validation * This means you can use the designer to make changes and validate it against your schema# Update model from database * This tells you if schema has changed and allows you to update the mapping from the designer. This was a short coming in LINQ to SQL# Mapping EditorAllows you to use the designer to create the mappings - similar to the Java Hibernate editor (eclipse add-in)
  • Let student download it then show how to use itUse it before presentationChange to C# statementsResult.Count().Dump()How to translate to SQLTutorial
  • C# advanced topics and future - C#5

    1. 1. Developing Windows and Web Applications using Visual Studio.NET<br />Peter Gfader<br />
    2. 2. Who did the C# tutorial?<br />What is .NET?<br />What is the .NET framework?<br />What is the CLR?<br />What is OOP?<br />Session 1: Last week<br />
    3. 3. Agenda<br />C# 3.0 Language Enhancement<br />C# 4.0 Language Enhancement<br />C# 5<br />What is LINQ?<br />LINQ Project Goals<br />LINQ Project Architecture<br />Data Binding via LINQ<br />Session 2: Overview<br />
    4. 4. C# 3<br />2008<br />
    5. 5. Local Variable Type Interface<br />Anonymous Type<br />Lambda Expressions<br />Extension methods<br />Object initializers<br />Query Expressions<br />C# 3.0 Language Enhancement<br />
    6. 6. Local Variable Type Inference<br />
    7. 7. Fits with complex temporarily variables<br />E.g. I want a temp variable to hold username & password<br />Anonymous Type<br />
    8. 8. Anonymous methods in C#2<br />NumericalOperationmul1 = (x, y) => x * y; <br />Console.WriteLine( mul1(4, 5) ); <br />Lambda Expressions<br />
    9. 9. Extend every class with a static „helper“ method<br />Extension methods<br />
    10. 10. More<br />Object initializer’s<br />Query Expressions<br />Please visit:<br />HookedOnLINQ.com<br />
    11. 11. C# 4<br />April 2010<br />
    12. 12. „dynamic“ keyword<br />Optional parameters<br />Named parameters<br />Co and Contra variance<br />C# 4 Language Enhancement<br />
    13. 13. Co and Contra variance<br />How are generic types “broken” today?<br /> class Animal { }<br /> class Sheep : Animal { }<br /> void Speak(IEnumerable<Animal> animals)<br /> {<br /> // Do something with Animals<br /> }<br />Not Allowed:<br />IEnumerable<Animal> != IEnumerable<Sheep><br />var sheep = new List<Sheep>();<br /> Speak(sheep);<br />
    14. 14. Covariance – think “out”<br />Contravariance – think “in”<br />Break it down…<br />
    15. 15. C# 5?<br />???? 2013 ????<br />
    16. 16. „Meta programming is painful”<br />  Compiler as a service<br />C# 5 ? - Release date?<br />
    17. 17. Auto Implemented Properties<br />Collection Initializers<br />Named parameters<br />Optional parameters<br />Lambdas<br />Can span statements across multiple lines<br />Auto Implemented Properties<br />Collection Initializers (limited)<br />Named parameters<br />Optional parameters<br />Lambdas (limited)<br />Can span statements across multiple lines<br />.NET 3.5<br />
    18. 18. Auto Implemented Properties<br />Collection Initializers<br />Named parameters<br />Optional parameters<br />Lambdas<br />Can span statements across multiple lines<br />Auto Implemented Properties<br />Collection Initializers<br />Named parameters<br />Optional parameters<br />Lambdas<br />Can span statements across multiple lines<br />.NET 4<br />
    19. 19. Data access<br />Show me the data!<br />
    20. 20. The problem<br />
    21. 21. Problem<br />
    22. 22. First showcased at the American International Toy Fair in New York in 2005<br />LINQ, a clever bluffing game<br />What is LINQ?<br />
    23. 23. LanguageINtegratedQuery<br />Allows you to easily query information about an object<br />LINQ to SQL: An ORM made by Microsoft <br />LINQ to Entities: Another ORM made by Microsoft <br />LINQ to XML: Query over XML<br />...<br />What is LINQ?<br />
    24. 24. Language syntax+ a library of extension methods+ lambda expressions+ anonymous types+ a little magic<br />What is LINQ?<br />
    25. 25. .NET Framework 3.5<br />
    26. 26. LINQ is a natural language extension, part of .NET 3.5<br />Different data sources are queried with the same syntax. <br />Queries are strongly typed<br />Queries benefit from “Intellisense”<br />Applications becomes less error prone<br />LINQ - How does it solve the problem?<br />
    27. 27. Unified programming model for any data type<br />Database Relational Data<br />XML Files<br />Collections & Arrays<br />Introduce more declarative syntax<br />Helps the system find the best execution strategy<br />LINQ Project Goals<br />
    28. 28. LINQ Project Architecture<br />C#<br />VB<br />Others...<br />.NET Language Integrated Query (LINQ)<br />LINQ Enabled Data Sources<br />LINQ Enabled ADO.NET<br />LINQ to Objects<br />LINQ to ???<br />LINQ to XML<br />LINQ to Datasets<br />LINQ to SQL<br />LINQ to Entities<br /><book><br /> <title><br /> <author/><br /> <price /><br /> </title><br /></book><br />Objects<br />Database<br />XML<br />
    29. 29. LINQ Query<br />
    30. 30. stringstrSQL = “SELECT custID FROM Customers“;<br />SqlCommandcmd = new SqlCommand(strSQL, cnn);<br />SqlDataAdapteradAdapter = new SqlDataAdapter(cmd);<br />DataSetdst = new DataSet();<br />adAdapter.Fill(dst);<br /> Standard Data Access Code<br />
    31. 31. NorthwindDataContext db = newNorthwindDataContext();<br />var customers = from c indb.Customers<br /> select c;<br /> Code by LINQ<br />
    32. 32. How to use LINQ<br />
    33. 33. Benefits from LINQ<br />LINQ data binding<br />LINQ<br />
    34. 34. Stored procedures are easier to work with<br />Relationship editor UI is nicer (more like SQL Server)<br />Performance<br />In a recent benchmark LINQ to SQL performed 5 times faster than LINQ to Entities<br />LINQ 2 SQL<br />
    35. 35. More refined ORM than LINQ to SQL<br />Many to Many relationships<br />Richer support for Inheritance<br />Validation<br />Update model from database<br />Mapping Editor<br />LINQ 2 Entities<br />
    36. 36. Do you know why you should choose "LINQ to Entities" over "LINQ to SQL"?<br />http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx#WhyLINQtoEntitiesNotSQL<br />What to choose?<br />
    37. 37. LINQ Pad<br />http://www.linqpad.net/<br />Hot Tip<br />
    38. 38. New features in C# 3.0<br />New features in C# 4.0<br />What is LINQ and Why LINQ<br />LINQ data binding<br />Summary<br />
    39. 39. Introduction to LINQhttp://msdn.microsoft.com/en-us/library/bb397897.aspx<br />Getting Started with LINQ in C#http://msdn.microsoft.com/en-us/library/bb397933.aspx<br />LINQ Term and more detailshttp://www.hookedonlinq.com/<br />LinqPad<br />http://www.linqpad.net/<br />Resources<br />
    40. 40. What's next?<br />
    41. 41. Thank You!<br />Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA <br />ABN: 21 069 371 900 <br />Phone: + 61 2 9953 3000 Fax: + 61 2 9953 3105 <br />info@ssw.com.auwww.ssw.com.au <br />

    ×