重构:横切关注点解决方法

发布于 2024-08-14 11:34:17 字数 557 浏览 7 评论 0原文

是否有一种解决方法可以在不涉及方面和切点等的情况下实现横切关注点?

我们使用 Spring MVC,并正在开发一个业务应用程序,由于各种原因,进入 AspectJ 或 Spring 的切面处理是不可行的。

我们的一些控制器已经变得严重臃肿(太严重),大量失焦的代码无处不在。

每次我坐下来重构时,我都会看到同样的事情被一遍又一遍地做。请允许我解释一下:

每次我必须准备一个视图时,我都会向其中添加一个国家/地区列表以用于 UI。 (对象添加到 ModelAndView)。该列表从数据库中提取到 ehCache 中。

现在,最初当我尝试将列表内联添加到 mav 的各处时,情况很糟糕。相反,我准备了一个可以处理每个 ModelAndView 的函数。如何?好吧,对函数有更多的垃圾调用!

我买了一个麻烦又换了另一个麻烦。

有什么设计模式/技巧可以帮助我吗?我厌倦了调用函数将东西添加到我的 ModelAndView 中,并且只有超过 3500 行的控制器代码,我要疯狂地寻找所有丢失东西的粘合点!

欢迎提出建议。横切关注没有 AspectJ 或 Spring 原生的风味。

Is there a workaround for implementing cross cutting concerns without going into aspects and point cuts et al.?

We're in Spring MVC, and working on a business app where it's not feasible to go into AspectJ or Spring's aspect handling due to various reasons.

And some of our controllers have become heavily bloated (too heavily), with tons of out-of-focus code creeping in everywhere.

Everytime I sit down to refactor, I see the same things being done over and over again. Allow me to explain:

Everytime I have to prepare a view, I add a list of countries to it for the UI. (Object added to the ModelAndView). That list is pulled out of a DB into ehCache.

Now, initially it was terrible when I was trying to add the lists INLINE to the mav's everywhere. Instead, I prepared a function which would process every ModelAndView. How? well, with more garbage calls to the function!

And I bought out one trouble for another.

What's a design pattern/trick which can help me out a bit? I'm sick of calling functions to add things to my ModelAndView, and with over 3500 lines of only controller code, I'm going mad finding all the glue points where things have gone missing!

Suggestion are welcome. Cross cutting concerns flavor without AspectJ or Spring native.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无远思近则忧 2024-08-21 11:34:17

由于您使用的是 Java,您可以考虑将代码移至 Scala,因为它与 Java 交互良好,然后您可以使用特征来获得您想要的功能。

不幸的是,横切是 OOP 的一个问题,所以改用函数式编程可能是一个解决方案,但是,我希望实际上他们使用 AOP 来实现这些 mixin,所以它仍然是 AOP,只是抽象出来的。

另一种选择是考虑重新设计您的应用程序,并确保您没有重复的代码,但主要的重构非常困难且充满风险。

但是,例如,您最终可能会使用 ModelAndView 调用多个静态实用程序类来获取所需的数据,或者确保用户具有正确的角色。

您可能想看一本书,《重构模式》(http://www.industriallogic.com/ xp/refactoring/)一些想法。

Since you are using Java, you may consider moving your code to Scala, since it interacts well with Java, then you can use traits to get the functionality you want.

Unfortunately cross-cutting is a problem with OOP, so changing to functional programming may be a solution, but, I expect that in actuality they are using AOP to implement these mixins, so it would still be AOP, just abstracted out.

The other option is to look at redesigning your application, and make certain that you don't have duplicate code, but a major refactoring is very difficult and fraught with risk.

But, for example, you may end up with your ModelAndView calling several static utility classes to get the data it needs, or do ensure that the user has the correct role, for example.

You may want to look at a book, Refactoring to Patterns (http://www.industriallogic.com/xp/refactoring/) for some ideas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文