当前位置:网站首页>Row data gateway mode

Row data gateway mode

2021-05-04 16:52:13 Jiedao jdon

This mode belongs to   Data source architecture pattern Catalog , This directory belongs to   Enterprise application architecture pattern .

Purpose

An object , Act as a gateway to a single record in the data source . Each line has an instance .

here Gateway Objects that encapsulate access to external systems or resources .

The object does not contain domain logical methods . If other methods are introduced ( In particular, domain logic ), Then the object becomes the active record mode .

How to operate

The row data gateway acts as an object that fully emulates a single record , For example, a database row . for example ,person Class has one. id、firstname and lastname Field .

public class Person{
    private int id;
    private String firstName;
    private String lastName;
    // Getter and Setter methods
}

Person Java Class Person surface

ID    name         surname

1      Ramesh      Fadatare

This mode holds data about the row , So that the client can directly access the row data gateway . The gateway acts as a good interface for each line of data . This method is especially suitable for Transaction Scripts.

explain

Embedding database access code in memory objects may bring some disadvantages . First , If an object in memory has its own business logic , Then adding database operation code will increase the complexity . secondly , The test was embarrassing, too , Because if an object in memory is bound to a database , Because of all database access , The test runs slowly . You may need to access multiple databases , It contains all of these annoying SQL Small changes .

The row data gateway provides you with objects that are exactly like the records in the record structure , But you can access these objects using the normal mechanisms of the programming language . All details of data source access are hidden behind this interface .

When to use it

This data gateway is widely used in transaction script mode . under these circumstances , It's a good decomposition of the database access code , And allow different transaction scripts to easily reuse it .

Sample code

Let's make PersonGateway To create a class diagram ,   To demonstrate this pattern .

This is an example of row data gateway  . This is a simple personnel list .

create table people (ID int primary key, lastname varchar, 
                     firstname varchar, number_of_dependents int)

PersonGateway It's the portal to this table . It starts with data fields and accessors .

class PersonGateway... 

   private String lastName;
   private String firstName;
   private int numberOfDependents;
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public int getNumberOfDependents() {
      return numberOfDependents;
   }
   public void setNumberOfDependents(int numberOfDependents) {
      this.numberOfDependents = numberOfDependents;
   }

The gateway class itself can handle updates and inserts .

class PersonGateway... 

   private static final String updateStatementString =
         "UPDATE people " +
         "  set lastname = ?, firstname = ?, number_of_dependents = ? " +
         "  where id = ?";
   public void update() {
      PreparedStatement updateStatement = null;
      try {
         updateStatement = DB.prepare(updateStatementString);
         updateStatement.setString(1, lastName);
         updateStatement.setString(2, firstName);
         updateStatement.setInt(3, numberOfDependents);
         updateStatement.setInt(4, getID().intValue());
         updateStatement.execute();
      } catch (Exception e) {
         throw new ApplicationException(e);
      } finally {DB.cleanUp(updateStatement);
      }
   }
   private static final String insertStatementString =
         "INSERT INTO people VALUES (?, ?, ?, ?)";
   public Long insert() {
      PreparedStatement insertStatement = null;
      try {
         insertStatement = DB.prepare(insertStatementString);
         setID(findNextDatabaseId());
         insertStatement.setInt(1, getID().intValue());
         insertStatement.setString(2, lastName);
         insertStatement.setString(3, firstName);
         insertStatement.setInt(4, numberOfDependents);
         insertStatement.execute();
         Registry.addPerson(this);
         return getID();
      } catch (SQLException e) {
         throw new ApplicationException(e);
      } finally {DB.cleanUp(insertStatement);
      }
   }

To extract the person row from the database , We have a separate  PersonFinder. It works with the gateway to create a new gateway object ..

class PersonFinder... 

   private final static String findStatementString =
         "SELECT id, lastname, firstname, number_of_dependents " +
         "  from people " +
         "  WHERE id = ?";
   public PersonGateway find(Long id) {
      PersonGateway result = (PersonGateway) 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 = PersonGateway.load(rs);
         return result;
      } catch (SQLException e) {
         throw new ApplicationException(e);
      } finally {DB.cleanUp(findStatement, rs);
      }
   }
   public PersonGateway find(long id) {
      return find(new Long(id));
   }
class PersonGateway...

   public static PersonGateway load(ResultSet rs) throws SQLException {
         Long id = new Long(rs.getLong(1));
         PersonGateway result = (PersonGateway) 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 PersonGateway(id, lastNameArg, firstNameArg, numDependentsArg);
         Registry.addPerson(result);
         return result;
   }

In order to find multiple people according to certain criteria , We can provide an appropriate search method .

class PersonFinder... 

   private static final String findResponsibleStatement =
         "SELECT id, lastname, firstname, number_of_dependents " +
         "  from people " +
         "  WHERE number_of_dependents > 0";
   public List findResponsibles() {
      List result = new ArrayList();
      PreparedStatement stmt = null;
      ResultSet rs = null;
      try {
         stmt = DB.prepare(findResponsibleStatement);
         rs = stmt.executeQuery();
         while (rs.next()) {
            result.add(PersonGateway.load(rs));
         }
         return result;
      } catch (SQLException e) {
         throw new ApplicationException(e);
      } finally {DB.cleanUp(stmt, rs);
      }
   }

The finder uses registry mode to save the identity mapping .

We can now use the gateway in transaction script mode

PersonFinder finder = new PersonFinder(); 
Iterator people = finder.findResponsibles().iterator();
StringBuffer result = new StringBuffer();
while (people.hasNext()) {
   PersonGateway each = (PersonGateway) people.next();
   result.append(each.getLastName());
   result.append("  ");
   result.append(each.getFirstName());
   result.append("  ");
   result.append(String.valueOf(each.getNumberOfDependents()));
   result.append("

}
return result.toString();

版权声明
本文为[Jiedao jdon]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/05/20210504164657725g.html

随机推荐