Java控制器需要什么代码?

发布于 2024-12-26 20:41:04 字数 1001 浏览 0 评论 0原文

我正在开发一个支持 Spring 的 Web 项目,其中我们有多个控制器。最近我与高级开发人员讨论了控制器中应该包含哪些代码。

有的说是为了让控制器轻量化;有的说是为了让控制器轻量化。只有调用方法才应该进入控制器内部。即使只有 3 行处理,也使它们成为单独的方法并从控制器中调用它们,如下所示

public ModelAndView offersMapView(HttpServletRequest request, HttpServletResponse response) {

Map<String, Object> contentMap = getServiceLocator().getHyperLocalService().getOffersHubContents(searchPostcode, gmapKey, ip);

            return getViewGenerator().generateOffersHubMapView(brand, contentMap);
}

其中generateOffersHubMapView(brand, contentMap) 方法仅包含 2 行代码

public ModelAndView generateOffersHubMapView(Brand brand, Map<String, Object> contentMap) {     
        ModelAndView mv = POGAModelandView.createWebModelAndView(brand, WLConstants.__HUB_OFFERS);
        mv.addAllObjects(contentMap);       

        return mv;
    }

我的观点是,创建单独的视图生成器方法的 2 行不好直到我们有一定数量的代码处理行。

相反的观点是创建单独的方法来保持层之间的隔离。

我能否请问社区他们认为更好的方法是什么?如果可能的话,您能否提供参考资料来支持您的陈述?

I am working on a Spring enabled web project where we have multiple controllers. Recently I had a discussion with senior developers what code should go in the controller.

Some of them said to make the controller light-weight; only invoking methods should go inside the controller. Even if there are only 3 lines of processing, make them separate methods and call them from the Controller as follows

public ModelAndView offersMapView(HttpServletRequest request, HttpServletResponse response) {

Map<String, Object> contentMap = getServiceLocator().getHyperLocalService().getOffersHubContents(searchPostcode, gmapKey, ip);

            return getViewGenerator().generateOffersHubMapView(brand, contentMap);
}

Where generateOffersHubMapView(brand, contentMap) method contains only 2 line of code

public ModelAndView generateOffersHubMapView(Brand brand, Map<String, Object> contentMap) {     
        ModelAndView mv = POGAModelandView.createWebModelAndView(brand, WLConstants.__HUB_OFFERS);
        mv.addAllObjects(contentMap);       

        return mv;
    }

My point was that 2 lines creating separate view generator method is not good until we have number of lines of code processing.

The opposing point of view was creating separate method to keep segregation between layers.

Could I please ask the community what they think is the better approach? If possible, could you provide references to support your statements?

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

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

发布评论

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

评论(1

━╋う一瞬間旳綻放 2025-01-02 20:41:04

没有任何规则这么说

 Even if there are only 3 lines of processing, make them separate methods 

,我认为如果没有正当理由,每三行就分割一次只会让事情变得复杂。专注于可重用性和可读性。如果单个方法中有可重用的代码,请将其拆分。如果您的方法太长并且做了很多事情,请将其拆分。阅读 https://stackoverflow.com/questions/20981/ 的讨论代码行数太多https://stackoverflow.com/questions/903754/do-you-still-限制代码中的行长度

我个人认为,generateOffersHubMapView 方法 split 在您的示例中没有多大意义。

There is no rule which says that

 Even if there are only 3 lines of processing, make them separate methods 

and I think splitting every three lines without a valid reason to do so will only complicate things . Concentrate on re-usability and readability . If you have reusable code in a single method, split it up. If you have a method which is too long and does a lot of things , split it up . Read the discussion at https://stackoverflow.com/questions/20981/how-many-lines-of-code-is-too-many and https://stackoverflow.com/questions/903754/do-you-still-limit-line-length-in-code .

Personally I would say that generateOffersHubMapView method split does not make much sense in your sample .

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