0% found this document useful (0 votes)
44 views66 pages

Software Design: Layers Detailed

No summary provided.

Uploaded by

Lia Feier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views66 pages

Software Design: Layers Detailed

No summary provided.

Uploaded by

Lia Feier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 66

8/25/2019 Computer Science Department, TUC-N

SOFTWARE DESIGN
Layers detailed
8/25/2019 Computer Science Department, TUC-N

Content
• Patterns for Enterprise Application Architecture [Fowler]
• Domain Layer Patterns
• Transaction Script
• Domain Model
• Table Module
• Active Record
• Data Source Patterns
• Row Data Gateway
• Table Data Gateway
• Data Mapper
• Presentation
• Template View
• Transform View
• Page Controller
• Front Controller
8/25/2019 Computer Science Department, TUC-N

References
• Martin Fowler et. al, Patterns of Enterprise Application
Architecture, Addison Wesley, 2003 [Fowler]
• Univ. of Aarhus Course Materials
• Univ. of Utrecht Course Materials
8/25/2019 Computer Science Department, TUC-N

Patterns for Enterprise Applications [Fowler]


• Persistent data
• Volume of data
• Concurrent access
• Complicated user interface
• Integration with other applications
• Conceptual dissonance
• Business logic
8/25/2019 Computer Science Department, TUC-N

Enterprise applications
• Example: B2C online retailer
• High volume of users: scalability
• Example: processing of leasing agreements
• Complicated business logic
• Rich-client interface
• Complicated transaction behavior
• Example: expense tracking for small company
8/25/2019 Computer Science Department, TUC-N

Principal layers
• See pattern Layers in [POSA]
• Here: applied to enterprise
applications
• Presentation logic
• Interaction with user
• Command-line or rich client or Web
interface
• Domain logic
• Validation of input and calculation of
results
• Data source logic
• Communication with database and
other applications
8/25/2019 Computer Science Department, TUC-N
Data Source Domain Presentation EA Patterns

Page Controller Template View

Front Controller Transform View

Domain Model
Transaction Script

Active Record Table Module


Data Mapper

Row Data Gateway Table Data Gateway


8/25/2019 Computer Science Department, TUC-N

Basic Patterns
• Gateway
• Record Set
8/25/2019 Computer Science Department, TUC-N

Gateway
An object that encapsulates access to an external system
or resource
8/25/2019 Computer Science Department, TUC-N

Gateway – How it works


• External resources - each with its own API
• Wraps all the API’s into a common Gateway

• Sometimes, better to have 2 objects:


• Back-end - acts as a minimal overlay to the external resource and
does not simplify the API of the external resource at all
• Front-end - transforms the awkward API into one that's more
convenient for your application to use.
8/25/2019 Computer Science Department, TUC-N

Gateway - Benefits
• Easier handling of awkward API’s

• Easier to swap out one kind of resource for another

• Easier to test by giving you a clear point to deploy Service


Stubs (A stand-in implementation of an external service)
8/25/2019 Computer Science Department, TUC-N

Gateway - Example
build a gateway to an interface that just sends a message
using the message service

int send(String messageType, Object[] args);

Confirmation message
messageType = ‘CONFIRM’;
args[0] = id;
args[1] = amount;
args[2] = symbol;
8/25/2019 Computer Science Department, TUC-N

Better…
8/25/2019 Computer Science Department, TUC-N

Gateway vs. Façade vs. Adapter


• The facade is usually done by the writer of the service for general
use, while a Gateway is written by the client for their particular use.

• A facade always implies a different interface to what it's covering,


while a Gateway may copy the wrapped interface entirely, being used
for substitution or testing purposes

• Adapter alters an implementation's interface to match another


interface which you need to work with.

• With Gateway there usually isn't a an existing interface, although you


might use an adaptor to map an implementation to a an existing
Gateway interface. In this case the adaptor is part of the
implementation of the Gateway
8/25/2019 Computer Science Department, TUC-N

Record Set
An in-memory representation of tabular data
8/25/2019 Computer Science Department, TUC-N

Record Set – how it works


• provides an in memory structure that looks exactly like the
result of a SQL query, but can be generated and
manipulated by other parts of the system.
• Examples:
• DataSet of ADO.NET
• RowSet of JDBC

A disconnected Record Set is one that is separated from


it's link to the data source
8/25/2019 Computer Science Department, TUC-N

Domain Logic (Layer)


• “… also referred to as business logic. … It involves
calculations based on inputs and stored data, validation of
any data that comes in from the presentation, and figuring
out exactly what data source logic to dispatch …” [Fowler]
8/25/2019 Computer Science Department, TUC-N

Organizing the Domain Logic


• Key architectural decisions, which influence structure of
other layers.

• Pure patterns
• Transaction Script
• Domain Model

• Hybrid patterns
• Active Record
• Table Module.
8/25/2019 Computer Science Department, TUC-N

Domain Logic Patterns


Presentation

Page Controller Template View

Front Controller Transform View


Data Source Domain

Transaction Script Domain Model

Table Module Active Record

Table Data Gateway Row Data Gateway Data Mapper


8/25/2019 Computer Science Department, TUC-N

Transaction Script

Fowler: A TS organizes the business logic primarily as a


single procedure where each procedure handles a
single request from the presentation.

The TS may make calls directly to the DB or through a


thin DB wrapper.

Think of a script for: a use case or business transaction.


8/25/2019 Computer Science Department, TUC-N

Transaction Script

• … is essentially a procedure that takes the


• input from the presentation,
• processes it with validations and calculations,
• stores data in the database,
• (invokes any operations from other systems, and)
• replies with more data to the presentation perhaps
doing more calculation to help organize and format the
reply data.
[Fowler]
8/25/2019 Computer Science Department, TUC-N

TS Features
• Business logic is organized by procedures

• Each procedure handles a single transaction

• Transaction: well-defined endpoint

• Must be complete on all-or-nothing basis

• Makes call directly to the database

• May be organized as:


• a separate class/TS (Command pattern)
• several TS/class
8/25/2019 Computer Science Department, TUC-N

Example (http://lorenzo-dee.blogspot.ro/2014/06/quantifying-
domain-model-vs-transaction-script.html)
• Banking application
• Money transfer functionality
8/25/2019 Computer Science Department, TUC-N
8/25/2019 Computer Science Department, TUC-N

Analysis
• Strenghths
• Simplicity

• Weaknesses
• complicated transaction logic
• duplicated logic
8/25/2019 Computer Science Department, TUC-N

Domain Model (EA Pattern)


Fowler: An object model of the domain that
incorporates both behaviour and data.

A DM creates a web of interconnected objects,


where each object represents some meaningful
individual, whether as large as a corporation or
as small as a single line in an order form.
8/25/2019 Computer Science Department, TUC-N

Domain Model (EA Pattern)


• Realization (via design classes) of
UML Domain Model (conceptual classes).
 E.g. person, book, shopping cart, task, sales line item,

• Domain Model classes contain Logic for handling
validations and calculations.
 E.g. a shipment object calculates the shipping charge
for a delivery.
8/25/2019 Computer Science Department, TUC-N

DM Features
• Business logic is organized as an OO model of the
domain
• Describes both data and behavior
• Different from database model
• Process, multi-valued attributes, inheritance, design patterns
• Harder to map to the database

• Risk of bloated domain objects


8/25/2019 Computer Science Department, TUC-N
8/25/2019 Computer Science Department, TUC-N
8/25/2019 Computer Science Department, TUC-N

Choosing a Domain Logic Pattern


• Which one to choose?
• Influenced by the complexity of domain logic.
• Application is simple access to data sources
 Transaction Script, (or Active Record, Table Module)
• Significant amount of business logic
 Domain Model
• TS is simpler:
• Easier and quicker to develop and maintain.
• But can lead to duplication in logic / code.
• DM – difficult access to relational DB

• Which would be easier to refactor?


TS->DM or DM-> TS
8/25/2019 Computer Science Department, TUC-N

Towards Data Source Patterns


• Hybrid patterns.
• Active Record
• Table Module

• Pure patterns.
• Row Data Gateway,
• Table Data Gateway,
• Data Mapper
• …
8/25/2019 Computer Science Department, TUC-N

Data Source Patterns


Presentation

Page Controller Template View

Front Controller Transform View


Data Source Domain

Transaction Script Domain Model

Table Module Active Record

Table Data Gateway Row Data Gateway Data Mapper


8/25/2019 Computer Science Department, TUC-N

Table Module
• Provide a single object for all the behavior on a table

• Organizes domain logic with one class per table

• Table Module has no notion of an identity for the objects


that it's working with
Id references are necessary
8/25/2019 Computer Science Department, TUC-N

Typical interactions for TM


8/25/2019 Computer Science Department, TUC-N

TS vs TM
8/25/2019 Computer Science Department, TUC-N

TS vs TM
8/25/2019 Computer Science Department, TUC-N

Example - Revenue Recognition (RR)


• Revenue recognition is a common problem in
business systems.
• when you can actually count the money you receive on your
accounting books.

• E.g. selling a S/W package $120 today


• Book $40 today,
• $40 in 30 days,
• $40 in 60 days.
[Fowler]
8/25/2019 Computer Science Department, TUC-N

Revenue Recognition concepts


• Product type: description of item to be sold

• Contract: covers only one product.

• Revenue recognition: varies per product type.

• For each product instance: a set of revenue


recognition instances
8/25/2019

TS: Calculating Revenue Recognitions


8/25/2019

Implementation
• Database

CREATE TABLE products (ID int primary key,


name varchar, type varchar)

CREATE TABLE contracts (ID int primary key,


product int, revenue decimal, dateSigned
date)

CREATE TABLE revenueRecognitions (contract


int, amount decimal, recognizedOn date,
PRIMARY KEY (contract, recognizedOn))
8/25/2019

Implementation
• calculate the amount of recognition due by a
particular day:
- select the appropriate rows in the revenue recognitions
table,
- sum up the amounts.
8/25/2019

Gateway class
class Gateway...
public ResultSet findRecognitionsFor(long contractID,
MfDate asof) throws SQLException
{
PreparedStatement stmt =
db.prepareStatement(findRecognitionsStatement);

stmt.setLong(1, contractID);
stmt.setDate(2, asof.toSqlDate());

ResultSet result = stmt.executeQuery();


return result; }

private static final String findRecognitionsStatement


= "SELECT amount " + " FROM revenueRecognitions " + "
WHERE contract = ? AND recognizedOn <= ?";
private Connection db;
8/25/2019

RecognitionService class
class RecognitionService...
public Money recognizedRevenue(long contractNumber,
MfDate asOf)
{
Money result = Money.dollars(0);
try {
ResultSet rs =
db.findRecognitionsFor(contractNumber, asOf);
while (rs.next())
{
result =
result.add(Money.dollars(rs.getBigDecimal("amount")));
}
return result;
}
catch (SQLException e) {
throw new ApplicationException (e); }
}
8/25/2019

RR Domain Model
8/25/2019

Domain Model: Calculating Revenue


Recognitions
8/25/2019

Enhancement: e.g. New Revenue


Recognition Strategy
• Transaction Script:
• New conditional, or
• New subroutine.

• Domain Model:
• Create new Rev. Recog. Strategy class.
8/25/2019

RR updated Domain Model


8/25/2019

Implementation
class RevenueRecognition...
private Money amount;
private MfDate date;
public RevenueRecognition(Money amount, MfDate
date)
{
this.amount = amount;
this.date = date;
}
public Money getAmount()
{
return amount;
}
boolean isRecognizableBy(MfDate asOf)
{
return asOf.after(date) || asOf.equals(date);
}
8/25/2019

Contract class
class Contract...
private List revenueRecognitions = new
ArrayList();
public Money recognizedRevenue(MfDate asOf)
{
Money result = Money.dollars(0);
Iterator it =
revenueRecognitions.iterator();
while (it.hasNext())
{
RevenueRecognition r =
(RevenueRecognition) it.next();
if (r.isRecognizableBy(asOf))
result = result.add(r.getAmount());
}
return result;
}
8/25/2019

Introducing strategies…
class Contract...
private Product product;
private Money revenue;
private MfDate whenSigned;
private Long id;
public Contract(Product product, Money
revenue, MfDate whenSigned)
{
this.product = product;
this.revenue = revenue;
this.whenSigned = whenSigned;
}
8/25/2019

Introducing strategies …
class Product...
private String name;
private RecognitionStrategy recognitionStrategy;
public Product(String name, RecognitionStrategy
recognitionStrategy)
{
this.name = name;
this.recognitionStrategy = recognitionStrategy;
}
public static Product newWordProcessor(String name)
{
return new Product(name, new
CompleteRecognitionStrategy());
}
public static Product newSpreadsheet(String name)
{
return new Product(name, new
ThreeWayRecognitionStrategy(60, 90));
}
public static Product newDatabase(String name)
{
return new Product(name, new
ThreeWayRecognitionStrategy(30, 60)); }
8/25/2019

Introducing strategies …
class RecognitionStrategy...{
abstract void
calculateRevenueRecognitions(Contract
contract); }

class CompleteRecognitionStrategy...
void calculateRevenueRecognitions(Contract
contract)
{
contract.addRevenueRecognition(new
RevenueRecognition(contract.getRevenue(),
contract.getWhenSigned()));
}

class ThreeWayRecognitionStrategy...
8/25/2019 Computer Science Department, TUC-N

Introducing strategies
class Contract...
public void calculateRecognitions()
{

product.calculateRevenueRecognitions(this
);
}

class Product...
void calculateRevenueRecognitions(Contract
contract)
{
recognitionStrategy.calculateRevenueRecog
nitions(contract);
}
8/25/2019

TM in the RR example
8/25/2019

RR problem with TM
8/25/2019

Implementation (C#)
class TableModule...
protected DataTable table;
protected TableModule(DataSet ds,
String tableName)
{
table = ds.Tables[tableName];
}
8/25/2019

ContractModule subclass
class ContractModule...
public ContractModule (DataSet ds) : base
(ds, "Contracts") {}
public DataRow this [long key]
{
get
{
String filter = String.Format("ID =
{0}", key);
return table.Select(filter)[0];
}
}

contract = new ContractModule(dataset);


8/25/2019

RevenueRecognition class
class RevenueRecognition...
public Decimal RecognizedRevenue (long contractID,
DateTime asOf)
{
String filter = String.Format("ContractID =
{0} AND date <= #{1:d}#", contractID,asOf);
DataRow[] rows = table.Select(filter);
Decimal result = 0;
foreach (DataRow row in rows)
{
result += (Decimal)row["amount"];
}
return result;
}
8/25/2019 Computer Science Department, TUC-N

Which one to use?


8/25/2019 Computer Science Department, TUC-N

Active Record
Fowler: An object that wraps a record data
structure in an external resource, such as a row
in a database table, and adds some domain logic
to that object.

An AR object carries both data and behaviour.

The essence of an AR is a Domain Model in which


the classes match very closely the record
structure of the underlying database.
8/25/2019 Computer Science Department, TUC-N

Class operations

 construct an instance of the Active Record from a


SQL result set row
 construct a new instance for later insertion into the
table
 static finder methods to wrap commonly used SQL
queries and return Active Record objects
 methods to update the database and insert into the
database with the data in the Active Record
 getting and setting methods for the fields
 methods that implement some pieces of business
logic
8/25/2019 Computer Science Department, TUC-N

Implementation
class Person...
private String lastName;
private String firstName;
private int numberOfDependents;

create table people (ID int primary key,


lastname varchar, firstname varchar,
number_of_dependents int)
8/25/2019 Computer Science Department, TUC-N

Find + Load an object


class Person...
private final static String findStatementString = "SELECT
id, lastname, firstname, number_of_dependents" + " FROM
people" + " WHERE id = ?";
public static Person find(Long id)
{
Person result = (Person) Registry.getPerson(id);
if (result != null) return result;
PreparedStatement findStatement = null;
ResultSet rs = null;
try
{ findStatement = DB.prepare(findStatementString);
findStatement.setLong(1, id.longValue());
rs = findStatement.executeQuery();
rs.next();
result = load(rs);
return result;
} catch (SQLException e) { throw new
ApplicationException(e); }
finally { DB.cleanUp(findStatement, rs); }
}
8/25/2019 Computer Science Department, TUC-N

public static Person load(ResultSet rs)


throws SQLException
{
Long id = new Long(rs.getLong(1));
Person result = (Person)
Registry.getPerson(id);
if (result != null) return result;
String lastNameArg = rs.getString(2);
String firstNameArg = rs.getString(3);
int numDependentsArg = rs.getInt(4);
result = new Person(id, lastNameArg,
firstNameArg, numDependentsArg);
Registry.addPerson(result);
return result;
}
8/25/2019 Computer Science Department, TUC-N

Next Time
• More patterns

You might also like