Upload
lynn-langit
View
10.022
Download
8
Embed Size (px)
DESCRIPTION
deck for DevTeach Van
Citation preview
Real-world Entity Framework
Llewellyn Falco – A Developer’s DeveloperLynn Langit – A DBA and more
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
Programmatically access data by…
ADO.NET
• Custom DAO• T-SQL
Entity Framework
• Complete DAO• LINQ
CODE DEMOCurrent Practices- Life without Entity Framework
The Report T-SQL
What is Entity Framework 4?
Object Relational Mapper (ORM)•Included in .NET 4.0•Developers write .NET code
• using LINQ • generate database access
code
DEMO - STARTING WITH EFGenerating a Model
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
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
With Entity Framework…
• Simplified/ Reduced data access code CRUD operations Paging (Skip, Take) Complicated Joins
• Prevents Most SQL Injection Sanitizes database inputs
CODE DEMOReport with Entity Framework
Using EF – Writing a Query using LINQ
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
Looking at the T-SQL Generated by EF
• Use SQL Profiler• Create a trace• Filter for
Application:• EntityFramework
The N+1 Problem
1 + 1 + 5 = 7Customer Customers
InvoicesInvoices
ItemsTrips to
Database
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
CODE DEMOEager Loading
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
CODE DEMOCRUD Operations
Using EF – Adding Data using LINQ
SQL Injection and Entity Framework
Data Usage Patterns Load Model
Save Model
Manipulate Model
CommonPreferred
CODE DEMOModularity
MyProject.ControllerMyProject.Controller
MyProject.BusinessMyProject.Business
MyProject.ViewsMyProject.Views
MyProject.RulesMyProject.Rules
MyProject.ProcessesMyProject.Processes
Common Usage Pattern
Load Model
Save Model
Manipulate Model
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
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
Stored Procedures
• Security• Reduced Access• Performance• Modularity• Containment• Maintainability
Mapping Stored Procedures to EF Models
ORM to DB should be 1-1
• Complex mappings obscure the database
?=
CODE DEMOOne-to-One Mapping
Database Problems are
Amplified
• Primary Keys• Wide Tables• Bad Names• Dead Columns
“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
Resources for Attendees
• EF team blog – here
• Julie Lehrman book - here
• Testing EF – video coming soon
On ApprovalTests YouTube playlist - here
www.ApprovalTests.com
Related Session(s)
http://www.devteach.com/Schedule.aspx •SQL361 - Query Tuning Tips for SQL Server
TeachingKidsProgramming.org
Do a Recipe Teach a Kid (Ages 10 ++)Microsoft SmallBasic Free Courseware (recipes)
Contact Information
@LlewellynFalco@LlewellynFalcowww.ApprovalTests.comwww.ApprovalTests.com