JDBC Session 5 <ul><li>Tonight:  Data Access Patterns </li></ul><ul><li>J2EE Architecture & Design Patterns </li></ul><ul>...
JDBC Session 5 <ul><li>What are J2EE Design Patterns?  </li></ul><ul><li>J2EE Design Patterns are specifically geared towa...
JDBC Session 5 Partitioning a Web Application  J2EE Architecture & Design Patterns   (p. 2) Presentation Tier Business Tie...
JDBC Session 5 <ul><li>What Each Tier Does </li></ul><ul><li>Presentation Tier </li></ul><ul><ul><li>Renders screens the u...
JDBC Session 5 <ul><li>Why Partition the Application into Tiers  </li></ul><ul><li>Makes the application easier to maintai...
JDBC Session 5 Categories of J2EE Design Patterns J2EE Architecture & Design Patterns   (p. 5) Presentation Tier Patterns ...
JDBC Session 5 Typical J2EE Design Patterns Implementation J2EE Architecture & Design Patterns   (p. 6) Front Controller B...
JDBC Session 5 The Business Layer J2EE Architecture & Design Patterns   (p. 7) Business Delegate Session  Facade Business ...
JDBC Session 5 The Data Access Layer J2EE Architecture & Design Patterns   (p. 8) Data Access Object Data Access Object Da...
JDBC Session 5 <ul><li>Why Focus on the Data Access Layer? </li></ul><ul><li>Most organizations spend a significant amount...
JDBC Session 5 <ul><li>The Solution to “Data Madness” </li></ul><ul><li>Apply common J2EE Data Access Patterns that will a...
JDBC Session 5 <ul><li>The three patterns we’ll study in this lesson are:  </li></ul><ul><li>Data Access Object Pattern (D...
JDBC Session 5 <ul><li>Here’s a class diagram of the architecture:  </li></ul><ul><li>The DataAccessObject (DAO) is the pr...
JDBC Session 5 <ul><li>The Data Access Object consists of an Interface called  DataAccessObject.java </li></ul><ul><li>The...
JDBC Session 5 <ul><li>The DAO pattern abstracts away the tasks associated with retrieving and manipulating data by defini...
JDBC Session 5 Remember,  DataAccessObject.java  is an  interface , so to use it we define one or more member classes that...
JDBC Session 5 <ul><li>The Value Object consists of an  abstract  class called  ValueObject.java </li></ul><ul><li>A Value...
JDBC Session 5 <ul><li>The VO class has 3 status fields with their own  get()  and  set()  methods that are used to tell t...
JDBC Session 5 Remember,  ValueObject.java  is  abstract , so to use it we define one or more member classes that extend t...
JDBC Session 5 <ul><li>Never allow data access specific exceptions to be thrown from the data access objects. </li></ul><u...
JDBC Session 5 <ul><li>Example on Web Site: </li></ul><ul><li>Lessons / JDBC / JDBC Session 5 / DataAccessExample  </li></...
JDBC Session 5 <ul><li>For 1000 no-points: </li></ul><ul><li>What other example have we looked at that kinda uses this pat...
JDBC Session 5 Designing a Framework  (p. 1) <ul><li>Layout your framework structure immediately.  It’s very difficult to ...
JDBC Session 5 <ul><li>In the enterprise, applications come and go, but the data in these systems lives on.  </li></ul><ul...
JDBC Session 5 DAO’s: Not just about Relational Databases: Designing a Framework  (p. 3) MemberDAO Relational Database JDB...
Upcoming SlideShare
Loading in …5
×

Jdbc Lecture5

816 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
816
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Jdbc Lecture5

  1. 1. JDBC Session 5 <ul><li>Tonight: Data Access Patterns </li></ul><ul><li>J2EE Architecture & Design Patterns </li></ul><ul><li>The Data Access Tier </li></ul><ul><li>Data Access Patterns </li></ul><ul><ul><li>DataAccessObject (DAO) </li></ul></ul><ul><ul><li>Value Object Pattern (VO) </li></ul></ul><ul><ul><li>DataAccessException </li></ul></ul><ul><li>Designing a Framework </li></ul>
  2. 2. JDBC Session 5 <ul><li>What are J2EE Design Patterns? </li></ul><ul><li>J2EE Design Patterns are specifically geared towards J2EE and Web Application Development. </li></ul><ul><li>These patterns focus on properly partitioning an application into distinct layers. </li></ul><ul><li>This makes it easier to maintain the application and gives more flexibility in distributing the application among different physical locations. </li></ul>J2EE Architecture & Design Patterns (p. 1)
  3. 3. JDBC Session 5 Partitioning a Web Application J2EE Architecture & Design Patterns (p. 2) Presentation Tier Business Tier (a.k.a The Middle Tier) Data Tier Corporate Database Corporate Database
  4. 4. JDBC Session 5 <ul><li>What Each Tier Does </li></ul><ul><li>Presentation Tier </li></ul><ul><ul><li>Renders screens the user will see and interact with. For a Java web application the presentation tier consists of JSP or servlet code that generates HTML for the end user to see. </li></ul></ul><ul><li>Business Logic Tier </li></ul><ul><ul><li>Enforces the application’s business rules. </li></ul></ul><ul><li>Data Access Tier </li></ul><ul><ul><li>Carries out all data source tasks (CRUD Tasks). </li></ul></ul>J2EE Architecture & Design Patterns (p. 3)
  5. 5. JDBC Session 5 <ul><li>Why Partition the Application into Tiers </li></ul><ul><li>Makes the application easier to maintain. Each tier in an application is unaware of the implementation details of any of the other tiers. </li></ul><ul><li>Enforces a layered, closed architecture where each tier can only speak to the tier below it. </li></ul><ul><li>Even when speaking to the tier below it, the upper tier only speaks through well-defined interfaces to the lower tiers. We try never to expose the upper tier to any implementation details of the lower tier. </li></ul><ul><li>Tiers are a logical division of labor and do not necessarily denote that the application has to be physically separated onto three distinct pieces of hardware. </li></ul>J2EE Architecture & Design Patterns (p. 4)
  6. 6. JDBC Session 5 Categories of J2EE Design Patterns J2EE Architecture & Design Patterns (p. 5) Presentation Tier Patterns Business Tier Patterns Integration Tier/Data Access Tier Patterns Intercepting Filter Front Controller View Helper Composite View Business Delegate Session Façade Value Object Assembler Data Access Object Value Object Service Locator
  7. 7. JDBC Session 5 Typical J2EE Design Patterns Implementation J2EE Architecture & Design Patterns (p. 6) Front Controller Business Delegate View Helper Integration/ Data Access Tier 1. 2. 3. <ul><li>Front controller takes incoming user request and decides what to do with it. </li></ul><ul><li>Business delegate is the business logic responsible for processing the user’s request. </li></ul><ul><li>View helpers are responsible for rendering the output that is sent back to the end user. </li></ul>
  8. 8. JDBC Session 5 The Business Layer J2EE Architecture & Design Patterns (p. 7) Business Delegate Session Facade Business Objects <ul><li>A business delegate hides the mechanics of how a process is invoked. The developer using the delegate does not know if they are using an EJB or a web service. </li></ul><ul><li>A session façade wraps the individual classes that carry out an entire business process. </li></ul><ul><li>The business objects are fine-grained objects used in carrying out the process. </li></ul>Session Facade Session Facade 1. 2. 3.
  9. 9. JDBC Session 5 The Data Access Layer J2EE Architecture & Design Patterns (p. 8) Data Access Object Data Access Object Data Access Object Value Objects <ul><li>A business object will retrieve and manipulate data using a Data Access Object. (DAO) </li></ul><ul><li>A DAO will encapsulate all of the CRUD logic (Create, Replace, Update, Delete) in the application. </li></ul><ul><li>All data sent to a DAO or retrieved from a DAO will be encapsulated in a Value Object (VO) </li></ul>1. 2. 3.
  10. 10. JDBC Session 5 <ul><li>Why Focus on the Data Access Layer? </li></ul><ul><li>Most organizations spend a significant amount of time modeling their business logic and presentation tiers. </li></ul><ul><li>However the data access tier is often left as an after thought. Most developers do not see the long-term benefits of a well-defined data access tier. </li></ul><ul><li>JDBC code when not used properly can wreak havoc on the long-term maintainability of a system. </li></ul><ul><li>Failure to clearly separate the data access layer into a well-defined set of interfaces creates tight dependencies in: </li></ul><ul><ul><li>The business objects consuming the data in the database. Change the structure or relationships in your database and you have to change your applications. </li></ul></ul><ul><ul><li>Exposes your applications to vendor specific database extensions that make it difficult to port your applications to another platform. </li></ul></ul>The Data Access Tier (p. 1)
  11. 11. JDBC Session 5 <ul><li>The Solution to “Data Madness” </li></ul><ul><li>Apply common J2EE Data Access Patterns that will allow us to hide all JDBC access code behind a well-defined layer. </li></ul><ul><li>Provide an OO-based set of objects for manipulating relational data (Object-Relational mapping). </li></ul><ul><li>Have a clearly defined data persistence framework that: </li></ul><ul><ul><li>Encourages a clean separation of data-persistence logic from presentation and business logic. </li></ul></ul><ul><ul><li>Decouples the application(s) built on the framework from having any knowledge of the database platform the data is residing in. </li></ul></ul><ul><ul><li>Abstracts away the physical details of how data is stored within a database and also the relationships that exist between entities in the database. </li></ul></ul><ul><ul><li>Simplifies the development process by hiding the details associated with getting a database connection, issuing a command and managing transactions. </li></ul></ul>The Data Access Tier (p. 2)
  12. 12. JDBC Session 5 <ul><li>The three patterns we’ll study in this lesson are: </li></ul><ul><li>Data Access Object Pattern (DAO) </li></ul><ul><li>Value Object Pattern (VO) </li></ul><ul><li>DataAccessException </li></ul>Data Access Patterns (p. 1)
  13. 13. JDBC Session 5 <ul><li>Here’s a class diagram of the architecture: </li></ul><ul><li>The DataAccessObject (DAO) is the primary mechanism for retrieving and manipulating data from the data base. </li></ul><ul><li>The ValueObject (VO) wraps data retrieved from a relational database in a Java object. </li></ul>Data Access Patterns (p. 2)
  14. 14. JDBC Session 5 <ul><li>The Data Access Object consists of an Interface called DataAccessObject.java </li></ul><ul><li>The two functions of the DAO Pattern: </li></ul><ul><li>Completely abstract away the data source in which the data the user is requesting resides. </li></ul><ul><li>Completely abstracts away all of the CRUD (Create, Replace, Update, Delete) logic normally associated with accessing a data source. </li></ul>DataAccessObject (p. 1)
  15. 15. JDBC Session 5 <ul><li>The DAO pattern abstracts away the tasks associated with retrieving and manipulating data by defining a Java interface that has 5 basic methods: </li></ul><ul><li>findByPrimaryKey( Object pPrimaryKey ) </li></ul><ul><li>createValueObject() </li></ul><ul><li>insert( ValueObject pValueObject ) </li></ul><ul><li>update( ValueObject pValueObject ) </li></ul><ul><li>delete( ValueObject pValueObject ) </li></ul>DataAccessObject (p. 2)
  16. 16. JDBC Session 5 Remember, DataAccessObject.java is an interface , so to use it we define one or more member classes that implement the interface: DataAccessObject (p. 3)
  17. 17. JDBC Session 5 <ul><li>The Value Object consists of an abstract class called ValueObject.java </li></ul><ul><li>A Value Object Pattern wraps a row of data retrieved from a data source in a Plain Old Java (POJ) class: </li></ul><ul><li>This lets the user deal with a logical view of the data instead of a physical view. </li></ul><ul><ul><li>Value objects hide the data types of the data being retrieved. </li></ul></ul><ul><ul><li>Value objects hide the relationships that exist between tables in the database. </li></ul></ul><ul><li>Value objects are the glue that ties the tiers together. They are used to pass data back and forth between all tiers in the application. </li></ul>ValueObject (p. 1)
  18. 18. JDBC Session 5 <ul><li>The VO class has 3 status fields with their own get() and set() methods that are used to tell the DAO what to do with the VO: </li></ul><ul><li>boolean insertFlag </li></ul><ul><li>boolean updateFlag </li></ul><ul><li>boolean deleteFlag </li></ul><ul><li>At any given time, only 1 of the flags may be true . Setting any one of the flags resets the others to false . The VO class also provides a reset() method that clears all flags to false . </li></ul><ul><li>Finally, the VO class provides get() and set() methods for a rowVersion field. long rowVersion is used to implement the framework’s locking strategy. </li></ul>ValueObject (p. 2)
  19. 19. JDBC Session 5 Remember, ValueObject.java is abstract , so to use it we define one or more member classes that extend the VO: ValueObject (p. 3)
  20. 20. JDBC Session 5 <ul><li>Never allow data access specific exceptions to be thrown from the data access objects. </li></ul><ul><li>Capture all exceptions thrown and re-throw them under a generic data access tier. </li></ul><ul><li>The data access tier should be responsible for logging and handling all errors thrown from it. </li></ul>DataAccessException (p. 1)
  21. 21. JDBC Session 5 <ul><li>Example on Web Site: </li></ul><ul><li>Lessons / JDBC / JDBC Session 5 / DataAccessExample </li></ul>Finally an Example!
  22. 22. JDBC Session 5 <ul><li>For 1000 no-points: </li></ul><ul><li>What other example have we looked at that kinda uses this pattern? </li></ul>Test Question
  23. 23. JDBC Session 5 Designing a Framework (p. 1) <ul><li>Layout your framework structure immediately. It’s very difficult to refactor out packages later on. </li></ul><ul><li>Keep the structure simple. </li></ul><ul><li>Centralize responsibility for the code in one team. Remember this code is supposed to be shared across applications. </li></ul>
  24. 24. JDBC Session 5 <ul><li>In the enterprise, applications come and go, but the data in these systems lives on. </li></ul><ul><li>A persistence framework defines a standard set of interfaces for retrieving and manipulating data. Code re-use often comes from interface re-use. </li></ul><ul><li>A well-defined data access tier is often times one of the most re-used pieces of code within the enterprise. </li></ul><ul><li>Stick to your implementation. Do not let time constraints compromise the integrity of your architecture. </li></ul><ul><li>Architecture and J2EE Pattern Usage has to have a business case. Using patterns for the sake of patterns is self-destructive. </li></ul>Designing a Framework (p. 2)
  25. 25. JDBC Session 5 DAO’s: Not just about Relational Databases: Designing a Framework (p. 3) MemberDAO Relational Database JDBC MemberDAO Java Data Object JDO MemberDAO SOAP envelope Web Service MemberDAO XML (Xindice) XPath

×