orm - ivan marković

35
Ivan Marković MSP Lead Software Developer at SPAN d.o.o. ivan.markovic@studentpartn er.com

Upload: software-startup-academy-osijek

Post on 06-Aug-2015

66 views

Category:

Education


1 download

TRANSCRIPT

Page 1: ORM - Ivan Marković

Ivan MarkovićMSP LeadSoftware Developer at SPAN [email protected]

Page 2: ORM - Ivan Marković

Vidjeli smo…

• Tipovi aplikacija• Metode razvoja softverskog

proizvoda+ALM u Microsoft alatima• C#• Baze podataka

Page 3: ORM - Ivan Marković

Agenda

1) Entity Framework2) LINQ3) EF+LINQ Demo(s)

Page 4: ORM - Ivan Marković

Entity framework

Page 5: ORM - Ivan Marković

What is Entity Framework?

• Entity framework is an Object/Relational Mapping (O/RM) framework

• Automated mechanism for accessing & storing the data in the database

Page 6: ORM - Ivan Marković

What is ORM?

• ORM is a tool for storing data from domain objects to relational database (like MS SQL Server)

• O/RM includes three main parts: Domain class objects, Relational database objects and Mapping information

Page 7: ORM - Ivan Marković

Why ORM?

• ORM allows us to keep our database design separate from our domain class design

• This makes the application maintainable and extendable

Page 8: ORM - Ivan Marković

ORM vs Traditional Data Access Techniques

• ORM often reduces the amount of code that needs to be written

• High level of abstraction obscuring what is actually happening in the implementation code

Page 9: ORM - Ivan Marković

Entity Framework Architecture

Page 10: ORM - Ivan Marković

SQL Relationships

• One-to-Many Relationship• Many-to-Many Relationship• One-to-One Relationship

Page 12: ORM - Ivan Marković

Database first

• An existing database can be used• Code can be auto-generated.• Extensible using partial classes/ T4 templates• The developer can update the database

manually• There is a very good designer, which sync

with the underlining database

Page 13: ORM - Ivan Marković

Code first

• There is full control of the model from the Code; no EDMX/designer

• No manual intervention to DB is required

• The database is used for data only• POCO Class

Page 14: ORM - Ivan Marković

Model first

• Good support with EDMX designer• We can visually create the database model• EF generates the Code and database script• Extensible through partial classes• We can modify the model and update the

generated database.

Page 15: ORM - Ivan Marković

Code first from existing database

• POCO Classes from existing database

Page 16: ORM - Ivan Marković

Which one to choose?

• It depends on you…

Page 17: ORM - Ivan Marković

LINQ

Page 18: ORM - Ivan Marković

• Language-Integrated Query (LINQ)• Set of features that extends powerful

query capabilities to the language syntax of C# and Visual Basic. 

LINQ

Page 19: ORM - Ivan Marković

Three parts

• All LINQ query operations consist of three distinct actions:– Obtain the data source.– Create the query.– Execute the query.

Page 20: ORM - Ivan Marković

Three parts• // 1. Data source. • int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

• // 2. Query creation. • // numQuery is an IEnumerable<int> • var numQuery =• from num in numbers• where (num % 2) == 0• select num;

• // 3. Query execution. • foreach (int num in numQuery)• {• Console.Write("{0,1} ", num);• }

Page 21: ORM - Ivan Marković

The Data Source

• Types that support IEnumerable<T> or a derived interface such as the generic IQueryable<T> are called queryable types

Page 22: ORM - Ivan Marković

The Query

• The query specifies what information to retrieve from the data source or sources

• Query also specifies how that information should be sorted, grouped, and shaped before it is returned

• The query expression contains three clauses: from, where and select

Page 23: ORM - Ivan Marković

Deferred execution

• query variable itself only stores the query commands

• The actual execution of the query is deferred until you iterate over the query variable in a foreach statement

Page 24: ORM - Ivan Marković

Forcing Immediate Execution

• Examples of such queries are Count, Max, Average, and First. These execute without an explicit foreach statement because the query itself must use foreach in order to return a result.

• That these types of queries return a single value, not an IEnumerable collection.

• ToList<T>, ToArray<T>

Page 25: ORM - Ivan Marković

LINQ vs SQL

SELECT UPPER(Name)FROM CustomerWHERE Name LIKE 'A%'ORDER BY Name

Page 26: ORM - Ivan Marković

LINQ vs SQL

SELECT UPPER(Name) FROM( SELECT *, RN = row_number() OVER (ORDER BY Name) FROM Customer WHERE Name LIKE 'A%') AWHERE RN BETWEEN 21 AND 30ORDER BY Name

Page 27: ORM - Ivan Marković

LINQ vs SQL

var query = from c in db.Customers where c.Name.StartsWith ("A") orderby c.Name select c.Name.ToUpper();

var thirdPage = query.Skip(20).Take(10);

Page 28: ORM - Ivan Marković

LINQ vs SQL

from p in db.Purchaseswhere p.Customer.Address.State == "WA" || p.Customer == nullwhere p.PurchaseItems.Sum (pi => pi.SaleAmount) > 1000select p

Page 29: ORM - Ivan Marković

LINQ vs SQLSELECT p.*FROM Purchase p LEFT OUTER JOIN Customer c INNER JOIN Address a ON c.AddressID = a.ID ON p.CustomerID = c.IDWHERE (a.State = 'WA' || p.CustomerID IS NULL) AND p.ID in ( SELECT PurchaseID FROM PurchaseItem GROUP BY PurchaseID HAVING SUM (SaleAmount) > 1000 )

Page 30: ORM - Ivan Marković

When not to use LINQ for querying databases

• Hand-tweaked queries (especially with optimization or locking hints)

• Queries that involve selecting into temporary tables, then querying those tables

Page 31: ORM - Ivan Marković

Query syntax vs Method syntax

• IEnumerable<int> numQuery1 =

• from num in numbers

• where num % 2 == 0

• orderby num• select num;

• IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);

Page 32: ORM - Ivan Marković

Demo

Page 33: ORM - Ivan Marković

Q & A

?

Page 34: ORM - Ivan Marković

What’s next?

• 6.12. – Programiranje i testiranje softverskog proizvoda, Denis Sušac (Mono)– Radionica: Odabir tehnologija

• 10.12. – WCF servisi, REST servisi, Načini povezivanja i komunikacije klijenta i servisa, Leo Tot (MSP)

Page 35: ORM - Ivan Marković

Thank [email protected]