Spring formBackingObject,业务对象创建和工厂 [英] Spring formBackingObject, Business Object Creation, and Factories

查看:175
本文介绍了Spring formBackingObject,业务对象创建和工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring SimpleFormController中将业务对象用作formBackingObjects的设计问题.

我们的控制者的责任是允许最终用户向我们的Web应用程序中添加新的业务对象.

因此,我们正在通过formBackingObject(HttpServletRequest request)方法传递业务对象.但是,我们遇到了一个难题.

我们用于创建新业务对象的工厂将强制执行某些属性不能为空的业务规则.但是,由于我们不知道最终用户想要输入什么,我们一直在传递合理的默认值",例如请输入您想要的名称",但这似乎充其量不过是骇客.

开发人员要做什么?我觉得这似乎是经典的鸡肉/鸡蛋问题.

我们所有的业务对象都基于接口,我们是否应该创建一个代表业务对象的存根,将存根作为formBackingObject传递,然后在表单提交时将存根传递给工厂?还是我们不应该在formBackingObject中传递任何内容,然后从请求中手动收集提交的信息?

还有其他合理的想法/模式吗?

谢谢您的时间.

解决方案

我绝对不会选择不使用formBackingObject并手动收集信息的选项-这将消除使Spring MVC值得拥有的强大功能首先.

如果我是您,我只会创建一个新的工厂或工厂方法,该工厂或工厂方法专门用于创建未初始化的"业务对象,并将其用作您的formBackingObject.

另一种广泛使用的方法是根本不使用业务对象作为formBackingObject,而是创建一个单独的传输对象,其唯一目的是成为formBackingObject(然后为您的业务对象添加一个工厂方法,使您可以从传输对象对其进行初始化).这样做的一大优点是,如果您的业务对象内部具有其他对象的一棵深树,则使用formBackingObject可能会很麻烦.如果创建单独的传输对象仅用作formBackingObject,则可以为其提供更扁平的结构.

Design question for using Business Objects as formBackingObjects in a Spring SimpleFormController.

Our controller's responsibility is to allow an End User to add a new Business Object to our web application.

So we are passing our Business Object though the formBackingObject(HttpServletRequest request) method. However, we've run into a conundrum.

The factory that we are using to create our new Business Object enforces the business rules that some of the attributes cannot be null. But since we don't know what the End User wants to enter we've been passing in "reasonable defaults" like "Please enter the name you want", but that seems hackie/icky at best.

What is a developer to do? I feel as though this is the classic chicken/egg problem.

All our Business Object are based off of Interfaces, should we create a Stub that represents the Business Object, pass the Stub as the formBackingObject, and then pass the Stub to the Factory on a Form submit? Or should we not pass anything in the formBackingObject and then manually gather the submitted information from the request?

Any other reasonable ideas/patterns?

Thank you for your time.

解决方案

I definitely wouldn't choose the option of not using a formBackingObject and gathering the information manually -- that would eliminate a lot of the power that makes Spring MVC worthwhile in the first place.

If I were you, I would just make a new factory, or factory method, that is designed specifically to create an "uninitialized" business object, and use that as your formBackingObject.

Another approach that is widely used is not to use a business object as your formBackingObject at all, but create a separate transport object whose only purpose is to be the formBackingObject (and then add a factory method for your business object that lets you initialize it from the transport object). One of the big advantages of this is if your business object has a deep tree of other objects inside it, this can make it a pain to use as a formBackingObject. If you create a separate transport object just for use as a formBackingObject, you can give it a much flatter structure.

这篇关于Spring formBackingObject,业务对象创建和工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆