Real-world Entity FrameworkLlewellyn Falco – A Developer’s DeveloperLynn Langit – A DBA and more
Agenda          Query          Save         Patterns
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...
Programmatically access data by…ADO.NET             Entity Framework• Custom DAO        • Complete DAO• T-SQL             ...
Agenda         Query
Real-World Problem #1 - Report• Report for customer ‘Lynn’• Sent orders    # 54, #63, #57• Partially sent orders    #82:...
The Report T-SQL
Current Practices- Life without Entity FrameworkCode Demo
Report with Entity FrameworkCode Demo
Using EF – Writing a Query using LINQ
Compare Extracting Data
Mapping Relationships Id   Name Id Number Total Customerid                         ADO                                .Ne ...
Report with Entity FrameworkCode Demo - Continued
Smart Objects               Customer               GetShipped               GetUnshipped               GetPartiallyShiped ...
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...
Constrained Eager LoadingCode Demo
Agenda         Save
Real-world Problem #2 – CRUD operations      Warehouse              Process        Load / Create       Report             ...
WarehouseProcessingCode Demo
Using EF – Adding Data using LINQ
With Entity Framework…• Simplified/ Reduced data access code    Save Encapsulates       Delete       Insert       Upda...
SQL Injection and Entity Framework    “Little Bobby Tables”
Agenda         Patterns
Data Usage Patterns   Load Model                      Manipulate Model                      Save Model    Common          ...
ModularityCode Demo
Making Entity Framework Testable Old Way                   New Way - Testable DoSomething()             DoSomething() 2)Ge...
Testing LoadersCode Demo
Common Usage Pattern                             MyProject.Views     Load Model      MyProject.Controller                 ...
Common Usage Pattern                             MyProject.Views     Load Model      MyProject.Controller                 ...
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                      ?                      =
Existing 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    Master LIN...
More about learning LINQ – using Koans• 28 progressive examples to help you learn LINQ• LinqKoans.codeplex.com
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 ++) SmallBasic or Java  Free Courseware (recipes)
Contact Information                  @LlewellynFalco                  www.ApprovalTests.com                      @LynnLang...
Upcoming SlideShare
Loading in …5
×

Real-world Entity Framework

4,095 views

Published on

deck from our talk at DevTeach

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

No Downloads
Views
Total views
4,095
On SlideShare
0
From Embeds
0
Number of Embeds
1,755
Actions
Shares
0
Downloads
56
Comments
0
Likes
3
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
  • Real-world Entity Framework

    1. 1. Real-world Entity FrameworkLlewellyn Falco – A Developer’s DeveloperLynn Langit – A DBA and more
    2. 2. Agenda Query Save Patterns
    3. 3. What is Entity Framework 4?Object Relational Mapper (ORM)•Included in .NET 4.0•Developers write .NET code • using LINQ • generate database access code
    4. 4. Generating a ModelDEMO - startING with EF
    5. 5. 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
    6. 6. Programmatically access data by…ADO.NET Entity Framework• Custom DAO • Complete DAO• T-SQL • LINQ
    7. 7. Agenda Query
    8. 8. 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
    9. 9. The Report T-SQL
    10. 10. Current Practices- Life without Entity FrameworkCode Demo
    11. 11. Report with Entity FrameworkCode Demo
    12. 12. Using EF – Writing a Query using LINQ
    13. 13. Compare Extracting Data
    14. 14. Mapping Relationships Id Name Id Number Total Customerid ADO .Ne t CustomerCustomer Id InvoiceId Invoice Name IdName Id Number Number EF Total Total CustomerId CustomerId
    15. 15. Report with Entity FrameworkCode Demo - Continued
    16. 16. Smart Objects Customer GetShipped GetUnshipped GetPartiallyShiped Invoice IsShipped IsUnshipped IsPartiallyShiped
    17. 17. Looking at the T-SQL Generated by EF• Use SQL Profiler• Create a trace• Filter for Application:• EntityFramework
    18. 18. The N+1 Problem 1 + 1 + 5 =7 Customer Customers Invoices Trips to Invoices Items Database
    19. 19. 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
    20. 20. Constrained Eager LoadingCode Demo
    21. 21. Agenda Save
    22. 22. Real-world Problem #2 – CRUD operations Warehouse Process Load / Create Report Shipment Customer Load / Create Load / Create Action Invoice Item Invoice ShippedCancelled Remove Set Item Item Shipped
    23. 23. WarehouseProcessingCode Demo
    24. 24. Using EF – Adding Data using LINQ
    25. 25. 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
    26. 26. SQL Injection and Entity Framework “Little Bobby Tables”
    27. 27. Agenda Patterns
    28. 28. Data Usage Patterns Load Model Manipulate Model Save Model Common Preferred
    29. 29. ModularityCode Demo
    30. 30. Making Entity Framework Testable Old Way New Way - Testable DoSomething() DoSomething() 2)Get Data 2)Load Data 3)Manipulate Data 3)DoSomething(Data) 4)Render Data Load() 2)Get Data DoSomething(Data) 2)Manipulate Data 3)Render Data
    31. 31. Testing LoadersCode Demo
    32. 32. Common Usage Pattern MyProject.Views Load Model MyProject.Controller Manipulate Model Save Model MyProject.Business MyProject.Rules MyProject.Processes
    33. 33. Common Usage Pattern MyProject.Views Load Model MyProject.Controller Manipulate Model Save Model MyProject.Business MyProject.Rules MyProject.Processes MyProject.Loaders
    34. 34. Stored Procedures• Security• Reduced Access• Performance• Modularity• Containment• Maintainability
    35. 35. Mapping Stored Procedures to EF Models
    36. 36. ORM to DB should be 1-1• Complex mappings obscure the database ? =
    37. 37. Existing Database Problems areAmplified• Primary Keys• Wide Tables• Bad Names• Dead Columns
    38. 38. “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
    39. 39. More about learning LINQ – using Koans• 28 progressive examples to help you learn LINQ• LinqKoans.codeplex.com
    40. 40. Resources for Attendees• EF team blog – here• Julie Lehrman book - here• Testing EF – video coming soon  On ApprovalTests YouTube playlist - here  www.ApprovalTests.com
    41. 41. Related Session(s)http://www.devteach.com/Schedule.aspx•SQL361 - Query Tuning Tips for SQL Server
    42. 42. TeachingKidsProgramming.org Do a Recipe  Teach a Kid (Ages 10 ++) SmallBasic or Java  Free Courseware (recipes)
    43. 43. Contact Information @LlewellynFalco www.ApprovalTests.com @LynnLangit www.LynnLangit.com

    ×