Modules vs Layers in Java package structure

拈花ヽ惹草 提交于 2019-12-05 23:37:17

问题


I used to put everything in packages like this:

com.company.app.module1
com.company.app.module2

But it has made package-based AOP pointcuts difficult, and resulted in huge packages that need an IDE to make sense of.

So now I realize I need a deeper package structure, but I am constantly torn. Give modules preference, like this?

com.company.app.module1.domain
com.company.app.module1.logic
com.company.app.module1.persistence
com.company.app.module2.domain
com.company.app.module2.logic
com.company.app.module2.persistence

or give layers preference, like this?

com.company.app.domain.module1
com.company.app.domain.module2
com.company.app.logic.module1
com.company.app.logic.module2
com.company.app.persistence.module1
com.company.app.persistence.module2

Pros and cons of each?


回答1:


Modules-first.

I had a project that was initially layer-first, but it became too bulky to read and maintain, so we refactored it. It used AOP as well - without any problems. We just used .. in the middle of the package definition (we used spring aop with aspectj syntax). Here's how it looks like:

execution(* com.foo.app.modules..service..*.*(..))

This matches both modules.module1.service and modules.module2.service




回答2:


Organizing by module allows developers to focus on feature sets as the unit of delivery rather than technical infrastructure. Scaling your code base is probably easier if you break things up based on modules - some open source projects whose code I have looked at (such as Artifactory and Continuum) organized things this way, but I haven't looked at enough to know if this is the general trend.

It probably does depend though, depending on your code base size.




回答3:


I'll admit, I've never really done a lot of (formal) AOP.

Personally, I would put modules first.

That way, if you later split the modules into several JAR/WAR files (into separate maven project, for example), they are already in the correct directory structure to split them by module.




回答4:


I would organize the package hierarchy by layer in order to allow your tools to work. Each module would go into it's own project with it's own source folder. This gives your IDE and developers easy module-orient grouping but your runtime tools easy layer oriented grouping




回答5:


I rather put module first as well, but it seems to me that doing that you have to reference everything everywhere. It might be the reason for such confusion among developers.



来源:https://stackoverflow.com/questions/4821130/modules-vs-layers-in-java-package-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!