© Logica 2008. All rights reserved ADO vNext LINQ LINQ to SQL Entity Framework Freek Leemhuis...

26
© Logica 2008. All rights reserved ADO vNext LINQ LINQ to SQL Entity Framework Freek Leemhuis [email protected]

Transcript of © Logica 2008. All rights reserved ADO vNext LINQ LINQ to SQL Entity Framework Freek Leemhuis...

© Logica 2008. All rights reserved

ADO vNext

LINQ

LINQ to SQL

Entity Framework

Freek [email protected]

Microsoft en ORM – de historieMicrosoft en ORM – de historie

WinFS

Defining LINQ and LINQ to SQL

• Bestaat uit taal uitbreidingen in C# 3.0 en VB 9.0

• Uniforme manier om queries uit te voeren op objecten

• Een framework (OR mapper) voor het mappen van data classes op SQL Server tables, views en stored procedures.

LINQ to SQL

LINQ

LINQ parts

Interne query engine

LINQ to Objects

IQueryableIEnumerable

Taaluitbreidingen

• Extension methods

• Partial Methods

• Lambda expressions

• Anonymous types

• Object initializers

• Local variable inference

• Linq keywords and operators

• Extention methods

• Local variable inference

• Anonymous types

Demo

Demo

De basis van LINQ

• LINQ queries gaan over elementen uit sequences

• Een sequence is een object dat de IEnumerable<T> interface implementeert

Bijvoorbeeld:

String[] namen = {“Maarten”, “Ralph”, “Freek”};

Selectie = namen.Where(n=>n.Contains(“a”)) .Orderby(n=>n)

.Select(n=>n.ToUpper());

IEnumerable

Ienumerable : enumeratie met yield return

Comprehension Syntax

Demo

Query voorbeelden

Query composition

var filtered = names .Where (n => n.Contains ("a"));

var sorted = filtered.OrderBy (n => n);

var query = sorted.Select (n => n.ToUpper( ));

LINQ to XML

•Demo

Data Access met LINQ

LINQ to SQL

Object Relational Impedance Mismatch

Objecten

Objects != Data

Relationele Data

Verschillen tussen LINQ to SQL en het Entity Framework

Data Access met ADO.Net

SqlConnection c = new SqlConnection(SqlConnection c = new SqlConnection(……););c.Open();c.Open();SqlCommand cmd = new SqlCommand(SqlCommand cmd = new SqlCommand( @"@"SELECT c.Name, c.PhoneSELECT c.Name, c.Phone FROM Customers cFROM Customers c WHERE c.City = @p0WHERE c.City = @p0");");cmd.Parameters.AddWithValue("cmd.Parameters.AddWithValue("@p0@p0", ", "London"London““););DataReader dr = c.Execute(cmd);DataReader dr = c.Execute(cmd);while (dr.Read()) {while (dr.Read()) { string name = dr.GetString(0);string name = dr.GetString(0); string phone = dr.GetString(1);string phone = dr.GetString(1); DateTime date = dr.GetDateTime(2);DateTime date = dr.GetDateTime(2);}}dr.Close();dr.Close();

Data Access met klassiek ADO.NetData Access met klassiek ADO.NetQueries in Queries in

quotesquotes

Loosely bound Loosely bound

argumentsarguments

Loosely typed Loosely typed

result setsresult sets

No compile No compile

time checkstime checks

public class Customer { public class Customer { …… } }

public class Northwind: DataContextpublic class Northwind: DataContext{{ public Table<Customer> Customers;public Table<Customer> Customers; ……}}

Northwind db = new Northwind(Northwind db = new Northwind(……););var contacts =var contacts = from c in db.Customersfrom c in db.Customers where c.City == "London"where c.City == "London" select new { c.Name, c.Phone };select new { c.Name, c.Phone };

Data Access met LINQ to SQL

Classes Classes

describe datadescribe data

Strongly typed Strongly typed

connectionconnection

Integrated Integrated

query syntaxquery syntax

Strongly typed Strongly typed

resultsresults

Tables are Tables are

like collectionslike collections

Linq to SQL : Mapping DB to Objects

LINQ to SQL Architectuur

from c in Context.Customers Where c.LastName. StartsWith(“Niks”)

select new { c.Name,

c.FirstName};

from c in Context.Customers Where c.LastName. StartsWith(“Niks”)

select new { c.Name,

c.FirstName};

select Name, FirstNameselect Name, FirstNamefrom customersfrom customerswhere Lastname like where Lastname like ‘Niks%'‘Niks%' SQL Server

Customer c = new Customer();Customer c = new Customer();Customer.LastName = “Bos”;Customer.LastName = “Bos”;Customers.InsertOnSumbit(c);Customers.InsertOnSumbit(c);Context.SubmitChanges();Context.SubmitChanges();

Dynamische SQLDynamische SQLof Stored Procedureof Stored Procedure

Services:Services:- Change tracking- Change tracking- Concurrency control- Concurrency control- Object identity- Object identity

LINQ to SQL

No. 2018 April 2023 Title of Presentation

Demo

Het ADO.Net Entity Framework

Conceptual Mapping Logical

Object Model

Relational Data

Entity

Entity

Entity

Entity

Table

Table

Table

Table

CSDL MSL SSDL

Table

Entity Relationship Model

Dr. Peter Chen, 1970

Entity Framework Layers

Conceptual

Mapping

Logical

Entity Framework Architecture

CSDL

Entity SQL

MSL

SSDL

Entity Client

Entity SQL

Object Services

Object Query

LINQ to Entities

Lange termijn visie op data access

Resources

Starten met Linq

http://msdn2.microsoft.com/en-us/library/bb308961.aspxhttp://www.asp.net/learn/linq-videos/http://weblogs.asp.net/scottgu/archive/tags/LINQ/ http://www.hookedonlinq.com/http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx

Linq ninjas

http://blogs.msdn.com/mattwar/default.aspx

http://weblogs.asp.net/fbouma

http://codebetter.com/blogs/ian_cooper/

http://mtaulty.com

LINQPad

http://www.linqpad.net/

Books