构造函数中的业务逻辑是一个好主意吗? [英] Is business logic in constructors a good idea?

查看:93
本文介绍了构造函数中的业务逻辑是一个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在工作中重建一个专用的票务系统(主要用于为遥感硬件有故障的人提供支持...).无论如何,我想知道在对象的构造函数中进行大量工作流类型活动是否是一个好主意.

I'm currently rebuilding a specialised ticket system at work (mainly used to support people with faults in remote sensing hardware...). Anyway, I was wondering whether doing lots of workflow type activity in an object's constructor is a good idea.

例如,当前存在以下内容:

For example, there is currently this:

$ticket = new SupportTicket(
    $customer,
    $title,
    $start_ticket_now,
    $mail_customer
);

对象创建后,它将放入数据库中的行,然后向客户发送确认电子邮件,还可能向附近的技术人员发送文本消息,等等.

as soon as the object is created, it'll put a row into a database, go and mail the customer a confirmation e-mail, possibly send a text message to the nearest technician, etc..

构造函数是否应该解雇所有工作,或更类似于以下内容?

Should a constructor be firing off all that work, or something more like the following?

$ticket = new SupportTicket($customer, $title);
$customer->confirmTicketMailed($ticket);
$helpdesk->alertNewTicket($ticket);

如果有帮助,则对象全部基于ActiveRecord样式.

If it helps, the objects are all based on the ActiveRecord style.

我想这可能是个见解,但是您认为最好的做法是什么?

I guess it may be a matter of opinion, but what do you think is the best thing to do?

推荐答案

如果构造函数完成了所有工作,则构造函数会知道许多其他域对象.这产生了依赖性问题. ticket是否应该真正了解CustomerHelpDesk?添加新功能后,是否有可能将新的域对象添加到工作流中,这是否不意味着我们可怜的ticket将不得不知道域对象的数量在不断增加?

If the constructor does all that work then the constructor knows about many other domain objects. This creates a dependency problem. Should the ticket really know about the Customer and the HelpDesk? When new features are added, isn't it likely that new domain objects will be added to the workflow, and doesn't that mean that our poor ticket will have to know about an ever increasing population of domain objects?

像这样的依赖蜘蛛网的问题是,对任何一个域对象进行源代码更改都会对我们可怜的ticket产生影响. ticket将对系统有非常多的了解,因此无论发生什么情况,都会涉及到ticket.您会发现讨厌的if语句聚集在该构造函数内部,检查配置数据库,会话状态以及许多其他内容. ticket将成长为神类.

The problem with spiderwebs of dependency like this is that a source code change to any one of the domain object will have an impact upon our poor ticket. The ticket will have so much knowledge of the system that no matter what happens, the ticket will be involved. You will find nasty if statements gathering inside that constructor, checking the configuration database, and the session state, and so many other things. The ticket will grow to become a god class.

我不喜欢构造函数做事的另一个原因是,它使周围的对象很难测试.我喜欢写很多模拟对象.当我针对customer编写测试时,我想通过一个模拟的ticket.如果ticket的构造函数控制工作流程以及customer和其他域对象之间的舞动,那么我不太可能将其模拟出来以测试customer.

Another reason I dislike constructors that do things is that it makes the objects around them very hard to test. I like to write lots of mock objects. When I write a test against the customer I want to pass it a mocked out ticket. If the constructor of ticket controls the workflow, and the dance between customer and other domain objects, then it is unlikely that I will be able to mock it out to test the customer.

我建议您阅读《 SOLID原则》 ,这是我几年前写的关于管理依赖项的论文在面向对象的设计中.

I suggest you read The SOLID Principles, a paper I wrote several years back about managing dependencies in object oriented designs.

这篇关于构造函数中的业务逻辑是一个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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