简体   繁体   English

Comparator 在域驱动的 MVC 世界中的位置在哪里?

[英]Where does a Comparator live in a domain-driven MVC world?

I have a class that needs to be sortable in a couple of different ways, many of which break the equals() contract, so I need to have a bunch of different Comparator classes.我有一个 class 需要以几种不同的方式进行排序,其中许多方式破坏了equals()合同,所以我需要有一堆不同的Comparator类。 The question I have is where should those classes live?我的问题是这些课程应该住在哪里?

So that there is something concrete to use as an example namespace structure and to limit the question to package structure rather than file structure, lets assume the following namespaces:因此,有一些具体的东西可以用作示例命名空间结构并将问题限制为 package 结构而不是文件结构,让我们假设以下命名空间:

app
  domain
    exception
    hibernatemapping
  mvc
    propertyeditor
    tags
  persistence
    hibernate

Domain classes live in the domain namespace, and related exceptions and hibernate mapping files in exception and hibernatemapping respectively.域类存在于domain命名空间中,相关的异常和 hibernate 映射文件分别在exceptionhibernatemapping中。 persistence holds DAO interfaces, with hibernate-based implementations in hibernate . persistence包含 DAO 接口,在hibernate中具有基于休眠的实现。 All of the MVC controllers live in mvc , with specialized property editors (this is Spring MVC) in propertyeditor and classes that back custom tags in tags .所有的 MVC 控制器都存在于mvc中,在propertyeditor中有专门的属性编辑器(这是 Spring MVC)和在tags中支持自定义标签的类。

My gut says that Comparators should live under the domain namespace, perhaps in domain.comparator , but I'm not sure.我的直觉说Comparators应该存在于domain命名空间下,也许在domain.comparator中,但我不确定。

Where would you put them and why?你会把它们放在哪里,为什么?


Update: A number of people have suggested using a general Util package.更新:许多人建议使用通用的Util package。 When going that route, would you separate out the Util classes by UI helpers vs. domain helpers?走这条路时,您会通过 UI 助手和域助手将Util类分开吗? For example, if the domain needed to sort things for business logic reasons, but the UI needed additional sortings that the domain doesn't care about?例如,如果域出于业务逻辑原因需要对事物进行排序,但 UI 需要域不关心的额外排序? Basically, would you tend to have a helper package per-layer?基本上,你会倾向于每层都有一个助手 package 吗?

As suggested, something like a util or a commons package would seem to make the most sense, though be sure you don't end up using that as a "goto" package if you don't know where to put something.正如所建议的那样,像 util 或 commons package 这样的东西似乎是最有意义的,但如果你不知道把东西放在哪里,请确保你最终不会将它用作“goto” package。

I'd also suggest looking into package by feature .我还建议通过 feature 研究 package In this cause, your compartor would just go with whatever corresponding feature its used with.在这种情况下,您的比较器将只是 go 与它使用的任何相应功能。 Again, this is only a suggestion - you'll have to make the call for the best layout of your project.同样,这只是一个建议——您必须要求项目的最佳布局。

I usually go with the following structure:我通常 go 具有以下结构:

company
  appname
     model
     util
     dao
     service
     controller

In this type of structure a comparator would probably get dumped into the util package (if there were really a lot of comparators).在这种类型的结构中,比较器可能会被转储到实用程序 package 中(如果真的有很多比较器的话)。

However, usually I find that comparators are used in exactly one place, and so I declare them inline where they're actually used, eg但是,通常我发现比较器只在一个地方使用,所以我在实际使用它们的地方声明它们内联,例如

public class SomeService {
    public void someMethod(String id) {
        List<ListType> list = dao.getSomeListById(id);
        Collections.sort(list, new Comparator<ListType>() {
            public compare(ListType a, ListType b) {
            // ...
            }
        });
    }
}

I suggest to create a package called "app.util" or "app.helper".我建议创建一个名为“app.util”或“app.helper”的 package。

Conceptionally speaking, in Domain Driven Design this belongs in the infrastructure layer ( http://www.infoq.com/articles/ddd-in-practice ).从概念上讲,在领域驱动设计中,这属于基础设施层( http://www.infoq.com/articles/ddd-in-practice )。

I made a package called comparators which is where I store mine.我制作了一个名为comparators的 package,这是我存储我的地方。

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

相关问题 域驱动设计:DTO在哪里? - Domain-Driven Design: where are the DTO? 将MVC应用于领域驱动的设计 - Applying mvc to domain-driven design 域驱动的设计-每个实体都应该有向导吗? - domain-driven design - should each entity have guid? 考虑到业务需求的变化,在这种情况下,POJO /模型或域驱动对象的实质是什么? - What really is the essence of POJOs/models or domain-driven objects in the situation in context considering business requirements changes? OrmLite 不会在带有“不等于”比较器和空列值的“where”上带来结果 - OrmLite does not bring results on "where" with "not equals" comparator and null column values Spring MVC Samples中的“Hello World”来自哪里? - Where is the “Hello World” coming from in Spring MVC Samples 是hibernate应用程序域驱动? - are hibernate applications domain driven? 域驱动设计中的实体 - Entities in domain driven design Spring MVC为什么这个Hello World在没有注释驱动标签的情况下运行良好(不像其他任何项目Spring) - Spring MVC why this Hello World run well without annotation-driven tag (unlike any other project Spring) 设计方法(域驱动或服务驱动) - Design approach (Domain driven or Service Driven)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM