什么是DAO架构的必要性 [英] What is the necessity of DAO Architecture

查看:127
本文介绍了什么是DAO架构的必要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 在Java编程根据DAO架构是它总是需要code?如果是的话是什么使用它的优势是什么?

  1. When programming in Java is it always necessary to code according to the DAO architecture? If so what are advantages of using it?

我在做它有像下面的类图的项目。这有什么缺点呢?

I'm doing a project which has a class diagram like below. what are the disadvantages of this?

实体类:

private void fillSONumber() {
    try {
        ZnAlSalesOrder o = new ZnAlSalesOrder();
        ArrayList a = o.getPendingSalesOrderIDs();
        for (int i = 0; i < a.size(); i++) {
            cmbSoNo.addItem(a.get(i));
        }
        o.close();
    } catch (Terminated ex) {

    }
}

EntityTable类示例:

EntityTable Class Example:

public ResultSet select(String fields, String selection) {
    db = new Database();
    db.select("SELECT " + fields + " FROM " + name + " WHERE " + selection);
    return rs = db.rs;
}

和数据库类做连接的建立和破坏。

and the database class do the connection establishment and destroying.

推荐答案

一个DAO模式的目的是分离你从它的存储方式试图访问的数据。

The purpose of a DAO pattern is to separate what data you are trying to access from how it is stored.

例如,你可以创建一个DAO,指定了一些方法,您再实施打击的MySQL。如果你决定你需要移动到MSSQL或Oracle,你只需要改变的实施,并不可能在许多不同的地方在code使用的接口。

For example, you could create a DAO that specifies a number of methods that you then implement against MySQL. If you ever decide you need to move to MSSQL or Oracle, you only need to change the implementation, not the interface that could be used in a number of different places in your code.

这是没有必要做到这一点,但它可以是一个好主意,使未来的变化更容易,让您的code分离。

It is not necessary to do this, but it can be a good idea to make future changes easier and keep your code decoupled.

至于您的设计,基本布局是好的,但我不推荐的通用方法,选择像你有。你基本上只是创造抽象的另一层东西的地方可能出错,没有任何额外的好处。

As for your design, the basic layout is fine, but I would recommend against a generic select method like you have. You are basically just creating another layer of abstraction where something could go wrong without any extra benefit.

这将很好的工作简单的查询,但如果你需要做任何连接,你很快就会有一个大的烂摊子,为不同的连接类型的方法结束。

It will work well for simple queries, but if you need to do any joins, you will quickly end up with a large mess of methods for different join types.

这是更好地只写你的SQL为每个需要访问和创建一个返回您想要的数据类型的方法类型的数据。这将减少您的耦合,并允许您根据需要更改您的实现。

It is better to just write your SQL for each type of data you need to access and create a method that returns the type of data you want. This reduces your coupling and allows you to change your implementation if needed.

这篇关于什么是DAO架构的必要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆