4.2.4 - Data Source Architectural Patterns
4.2.4 - Data Source Architectural Patterns
Patterns
Krishnaprasad P.K
Slide 1
Syllabus – Module IV
• Defining EAI, Data-Level EAI, Application Interface-Level EAI.,
Method- Level EAI., User Interface-Level EAI, The EAI Process -
An Introduction to EAI and Middleware, Transactional Middleware
and EAI, RPCs, Messaging, and EAI, Distributed Objects and EAI,
Database- Oriented Middleware and EAI, Java Middleware and EAI,
Implementing and Integrating Packaged Applications—The General
Idea, XML and EAI, Message Brokers—The Preferred EAI Engine,
Process Automation and EAI. Layering, Organizing Domain Logic,
Mapping to Relational Databases, Web Presentation, Domain Logic
Patterns, Data Source Architectural Patterns, Object-Relational
Behavioral Patterns, Object-Relational Structural Patterns, Object-
Relational Metadata Mapping Patterns, Web Presentation Patterns,
Slide 2
Distribution Patterns, Offline Concurrency Patterns.
Data Source Architectural Patterns
3. Active Record
4. Data Mapper
Slide 3
Gateway
– Gateway: “An object that encapsulates access to an external system
or resource”.
– Object-oriented system often has to deal with things that aren't
objects, such as relational database tables
– Wrap all the special API code of external resources like DB into a
class whose interface looks like a regular object.
– Other objects access the resource through this Gateway, which
translates the simple method calls into the appropriate specialized
API.
– Gateway is helpful if we are changing the external resources.
Slide 4
Source: Martin Fowler, “Patterns of Enterprise Application Architecture”
Table Data Gateway
– Two main ways (w.r.t Datasource) in which you can use a Gateway
– A Table Data Gateway(TDG) holds all the SQL for accessing a single
table: selects, inserts, updates, and deletes.
– Other code calls TDG’s methods for all interaction with the
database.
Slide 5
Source: Martin Fowler, “Patterns of Enterprise Application Architecture”
Table Data Gateway: How it works
– It usually consisting of several find methods to get data from the
database and update, insert, and delete methods.
– Each method maps the input parameters into a SQL call and executes
the SQL against a database connection.
Slide 6
Table Data Gateway
– Table Data Gateway is probably the simplest database interface
pattern to use.
– It provides good decoupling.
– Good choice for Table Module .
– Approach widely used in .NET
– Table Data Gateway is also suitable for Transaction Scripts.
Slide 7
Row Data Gateway
– Row Data Gateway: “An object that acts as a Gateway to a single
record in a data source. There is one instance per row”
– The Row Data Gateway will usually do any type conversion from the
data source types to the in-memory types.
– A Row Data Gateway should contain ONLY database access logic and
NO domain logic.
Slide 9
Source: Martin Fowler, “Patterns of Enterprise Application Architecture”
Row Data Gateway:
Slide 10
Source: Martin Fowler, “Patterns of Enterprise Application Architecture”
Active Record
– “An object that wraps a row in a database table, encapsulates the
database access, and adds domain logic on that data.”
– An object carries both data and behaviour.
– Active Record uses the most obvious approach, putting data access
logic in the domain object. This way all people know how to read and
write their data to and from the database.
Slide 11
Source: Martin Fowler, “Patterns of Enterprise Application Architecture”
Active Record: How it works
– The data structure of the Active Record should exactly match that
of the database. (one field in the class for each column in the table)
– In this pattern the classes are convenient, but they don't hide the
fact that a relational database is present.
– The Active Record class typically has methods that do the following:
– Update the database and insert into it the data in the Active
Record.
– Get and set the fields. (convert from SQL-oriented types to better
in-memory types.)
– It's easy to build Active Records, and they are easy to understand.
– Primary problem is that they work well only if the Active Record
objects correspond directly to the database tables: an isomorphic
schema.
Slide 15
Source: Martin Fowler, “Patterns of Enterprise Application Architecture”
Data Mapper
– With Data Mapper the in-memory objects needn't know even that
there's a database present; they need no SQL interface code, and
certainly no knowledge of the database schema.
Slide 16
Data Mapper: How It Works
– The separation between domain and data source is the main function
of a Data Mapper.
– Mapper uses Identity Map to see if object is already loaded. If not it
loads it.
– Identity Map: “Ensures that each object gets loaded only once by
keeping every loaded object in a map“
Retrieving data from a database.
Slide 17
Source: Martin Fowler, “Patterns of Enterprise Application Architecture”
Data Mapper: How It Works
– For insert and updates: The mapper must know what objects have
changed, which are new, and which must be destroyed.
Slide 18
Data Mapper: When to Use It
Slide 19
Reference
[1] Martin Fowler, David Rice, Matthew Foemmel, Edward Hieatt, Robert Mee,
Randy Stafford, “Patterns of Enterprise Application Architecture”,
Addison- Wesley, 2002.
Slide 20
Source: Martin Fowler, “Patterns of Enterprise Application Architecture”