l15 data source layer

Download L15 Data Source Layer

Post on 06-Feb-2015

141 views

Category:

Technology

1 download

Embed Size (px)

DESCRIPTION

Forritun gagnaagangs er lklega eitt algengasta vifangsefni vi ger enterprice lausna. Einhvern vegin verum vi a geyma stur og ggn. Til ess eru tflugagnagrunnar (relational databases) llkega algengasta formi af geymslu. Gallinn er s a hlutbundin forritun fellur ekkert srlega vel a tflugrunnum. essum fyrirlestri er yfir au vandaml sem koma upp vi hnnun gagnalagsins og hvernig best er a bra bili milli klasa forriti og taflna grunni.

TRANSCRIPT

  • 1. Lecture 15 Data Source Layer
  • 2. Reading Object-relational impedance mismatch Data Source Patterns Data Transfer Object Row Data Gateway Table Data Gateway Active Record Data Mapper Record set
  • 3. Agenda Design Objectives Object-Relational Impedance Mismatch Problems with database programming Data Source Patterns Spring JDBC RU Data Framework Content Example
  • 4. Design Objectives
  • 5. The Three Layers Presentation Users interface to the system User can be another system Accepts input, displays views Domain The Application of the system The Business logic Tends to creep into presentation and data source Data Source Connection to the database Also Persistence
  • 6. Relational Databases We are dealing with relational databases Wide-spread and well understood SQL based These are important in enterprise software Alternatives NoSQL databases O/R Mappers
  • 7. Connecting to Data Sources Programs need to interface the Data Source Usually this means relational databases Database vendors usually supply drivers for database Rational databases use SQL language Fairly standard Program Database classes Driver Database
  • 8. Objectives Hide SQL from the Domain Layer Access to database needs to ensure Speed and data integrity Concurrent access of many clients Database independence It can be an objective to keep the system independent of particular database technology Data Source Layer needs to be maintainable Database will change
  • 9. Impedance Mismatch
  • 10. Mapping Objects to Tables Object Relational Impedance Mismatch Objects are different than tables Encapsulation, Accessibility, Interfaces, classes, inheritance Data types are different Booleans, dates, string etc. Structural differences Classes contain other classes
  • 11. The Legacy Problem In most cases the data model exists The schema already exists We cannot assume that we create the schema Data tends to stick where it lends Cannot assume that our application controls the schema The schema will likely outlive the application
  • 12. The Usability Problem The Database API determines the usability of the data access Should be easy to use The programming model is important Does matter how efficient and good a persistence framework is, if it is complex and cumbersome to use Tools may help, but should not be used to conceal excessive complexity If tools are required to generate data access the programming model is likely to be complex
  • 13. Using Databases Programmers tend to want to solve all problems in their domain Should we solve all problems in our object domain? Should we write everything in Java or C#? Databases are good at what they do But its necessary to let them do it in a natural way
  • 14. Database Code Database programming can be very repetitive Opportunities for reusability JDBC is too low-level Code is Bad! Dont write code unless you have to Try to write code for the business layer Persistence Frameworks are difficult to build Use the frameworks that exist
  • 15. QUIZ Which of these statements is false A) Data tends to stick where it lends B) Database programming tends to be low-level C) Objects tend to map nicely to the database D) Database programming tends to be repetitive
  • 16. QUIZ Which of these statements is false A) Data tends to stick where it lends B) Database programming tends to be low-level C) Objects tend to map nicely to the database D) Database programming tends to be repetitive
  • 17. Data Source Patterns
  • 18. Domain Layer Patterns Recap Transaction Script Organizes business logic by procedures where each procedure handles a single request from the presentation Domain Model An object model of the domain that incorporates both behaviour and data Table Module A single instance that handles the business logic for all rows in a database table or view
  • 19. Good Design Separate database code from other code Provide database classes to access the database All SQL code in the same place Factories for each database Use of Connection Pools Error handling SQLException is isolated in the Data Source Layer Wrap in domain specific exceptions use of runtime exceptions
  • 20. Design Example Domain Model uses gateways
  • 21. Useful Patterns Data Transfer Object An object that carries data between processes in order to deduce the number of method calls Record Set An in-memory representation of tabular data
  • 22. Data Transfer Object An object that carries data between processes in order to deduce the number of method calls Object that is used to transfer data between layers Data Source returns data objects to web layer
  • 23. Data Transfer Object How it Works Similar to Value Object but is constructed to carry data between layers Data source layer creates DTO for transfer DTOs holds data get/set method Can be mutable or immutable Could have methods to transform data for example serialize the data or convert to XML Simple Domain Objects can be used as DTO Creates dependencies
  • 24. Data Transfer Object Assembling DTO from domain objects Assembler reduces dependencies When To Use It Whenever you need to transfer multiple items of data between two processes in a single method call
  • 25. Record Set An in-memory representation of tabular data How It Works Contains the result of a database query Common in ADO.NET and JDBC One record is current, clients can traverse the set Usually provided by the database code When to Use It When returning data from a query
  • 26. Data Source Patterns Table Data Gateway Acts as a Gateway to a database table Row Data Gateway Acts as a Gateway to a single record in a data source Active Record Wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data Data Mapper A layer of Mappers that moves data between objects and database while keeping them independent
  • 27.