简体   繁体   English

具有数据库访问权限的OOP编程

[英]OOP Programming with database access

I am working on a project with database access. 我正在一个具有数据库访问权限的项目。 Here is the outline of the classes i have implemented 这是我已经实现的类的概述

public class Foo {

    private String id;
    private String name;
    private int x;
    private String y;
    private String z;
    ...

    public Foo(String id) throws SQLException {
       this.id=id;
       try (Statement stmt = MyConnectionManager().getConnection().createStatement()) {
          ResultSet rs = stmt.executeQuery("SELECT * FROM " + TABLE_NAME
                + " WHERE(id='" + this.id + "')");
          if (!rs.next()) {
             throw new NoExistException();
          }
          this.name = rs.getString("Name");
          this.x = ...
          ...
       } catch (SQLException ex) {
          throw ex;
       }
    }
    public int getId() {
       return this.id;
    }

    public String getName() {
    .
    .
    .

But when i looked at most oop samples i have found that most people use an additional class for database access. 但是,当我查看大多数oop示例时,我发现大多数人都使用其他类来访问数据库。 I don't know what im going to say is a blunder. 我不知道我要说的是什么错误。 Those codes access database for initializing each member variable. 这些代码访问数据库以初始化每个成员变量。 But in my code database is accessed only once to initialize my object. 但是在我的代码数据库中可以访问一次以初始化我的对象。 Should i change this approach. 我应该改变这种方法。 If i change will it affect the speed of my application when it wants to initialize more objects together (may be hundreds). 如果我更改, 它将在希望一起初始化更多对象(可能是数百个)时影响我的应用程序的速度 I think this might be a duplication question. 我认为这可能是一个重复的问题。 But i didn't find any satisfactory answer for my particular problem. 但是对于我的特定问题,我没有找到满意的答案。

It is a design choice, some people like to modularize their program, it is regarded as a good practice since modification and maintenance do not affect other classes. 这是一种设计选择,某些人喜欢将其程序模块化,因此,由于修改和维护不会影响其他类,因此这是一个好习惯。

If you create a new class for your database access (Refactoring), it allows other classes to access this class, as well, if they require database access. 如果您为数据库访问(重构)创建一个新类,则如果其他类需要数据库访问,它也会允许其他类访问该类。

You approach violates the Single Responsibility Principle and Separation of Concerns - basically a class shouldn't be both a domain class and access the database. 您的方法违反了“ 单一职责原则”“关注点分离” -基本上,一个类不应同时是域类和访问数据库。

An approach where each member variable is independently initialized and that initialization involves a database call is even worse and is not going to work either - as well as violating the previously mentioned Single Responsibility Principle and Separation of Concerns it simply won't perform. 每个成员变量都被独立初始化并且涉及数据库调用的初始化的方法更加糟糕,而且也行不通-并违反了之前提到的“ 单一职责原则”和“根本不会执行的关注点分离”

The way this is most commonly handled is through a data access layer where you have class that performs the database access and maps the result sets to new instances of your domain classes. 最常见的处理方法是通过数据访问层 ,在该层中 ,您具有执行数据库访问并将结果集映射到域类的新实例的类。

What this means in your case is you'd end up with DAO interface that might look something like this: 在您的情况下,这意味着您将最终获得可能类似于以下内容的DAO接口:

public interface FooDao
{
  public Collection<Foo> getAll() throws DaoException;

  public Foo getByIdentity(String id) throws DaoException;

  public Collection<Foo> getByName(String name) throws DaoException;

  public void save(Collection<Foo> foos) throws DaoException;
}

The interface might have a series of get methods or expose some sort of criteria based mechanism to allow you to construct more elaborate queries (there's some code I wrote a while back that does this here ) but the important thing is that it is this interface all your code uses. 该接口可能具有一系列的get方法或公开某种基于标准的机制,以使您能够构建更复杂的查询(我之前写过一些代码在此处进行了此操作 ),但重要的是该接口全部您的代码使用。 You'd then implement this interface as you please (using direct JDBC if your object model is simple or Hibernate if you need something more advanced) or mock it in your unit tests. 然后,您可以根据需要实现此接口(如果对象模型简单,则使用直接JDBC;如果需要更高级的对象,则使用Hibernate),或者在单元测试中对其进行模拟。

When you introduce a Bar class you'd repeat the pattern defining a BarDao and implementing as necessary again. 引入Bar类时,您将重复定义BarDao的模式,并根据需要再次实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM