real-world entity framework

43
Real-world Entity Framework Llewellyn Falco – A Developer’s Developer Lynn Langit – A DBA and more

Upload: lynn-langit

Post on 19-Dec-2014

3.216 views

Category:

Technology


1 download

DESCRIPTION

deck from our talk at DevTeach

TRANSCRIPT

Page 1: Real-world Entity Framework

Real-world Entity Framework

Llewellyn Falco – A Developer’s DeveloperLynn Langit – A DBA and more

Page 2: Real-world Entity Framework

Agenda

QueryQuery

SaveSave

PatternsPatterns

Page 3: Real-world Entity Framework

What is Entity Framework 4?

Object Relational Mapper (ORM)•Included in .NET 4.0•Developers write .NET code

• using LINQ • generate database access

code

Page 4: Real-world Entity Framework

DEMO - startING with EFGenerating a Model

Page 5: Real-world Entity Framework

What did we just do?

• Created a new project in Visual Studio• Added a new ADO.NET Entity Model • Generated a model from a database• Reviewed the generated model

diagram• Created an ObjectContext

Page 6: Real-world Entity Framework

Programmatically access data by…

ADO.NET

• Custom DAO• T-SQL

Entity Framework

• Complete DAO• LINQ

Page 7: Real-world Entity Framework

Agenda

QueryQuery

Page 8: Real-world Entity Framework

Real-World Problem #1 - Report

• Report for customer ‘Lynn’• Sent orders

# 54, #63, #57

• Partially sent orders #82: awaiting shoes, pants #92: awaiting gloves

• Unshipped orders #93 6 items #97 5 items

Page 9: Real-world Entity Framework

The Report T-SQL

Page 10: Real-world Entity Framework

Code DemoCurrent Practices- Life without Entity Framework

Page 11: Real-world Entity Framework

Code DemoReport with Entity Framework

Page 12: Real-world Entity Framework

Using EF – Writing a Query using LINQ

Page 13: Real-world Entity Framework

Compare Extracting Data

Page 14: Real-world Entity Framework

Mapping Relationships

CustomerIdName

CustomerIdName

Id Name Id Number Total Customerid

ADO.Net

InvoiceIdNumberTotalCustomerId

InvoiceIdNumberTotalCustomerId

CustomerIdName

CustomerIdName

InvoiceIdNumberTotalCustomerId

InvoiceIdNumberTotalCustomerId

EF

Page 15: Real-world Entity Framework

Code Demo - ContinuedReport with Entity Framework

Page 16: Real-world Entity Framework

Smart Objects

InvoiceIsShippedIsUnshippedIsPartiallyShiped

InvoiceIsShippedIsUnshippedIsPartiallyShiped

CustomerGetShippedGetUnshippedGetPartiallyShiped

CustomerGetShippedGetUnshippedGetPartiallyShiped

Page 17: Real-world Entity Framework

Looking at the T-SQL Generated by EF

• Use SQL Profiler• Create a trace• Filter for

Application:• EntityFramework

Page 18: Real-world Entity Framework

The N+1 Problem

1 + 1 + 5 = 7Customer Customers

InvoicesInvoices

ItemsTrips to

Database

Page 19: Real-world Entity Framework

More about Loading – Why Lazy Loading

• Lazy is Good and Easy, but

Lazy isn’t always efficient Lazy is always correct Sometimes better to use selective eager loading

• Syntax – use .Include("invoices.invoiceItems”)• More about – lazy loading - here

Page 20: Real-world Entity Framework

Code DemoConstrained Eager Loading

Page 21: Real-world Entity Framework

Agenda

SaveSave

Page 22: Real-world Entity Framework

Real-world Problem #2 – CRUD operations

WarehouseReport

Process Shipment

Load / Create Customer

Load / Create Invoice

Load / Create Invoice Item

Action

Remove Item

Set Item Shipped

ShippedCancelled

Page 23: Real-world Entity Framework

Code Demo

Warehouse

Processing

Page 24: Real-world Entity Framework

Using EF – Adding Data using LINQ

Page 25: Real-world Entity Framework

With Entity Framework…

• Simplified/ Reduced data access code Save Encapsulates

Delete Insert Update Key relationship Order of operations Transactions

• Prevents Most SQL Injection Sanitizes database inputs

Page 26: Real-world Entity Framework

SQL Injection and Entity Framework

“Little Bobby Tables”

Page 27: Real-world Entity Framework

Agenda

Patterns

Page 28: Real-world Entity Framework

Data Usage Patterns Load Model

Save Model

Manipulate Model

CommonPreferred

Page 29: Real-world Entity Framework

Code DemoModularity

Page 30: Real-world Entity Framework

Making Entity Framework Testable

Old Way

DoSomething()1)Get Data2)Manipulate Data3)Render Data

DoSomething()1)Get Data2)Manipulate Data3)Render Data

DoSomething()1)Load Data2)DoSomething(Data)

DoSomething()1)Load Data2)DoSomething(Data)

DoSomething(Data)1)Manipulate Data2)Render Data

DoSomething(Data)1)Manipulate Data2)Render Data

Load()1)Get DataLoad()1)Get Data

New Way - Testable

Page 31: Real-world Entity Framework

Code DemoTesting Loaders

Page 32: Real-world Entity Framework

MyProject.ControllerMyProject.Controller

MyProject.BusinessMyProject.Business

MyProject.ViewsMyProject.Views

MyProject.RulesMyProject.Rules

MyProject.ProcessesMyProject.Processes

Common Usage Pattern

Load Model

Save Model

Manipulate Model

Page 33: Real-world Entity Framework

Common Usage Pattern

MyProject.ControllerMyProject.Controller

MyProject.BusinessMyProject.Business

MyProject.ViewsMyProject.Views

MyProject.RulesMyProject.Rules

MyProject.ProcessesMyProject.ProcessesMyProject.LoadersMyProject.Loaders

Load Model

Save Model

Manipulate Model

Page 34: Real-world Entity Framework

Stored Procedures

• Security• Reduced Access• Performance• Modularity• Containment• Maintainability

Page 35: Real-world Entity Framework

Mapping Stored Procedures to EF Models

Page 36: Real-world Entity Framework

ORM to DB should be 1-1

• Complex mappings obscure the database

?=

Page 37: Real-world Entity Framework

Existing Database Problems are

Amplified

• Primary Keys• Wide Tables• Bad Names• Dead Columns

Page 38: Real-world Entity Framework

“Next Steps” for Attendees

• Use Entity Framework Write less CRUD code Protect against SQL Injection Master LINQ

• Use patterns / best practices with EF i.e. Loader pattern Modularity for testability (mocking)

• Map EF as closely as possible to the database Map 1-to-1 Fix design issues at the database level first

Page 39: Real-world Entity Framework

More about learning LINQ – using Koans

• 28 progressive examples to help you learn LINQ• LinqKoans.codeplex.com

Page 40: Real-world Entity Framework

Resources for Attendees

• EF team blog – here

• Julie Lehrman book - here

• Testing EF – video coming soon

On ApprovalTests YouTube playlist - here

www.ApprovalTests.com

Page 41: Real-world Entity Framework

Related Session(s)

http://www.devteach.com/Schedule.aspx •SQL361 - Query Tuning Tips for SQL Server

Page 42: Real-world Entity Framework

TeachingKidsProgramming.org

Do a Recipe Teach a Kid (Ages 10 ++)SmallBasic or Java Free Courseware (recipes)

Page 43: Real-world Entity Framework

Contact Information

@LlewellynFalco@LlewellynFalcowww.ApprovalTests.comwww.ApprovalTests.com

@[email protected]