java - 存储库搜索中带有对象与参数列表的方法。违反建议零售价?

标签 java c# design-patterns repository-pattern design-principles

我与同行就将存储库方法的搜索选项封装在对象或(可能不断增长的)参数列表中是否是良好实践进行了争论。

总而言之,伪代码是:

SearchItems(int id,string name,int statusId)

对比

class SearchOptions
{
   /*
   For illustration purposes, I intentionally made the, otherwise private fields public.
   Under normal circumstances, they would be private with their respective accessors or mutators or properties for C#
   */
   public int id;
   public string name;
   public int statusId;
}


SearchItems(SearchOptions filter)
{
   //Rest of code here
}

参数列表的参数是:

  1. 搜索的唯一责任是仅获取 此时所需的结果。如果搜索要求应该 改变,需要编写具有特定功能的新方法。
  2. 即使对象参数说它破坏性较小,搜索选项的任何更改仍然需要修改
  3. 跨平台集成更加友好(例如,由于方法签名简单,Java服务后端到WCF服务后端)

让对象保存过滤器选项的论点是:(我的论点)

  1. 方法保持完整,并且可以灵活更改,不会造成太大破坏,并且需要更少的工作
  2. 方法签名中没有难看的长参数列表
  3. 界面保持完整,增加了更多的权重,减少了重大变化

什么会被视为良好实践?这些论点是否成立(双方)或者这是主观的?

最佳答案

Argument for parameter list are:

That the search has the single responsibility of getting only the results required at that point. If search requirements should change,a new method with the specific function should be written.

如果 Paramter 对象专用于搜索方法,它也可以具有单一职责。例如

class SearchByIdNameAndStatus { // Maybe you find a better name depending on your use case

   public int id;
   public string name;
   public int statusId;
}

Even if object argument says it is less breaking, any change in search options will still require modification.

确实如此。此外...如果您在多个服务方法中使用一个参数对象,它们将相互依赖。例如。如果更改参数对象,也可能会影响使用它的其他服务。

It is friendlier to integrating across platforms (e.g. Java service backend to WCF service backend due to simplicity of method signature)

也许是这样。你真的有这个要求吗?您还可以在服务层之上添加一个简单的传输层以适应不同的传输。

Argument for having an object to hold filter options are: (my argument)

Method remains intact and flexible to change without breaking much and requiring less work.

我猜你的意思是你不会得到编译器错误,因为签名没有改变。但客户端仍然必须设置"new"属性,并且服务必须解释它们。所以工作量并没有减少。

No unsightly, long parameter list in method signature

这是一个很好的论点。此外,您可以简单地定义哪些参数是必需的,哪些参数是可选的。将强制参数设置为搜索参数对象的构造函数参数,可选参数将只是 setter 。参数的验证也可以在参数对象中完成。

Interfaces are left intact, adding more weight to less breaking changes

我想我之前已经回答过这个问题了。

至少你可以看一下我写的博客,也许它可以帮助你在讨论中找到一种方法。参见

  1. https://www.link-intersystems.com/blog/2012/02/26/separation-of-api-and-implementation/
  2. https://www.link-intersystems.com/blog/2012/02/05/simplify-service-layer-design-using-bean-validation-jsr-303/

关于java - 存储库搜索中带有对象与参数列表的方法。违反建议零售价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37202303/

相关文章:

c# - 如何正确初始化 Windows 窗体应用程序

design-patterns - 为什么单例会违反开/关原则?

design-patterns - 为什么使用单例模式?

java - 如何通过apache poi向docx文档添加背景图片?

java - 是否可以有条件地在 Java 中导入库?

java - JmDNS: 无法解析服务

java - 如何通过 JDBC 访问 Windows 版 Blackfish?

c# - ASP.NET MVC : How can I get my business rule validation to bubble up to the presentation layer?

c# - Linq 表达式获取插入的 Convert 函数

c# - 依赖属性是否实现享元模式?