GWT RPC:DTO与DAO? [英] GWT RPC: DTO vs. DAO?

查看:160
本文介绍了GWT RPC:DTO与DAO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一周前开始学习GWT,这里是我无法肯定回答的问题。



以下是服务器端:

  //业务对象 - 具有逻辑
interface Article {
String getTitle(); // lazy
void setTitle();
String getText(); // lazy
void setText();
Set< Comment> getComments(); // lazy
}

//也有逻辑
接口Comment {...}

我需要以某种方式创建一个GWT小部件来可视化 Article 。传递 Article Article 不起作用,因为它不是可序列化的,而且它有一些BL。所以,有两种方法:第一种是使用DTO:

  class ArticleDTO实现Serializable {
public int articleId;
public String title;
public String text;
}

class CommentDTO实现Serializable {
public int commentId;
public int articleId;
public String commentText;
}

我必须在我的GWT RPC服务中实现一个存储库逻辑:

  class MyRPCRepository ... {
ArticleDTO getArticle(int id);
void saveArticle(ArticleDTO文章);
void deleteArticle(ArticleDTO文章);
...类似的东西在这里注释...
}

<第二种方法是使用DAO:

  class ArticleDAO实现了Serializable {
private transitional MyRPC RPC;
private int articleId; //只有这一个是可序列化的
$ b $ public ArticleDAO(MyRPC rpc,int articleId){...}

public String getTitle(){
// i知道这将需要更多的代码在实际
返回rpc.getArticleTitle(articleId);
}
...
}

我喜欢第一个一个,因为它真的很愚蠢。我喜欢第二个,因为它非常有知识性。我应该选择哪一个让代码更容易理解和维护?

解决方案

在我工作过的项目中,他们对于采取这种问题的最佳方法的看法存在很大差异。两者都有优点和缺点。 GWT现在在一个名为 RequestFactory 的API中支持DTO方法,并且被标榜为标准GWT RPC使用(您的DAO方法)的替代方案。您可以获得与GWT数据绑定框架的性能和集成,以及维护DTO的成本。我认为这是一个很好的折衷,但对于小项目来说,这可能是过度的。

I've started learning GWT about a week ago and here's the question I can't answer for sure.

Here's the server-side:

// business object - has logic
interface Article {
  String getTitle(); // lazy
  void setTitle();
  String getText(); // lazy
  void setText();
  Set<Comment> getComments(); // lazy
}

// also has logic
interface Comment { ... }

I need to somehow create a GWT widget to visualize Article. Passing Article won't work, since it's not serializable and moreover, it has some BL. So, there are 2 ways:

The first one is to use DTOs like:

class ArticleDTO implements Serializable {
  public int articleId;
  public String title;
  public String text;
}

class CommentDTO implements Serializable {
  public int commentId;
  public int articleId;
  public String commentText;
}

The I'll have to implement a repository logic in my GWT RPC service:

class MyRPCRepository ... {
  ArticleDTO getArticle(int id);
  void saveArticle(ArticleDTO article);
  void deleteArticle(ArticleDTO article);
  ...similar things for comments here...
}

The second way is to use DAOs:

class ArticleDAO implements Serializable {
  private transitional MyRPC rpc;
  private int articleId; // only this one is serializable

  public ArticleDAO(MyRPC rpc, int articleId) { ... }

  public String getTitle() {
    // i know it would require more code in real
    return rpc.getArticleTitle(articleId);
  }
  ...
}

I like the first one, because it's really stupid. I like the second one because it's quite intellectual. Which one should I choose to make the code easier to understand and maintain?

解决方案

In projects I've worked on, people seem to differ a lot in their opinions about the best approach to take with problems like this. There are pros and cons for both. The DTO approach is now supported by GWT in an API called RequestFactory, and is advertised as an alternative to "standard" GWT RPC usage (your DAO approach). You gain performance and integration with GWT's data binding framework, and the cost of maintaining the DTOs. I think it's a good trade-off, but for small projects it might be overkill.

这篇关于GWT RPC:DTO与DAO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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