Entity Framework Best Practices

  • 10,034 views
Uploaded on

deck for DevTeach Van

deck for DevTeach Van

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
10,034
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
109
Comments
0
Likes
5

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Abstract -  Entity framework (an ORM) is fast becoming the way that developers are accessing SqlServer Databases. The meeting of the Developer mind and the DBA mind has long been “contentious”. In this session Lynn will speak for the DBA’s and Llewellyn will speak for the Developers. Are there Best Practices they both can agree on? Maybe there are at least some Worst Practices they can both agree to avoid? Listen in and find out…
  • http://msdn.microsoft.com/en-us/data/aa937723 Picture credit - http://www.theserverside.net/tt/articles/showarticle.tss?id=IntroducingEntityFramework&asrc=EM_NLN_1469313&uid=5982486
  • Using other tools to profile – Julie Lehrman MSDN Article - http://msdn.microsoft.com/en-us/magazine/gg490349.aspx
  • MSDN lazy loading - http://msdn.microsoft.com/en-us/library/bb896272.aspx
  • What is a Loader Single Interface (Delegate) found in ApprovalUtilities Façade Pattern T Load();
  • MSDN on how to map - http://msdn.microsoft.com/en-us/library/bb896279.aspx
  • About Mapping (MSDN) - http://msdn.microsoft.com/en-us/library/bb399232.aspx
  • Image credit - http://hibou-chatte.blogspot.com/2011/01/please-shop-at-boyds.html
  • http://www.monafoundation.org/project/Teaching-Kids-Programming/22
  • Both

Transcript

  • 1. Real-world Entity FrameworkLlewellyn Falco – A Developer’s DeveloperLynn Langit – A DBA and more
  • 2. 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
  • 3. Programmatically access data by…ADO.NET Entity Framework• Custom DAO • Complete DAO• T-SQL • LINQ
  • 4. Current Practices- Life without Entity FrameworkCODE DEMO
  • 5. The Report T-SQL
  • 6. What is Entity Framework 4?Object Relational Mapper (ORM)•Included in .NET 4.0•Developers write .NET code • using LINQ • generate database access code
  • 7. Generating a ModelDEMO - STARTING WITH EF
  • 8. 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
  • 9. 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
  • 10. With Entity Framework…• Simplified/ Reduced data access code  CRUD operations  Paging (Skip, Take)  Complicated Joins• Prevents Most SQL Injection  Sanitizes database inputs
  • 11. Report with Entity FrameworkCODE DEMO
  • 12. Using EF – Writing a Query using LINQ
  • 13. 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
  • 14. Looking at the T-SQL Generated by EF• Use SQL Profiler• Create a trace• Filter for Application:• EntityFramework
  • 15. The N+1 Problem 1 + 1 + 5 =7 Customer Customers Invoices Trips to Invoices Items Database
  • 16. 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
  • 17. Eager LoadingCODE DEMO
  • 18. 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
  • 19. CRUD OperationsCODE DEMO
  • 20. Using EF – Adding Data using LINQ
  • 21. SQL Injection and Entity Framework
  • 22. Data Usage Patterns Load Model Manipulate Model Save Model Common Preferred
  • 23. ModularityCODE DEMO
  • 24. Common Usage Pattern MyProject.Views Load Model MyProject.Controller Manipulate Model Save Model MyProject.Business MyProject.Rules MyProject.Processes
  • 25. Common Usage Pattern MyProject.Views Load Model MyProject.Controller Manipulate Model Save Model MyProject.Business MyProject.Rules MyProject.Processes MyProject.Loaders
  • 26. Making Entity Framework Testable Old Way New Way - Testable DoSomething() DoSomething() 2)Get Data 2)Choose DataLoader 3)Manipulate Data 4)Render Data DoSomething(Loader) 2)Call Loader 3)Manipulate Data 4)Render Data Load() 2)Get Data
  • 27. Stored Procedures• Security• Reduced Access• Performance• Modularity• Containment• Maintainability
  • 28. Mapping Stored Procedures to EF Models
  • 29. ORM to DB should be 1-1• Complex mappings obscure the database ? =
  • 30. One-to-One MappingCODE DEMO
  • 31. Database Problems areAmplified• Primary Keys• Wide Tables• Bad Names• Dead Columns
  • 32. “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
  • 33. Resources for Attendees• EF team blog – here• Julie Lehrman book - here• Testing EF – video coming soon  On ApprovalTests YouTube playlist - here  www.ApprovalTests.com
  • 34. Related Session(s)http://www.devteach.com/Schedule.aspx•SQL361 - Query Tuning Tips for SQL Server
  • 35. TeachingKidsProgramming.org Do a Recipe  Teach a Kid (Ages 10 ++) Microsoft SmallBasic  Free Courseware (recipes)
  • 36. Contact Information @LlewellynFalco www.ApprovalTests.com @LynnLangit www.LynnLangit.com