28
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler http://www.cs.concordia.ca/~gregb/home/ soen343h-f06.html

SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Embed Size (px)

Citation preview

Page 1: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

SOEN 343Software Design

Section H Fall 2006

Dr Greg Butlerhttp://www.cs.concordia.ca/~gregb/home/soen343h-f06.html

Page 2: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Outline• Data Gateways

– GetByLastNameEA

• Enterprise Applications:– SoenEA framework and samples.

• Controller patterns– Focus: Page Controller

Page 3: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Dom

ain

Data

Sou

rce

Table Data Gateway

Transaction Script Domain Model

Data MapperRow Data Gateway

Front Controller

Template View

Transform View

Page Controller

Pres

enta

tion

Active Record

Enterprise Application Patterns

Page 4: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Dom

ain

Data

Sou

rce

Table Data Gateway

Transaction Script Domain Model

Data MapperRow Data Gateway

Active Record

Front Controller

Template View

Transform View

Page Controller

Pres

enta

tion Data Source Patterns

Page 5: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Row Data Gateway

• An object that acts as a single record in the data source– There is one instance per row

• Fowler RDG combines two rolesClass …Finder with find(id):Gateway method

which returns the ‘object’

Class …Gateway which is the ‘object’

• Our PersGradeRDG (next slide) combines

Page 6: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Row Data Gateway

PersGradeRDG- name : String

- grade : int

+ PersGradeRDG(name, grade)

+ find(name) : PersGradeRDG

+ insert() : void

+ update() : void

+ delete() : void

+ … getters and setters …

Page 7: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Row Data Gateway: Find Code

Page 8: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Row Data Gateway

• Code sample for Fowler’s PersonRDG

• Note ‘find’ method is static– it is class method

Page 9: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

GetByLastName Example

Simple web application toInput a student’s last name

Fetch database record

Output student’s full name

Page 10: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

GetByLastName

Will look atRow Data Gateway

PageController

Transaction Script

Page 11: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Row Data Gateway

• StudInfoRDG– Represents record in DB of student

• StudInfoFinder– Finds student record based on lastName– Returns the StudInfoRDG– Locates DB using DBRegistry

Page 12: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Outline

• Enterprise Applications:– SoenEA framework and samples

– Simple sample: GetByLastName

– Builds on HelloWeb

Page 13: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Package structure

Page 14: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Dom

ain

Data

Sou

rce

Table Data Gateway

Transaction Script Domain Model

Data MapperRow Data Gateway

Front Controller

Template View

Transform View

Page Controller

Pres

enta

tion

Active Record

Enterprise Application Patterns

Page 15: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

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.

Implementation can be– shared among subroutines.– Subroutines can be used by more than one script

Page 16: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

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)

Page 17: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

GetByLastName

Will look atRow Data Gateway

PageController

Transaction Script

Page 18: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Do-it-all Transaction Script

A TS that does all the work itselfWithout a Finder

Without a RDG

Directly calls DB

Page 19: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Do-it-all Servlet or Script, Class

public class DoItAll extends javax.servlet.http.HttpServlet

{protected void doGet(

HttpServletRequest request,HttpServletResponse response)throws ... //

{…}

}

Page 20: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Do-it-all Transaction Script, doGet()

String lastName = request.getParameter(“…"); Connection dbc = DbRegistry.getDbConnection();String findSql = "SELECT * from … where LastName = ?”;PreparedStatement dbQuery = dbc.prepareStatement(findSql);

dbQuery.setString(1, lastName);ResultSet rs = dbQuery.executeQuery();rs.next();String firstName = rs.getString("FirstName");lastName = lastName.toUpperCase();PrintWriter out = response.getWriter();response.setContentType("text/html");out.println("<h1>Hello "+firstName+" "+lastName+"</h1>");

Page 21: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Outline

• Controller patterns– Focus: Page Controller

Page 22: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Dom

ain

Data

Sou

rce

Table Data Gateway

Transaction Script Domain Model

Data MapperRow Data Gateway

Active Record

Front Controller

Template View

Transform View

Page Controller

Pres

enta

tion Controller Patterns

Page 23: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Page Controller

Fowler:A Page Controller is an object that handles a

request for a specific page or action on a web site.

There is one input controller for each logical page of the web site.

Page Controller handles the http get and post

Decides which model and view to use

Page 24: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Page Controller

Responsibilities:– Decode the URL, extract any form data, decide action– Create and invoke any model objects

• All relevant data from the html request should be passed to the model, so the model does not need any connection to html request

– Determine which view should display the result page• Forward model information to it

Helper objects can be created/used to perform tasks that are shared

Page 25: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Page ControllerFowler Example:

display info about a recording artist– ActionServlet implements common ‘forward’ task– ArtistController implements doGet()

• Checks artist name is ok• Creates a new ArtistHelper object with Artist as attribute• Adds helper information to html request• Forwards request to Artist.jsp

– Artist.jsp is a TemplateView which gets information to display from helper object (which it gets from request)

Page 26: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Page Controller

Page 27: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

GetByLastName

Will look atRow Data Gateway

PageController

Transaction Script

Page 28: SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler gregb/home/soen343h-f06.html

Page ControllerSOEN EA sample:display info about a student

– PageController implements doGet()• Delegates to transaction script

Note:does not follow Fowler’s use of .jspdelegates rather than forwardsno explicit helper object

(really StudInfoRDG is helper)