Entity Framework Best Practices

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

description

deck for DevTeach Van

Transcript of Entity Framework Best Practices

Page 1: Entity Framework Best Practices

Real-world Entity Framework

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

Page 2: Entity Framework Best Practices

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 3: Entity Framework Best Practices

Programmatically access data by…

ADO.NET

• Custom DAO• T-SQL

Entity Framework

• Complete DAO• LINQ

Page 4: Entity Framework Best Practices

CODE DEMOCurrent Practices- Life without Entity Framework

Page 5: Entity Framework Best Practices

The Report T-SQL

Page 6: Entity Framework Best Practices

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 7: Entity Framework Best Practices

DEMO - STARTING WITH EFGenerating a Model

Page 8: Entity Framework Best Practices

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 9: Entity Framework Best Practices

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 10: Entity Framework Best Practices

With Entity Framework…

• Simplified/ Reduced data access code CRUD operations Paging (Skip, Take) Complicated Joins

• Prevents Most SQL Injection Sanitizes database inputs

Page 11: Entity Framework Best Practices

CODE DEMOReport with Entity Framework

Page 12: Entity Framework Best Practices

Using EF – Writing a Query using LINQ

Page 13: Entity Framework Best Practices

More about learning LINQ – using Koans

• Koans are ‘practices’ that you work on in Visual Studio• Koans are based on TDD – they use tests• ‘Fix’ each method (fill in the LINQ) and get your test to pass• Ask yourself ‘what did I learn?’ after getting each test to

pass• Get the LINQ Koans from CodePlex - here

Page 14: Entity Framework Best Practices

Looking at the T-SQL Generated by EF

• Use SQL Profiler• Create a trace• Filter for

Application:• EntityFramework

Page 15: Entity Framework Best Practices

The N+1 Problem

1 + 1 + 5 = 7Customer Customers

InvoicesInvoices

ItemsTrips to

Database

Page 16: Entity Framework Best Practices

More about Loading – Why Lazy Loading

• Lazy is Good and Easy, but Lazy isn’t always efficient Lazy is always correct

• Syntax - • More about – lazy loading - here

Page 17: Entity Framework Best Practices

CODE DEMOEager Loading

Page 18: Entity Framework Best Practices

Real-world Problem #2 – CRUD operations

• 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 19: Entity Framework Best Practices

CODE DEMOCRUD Operations

Page 20: Entity Framework Best Practices

Using EF – Adding Data using LINQ

Page 21: Entity Framework Best Practices

SQL Injection and Entity Framework

Page 22: Entity Framework Best Practices

Data Usage Patterns Load Model

Save Model

Manipulate Model

CommonPreferred

Page 23: Entity Framework Best Practices

CODE DEMOModularity

Page 24: Entity Framework Best Practices

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 25: Entity Framework Best Practices

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 26: Entity Framework Best Practices

Making Entity Framework Testable

Old Way

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

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

DoSomething()1)Choose DataLoaderDoSomething()1)Choose DataLoader

DoSomething(Loader)1)Call Loader2)Manipulate Data3)Render Data

DoSomething(Loader)1)Call Loader2)Manipulate Data3)Render Data

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

New Way - Testable

Page 27: Entity Framework Best Practices

Stored Procedures

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

Page 28: Entity Framework Best Practices

Mapping Stored Procedures to EF Models

Page 29: Entity Framework Best Practices

ORM to DB should be 1-1

• Complex mappings obscure the database

?=

Page 30: Entity Framework Best Practices

CODE DEMOOne-to-One Mapping

Page 31: Entity Framework Best Practices

Database Problems are

Amplified

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

Page 32: Entity Framework Best Practices

“Next Steps” for Attendees

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

• 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 33: Entity Framework Best Practices

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 34: Entity Framework Best Practices

Related Session(s)

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

Page 35: Entity Framework Best Practices

TeachingKidsProgramming.org

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

Page 36: Entity Framework Best Practices

Contact Information

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

@[email protected]