Real-world Entity FrameworkLlewellyn 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:...
Programmatically access data by…ADO.NET             Entity Framework• Custom DAO        • Complete DAO• T-SQL             ...
Current Practices- Life without Entity FrameworkCODE DEMO
The Report T-SQL
What is Entity Framework 4?Object Relational Mapper (ORM)•Included in .NET 4.0•Developers write .NET code    • using LINQ ...
Generating a ModelDEMO - STARTING WITH EF
What did we just do?• Created a new project in Visual Studio• Added a new ADO.NET Entity Model• Generated a model from a d...
Real-World Problem #1 - Report• Report for customer ‘Lynn’• Sent orders    # 54, #63, #57• Partially sent orders    #82:...
With Entity Framework…• Simplified/ Reduced data access code    CRUD operations    Paging (Skip, Take)    Complicated J...
Report with Entity FrameworkCODE DEMO
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 – ...
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 =7     Customer   Customers   Invoices    Trips to                 Invoices     Items    D...
More about Loading – Why Lazy Loading• Lazy is Good and Easy, but    Lazy isn’t always efficient    Lazy is always corre...
Eager LoadingCODE DEMO
Real-world Problem #2 – CRUD operations• Report for customer ‘Lynn’• Sent orders    # 54, #63, #57• Partially sent orders...
CRUD OperationsCODE DEMO
Using EF – Adding Data using LINQ
SQL Injection and Entity Framework
Data Usage Patterns   Load Model                      Manipulate Model                      Save Model    Common          ...
ModularityCODE DEMO
Common Usage Pattern                             MyProject.Views     Load Model      MyProject.Controller                 ...
Common Usage Pattern                             MyProject.Views     Load Model      MyProject.Controller                 ...
Making Entity Framework Testable Old Way             New Way - Testable DoSomething()       DoSomething() 2)Get Data      ...
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                      ?                      =
One-to-One MappingCODE DEMO
Database Problems areAmplified•   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 ...
Resources for Attendees• EF team blog – here• Julie Lehrman book - here• Testing EF – video coming soon   On ApprovalTest...
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                  www.ApprovalTests.com                      @LynnLang...
Upcoming SlideShare
Loading in …5
×

Entity Framework Best Practices

15,945 views

Published on

deck for DevTeach Van

Published in: Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
15,945
On SlideShare
0
From Embeds
0
Number of Embeds
47
Actions
Shares
0
Downloads
163
Comments
0
Likes
9
Embeds 0
No embeds

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

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

    ×