Data Access - Best Practice

4,046 views

Published on

Lars-Erik Kindblad presentation at the Capgemini Microsoft User Group 2012.

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

No Downloads
Views
Total views
4,046
On SlideShare
0
From Embeds
0
Number of Embeds
399
Actions
Shares
0
Downloads
104
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • MaintainPerformanceSecurityTransactionsLazy LoadingORMDbCommandTestingSQL / Mangelpå joins
  • Data Access - Best Practice

    1. 1. Data Access – Best PracticeLars-Erik KindbladSenior ConsultantBlog: kindblad.com
    2. 2. Agenda Why is Data Access so important? Common issues in many applications How to solve these issues Summary | Sector, Alliance, Offering
    3. 3. Why is Data Access so important? Retrieving, creating, updating and deleting data are core operations Affects the entire application in a bad way if done incorrectly | Sector, Alliance, Offering
    4. 4. Common issues in many applications | Sector, Alliance, Offering
    5. 5. Code issues The code in the data access layer is often put into a single or a few huge classes • Hard to maintain Database queries are constructed in the frontend or business layer • Leads to messy frontend or business code ORM framework limitations or lack of «ORM masters» • A lot of quickfixes Doesn’t support rollback of changes when errors occurs | Sector, Alliance, Offering
    6. 6. Performance issues The application slow • Too many database queries • Too heavy database queries • Lazy loading • Poorly generated SQL by the ORM framework | Sector, Alliance, Offering
    7. 7. Security issues Vulnerable to SQL Injection Users get access to data they should not have access too | Sector, Alliance, Offering
    8. 8. Quality issues Many bugs • No tests • ... or tests that doesn’t test the important stuff | Sector, Alliance, Offering
    9. 9. How to solve these issues | Sector, Alliance, Offering
    10. 10. What do we want? Well structured data access code that is easy to maintain • Small simple classes that does only one thing - Single Responsiblity Principle The application should be fast • Only retrieve the data that we actually need – Criteria Pattern • Do as much as possible in one query – SQL Joins • Avoid lazy loading The application should be secure • Use an ORM framework and/or use parameters instead of concatenated strings • Always check for permissions when retrieving, creating, updating or deleting data Rollback uncomitted changes if anything goes wrong • Transaction support As little dependency on the ORM as possible • Gateway Pattern Frontend, Business and Data Access Code should be separated • Logical Layering High quality – Bug free code • Integration tests | Sector, Alliance, Offering
    11. 11. Example code | Sector, Alliance, Offering
    12. 12. HOW TO:REDUCE ORM DEPENDENCIESSTRICTER LAYERINGROLLBACK IF ERROR OCCURSHAVE SIMPLE DATA ACCESS CODE | Sector, Alliance, Offering
    13. 13. The Common Way Frontend Layer BusinessORM Layer Query the Database Data Access Layer | Sector, Alliance, Offering
    14. 14. A Better Way Frontend Layer ORM Gateway Initialize ORM BusinessTransaction management Layer ORM Data Access Layer | Sector, Alliance, Offering
    15. 15. DbContexGateway for FluentData | Sector, Alliance, Offering
    16. 16. Data Access Layer | Sector, Alliance, Offering
    17. 17. Business Layer | Sector, Alliance, Offering
    18. 18. Frontend Layer | Sector, Alliance, Offering
    19. 19. EFFICIENT DATA RETRIEVAL & FILTERING | Sector, Alliance, Offering
    20. 20. Overview Frontend Criteria Pattern: Decide what data to retrieve and filter on Class Business Business logic ClassData Access Class Construct the most optimal and secure query | Sector, Alliance, Offering
    21. 21. Frontend Layer | Sector, Alliance, Offering
    22. 22. Business Layer | Sector, Alliance, Offering
    23. 23. Data Access Layer | Sector, Alliance, Offering
    24. 24. Overview Frontend Transaction management Layer Decide what data to retrieve and filter on Business Business logic LayerData Access Layer Construct the most optimal and secure query | Sector, Alliance, Offering
    25. 25. HOW TO:GET A HIGH QUALITY APPLICATIONTHROUGH INTEGRATION TESTS | Sector, Alliance, Offering
    26. 26. Test - CreateProductDbCommand | Sector, Alliance, Offering
    27. 27. Test - GetProductsDbCommand | Sector, Alliance, Offering
    28. 28. Summary Have small simple classes that does only one thing Use the Criteria Pattern to decide what data to retrieve Use joins to retrieve as much data as needed in one query Avoid lazy loading Use parameters instead of concatenated strings Always check for permissions when retrieving, creating, updating or deleting data Use transactions to rollback when errors occurs Use the Gateway Pattern to reduce ORM dependency Have a strict Frontend layer, Business layer, Data Access layer Verify quality through Integration Tests | Sector, Alliance, Offering
    29. 29. QUESTIONS? | Sector, Alliance, Offering
    30. 30. www.capgemini.comThe information contained in this presentation is proprietary. ©2010 Capgemini. All rights reserved

    ×