如何(或者应该)避免 jsf 中的长方法/类
我主要在基于 JSF 的项目中处理遗留代码,并且支持 bean 中有很多相当长的类和方法。
这一直困扰着我,但是当我寻找可以做什么时,大多数时候我能想到的就是将一个长方法分成 n 个小方法。这会给你带来很长的课程,有时也更难阅读。
那么,如何才能让你的支持 bean 变得简短明了呢?或者你会在一页上保留一个大的支撑豆吗?有没有最佳实践?
我认为这与 jsf 没有直接关系,而是与您使用控制器“备份”视图的任何模型相关。因此,一般建议也会有所帮助。
I'm mostly working with legacy code in a JSF based project and there are lots of quite long classes and methods in backing beans.
This is constantly bugging me but when I look for what can be done, most of the time all I can come up is to divide a long method to n small methods. Which gives you still a very long class and sometimes harder to read too.
So what do you do to keep your backing beans short and concise? Or do you keep one big backing bean for one page? Are there any best-practices?
I assume this is not directly related to jsf but to any model where you are 'backing' up your view with a controller. So a general advise would be helpful also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将所有字段放在自己的类中,也称为实体或 DTO 类(例如
User
、Product
、Order
等)并引用它。这些可以是 JDBC/JPA 实体。将所有业务方法放在自己的类中,也称为服务或域对象(例如UserService
、ProductService
等)并引用它。这些可以是 EJB 的。例如,因此不是
但更进一步
,我还看到了代码,其中嵌套对象/实体是通过 bean 中的附加 getter/setter 委托的,就像这样
这是完全没有必要的。实体只需一个 getter 就足够了(setter 不是强制的!),结合
实体本身还可以进一步划分。例如
的
可以被同一street
、houseNumber
、zipCode
、city
、country
UserUser
内的另一个实体/DTOAddress
替换。如果您运气不好,代码已由可视化编辑器(例如 Netbeans + Woodstock)自动生成。无论如何,如果不完全重新设计它,就没有太多需要重构的地方,我宁愿寻找另一个项目。
Put all fields in its own class, also called an entity or DTO class (e.g.
User
,Product
,Order
, etc) and reference it instead. Those can be JDBC/JPA entities. Put all business methods in its own class, also called a service or domain object (e.g.UserService
,ProductService
, etc) and reference it instead. Those can be EJB's.E.g. thus not
But more so
Further, I've also seen code wherein the nested objects/entities are delegated through by additional getters/setters in the bean like so
This is totally unnecessary. Just a single getter for the entity is sufficient (setter is not mandatory!), in combination with
The entities at its own can also be further divided. E.g.
street
,houseNumber
,zipCode
,city
,country
of anUser
could be replaced by another entity/DTOAddress
within the sameUser
.If you have bad luck, the code has been autogenerated by a visual editor (e.g. Netbeans + Woodstock). There's not much to refactor anyway without completely redesigning it, I would rather look for another project.
恕我直言,每个方法应该是 1 个步骤,如果该 1 个步骤包含多个内部步骤,您应该将它们分解为更小的方法,以便更容易阅读。现代 IDE 的好处是它们可以毫不费力地为您折射代码。
IMHO, each method should be 1 step, if that 1 step conatins multiple inner steps you should break them down into smaller methods so its easier to read. The good thing with modern IDE's is that they refractor code for you without much effort at all.
不仅适用于支持 bean,而且适用于一般的所有对象:
您应该始终通过以下方式重构 长方法 将它们提取成多个较小的。
Not only applicable to backing beans, but to all objects in general:
you should always refactor long methods by extracting them into multiple smaller ones.