问题描述
所以,一直的(我完全同意),是一个设计的气味,一般都自带形式:
接口IDoSomething
{
无效初始化();
无效DoSomethingThatOnlyWorksAfterInitialising();
}
在它的形式上面,有可能是一些你可以做些什么(用构造器注入的或者通过一个抽象工厂等)的手段。
然而,如何这是否适用于的组的工作的?
我目前在我UOW的面色有点像
<$ p欢乐的烂摊子$ p>
接口IUnitOfWork
{
无效初始化(); //各种输出连接共享/开/亚达
无效回滚();
无效提交();
}
不可否认,初始化应该还是不应该存在。我设法通过调用我的情况下方法争取
否则说服自己。忽略这虽然,是 还原
和 提交
下的时空耦合的?
进一步的想法
$考虑b$ b
已经思考过这一点了,是不是要么用词的选择(时空耦合)是错误的,或者至少是我的措辞解释?
在我的脑海里,什么香味正试图做的就是程序员不依赖某种形式的初始化
的方法,也可以反直观的指出了上面的链接上的 EndpointAddressBuilder
框架例子。
不过,方法,有效地终止之类的进一步使用是可以接受的?像的Dispose方法
显然有某种形式的的时间耦合的;尝试使用类调用后的Dispose
将会给你带来的问题,你应该知道更好。同样与提交
,还原
,我怀疑其他各种例子,在其他情况下。
难道气味只涉及到的初始化连接的(或更好的选择的话,该博客的人能想出?)。
初始化
应该返回,可回滚或COMMITED的对象:
接口IUnitOfWorkFactory
{
IUnitOfWork创建();
}
接口IUnitOfWork:IDisposable的
{
无效提交();
无效回滚(); //应处置被调用,如果提交从未被调用。
}
此外,最好你的 IUnitOfWork
应该延伸的IDisposable
和的Dispose
应该是一样的还原
。
So, as has been pointed out to me (and I entirely agree), temporal coupling is a design smell, and generally comes in the form:
interface IDoSomething
{
void Initialise();
void DoSomethingThatOnlyWorksAfterInitialising();
}
In it's form above, there probably is something you can do about it (by means of constructor injection or maybe through an abstract factory, etc).
However, how does this apply to a Unit Of Work?
I'm currently in the joyous mess of my UoW's looking a little like
interface IUnitOfWork
{
void Initialise(); // sorts out connection sharing/opening/yada
void Rollback();
void Commit();
}
Admittedly, initialise still shouldn't be there. I managed to convince myself otherwise by calling the method Enlist
in my case. Ignoring this though, are Rollback
and Commit
considered under temporal coupling?
Further thoughts
Having pondered over this a little, isn't it either that the choice of wording ("temporal coupling") is wrong, or at least my interpretation of the wording?
In my mind, what the smell is trying to do is get coders to not rely upon some form of Initialise
method, that can be counter-intuitive as is pointed out with the EndpointAddressBuilder
framework example on the link above.
However, methods that effectively terminate further usage of the class are acceptable? Methods like Dispose
obviously have some form of temporal coupling; attempting to use the class after calling Dispose
is going to cause you problems and you should know better. Similarly with Commit
, Rollback
, and I suspect various other examples, in other cases.
Is it that the smell only relates to initialisation coupling (or some better choice of words that one of the bloggers can come up with?).
Initialize
should return an object which can be rolled back or commited:
interface IUnitOfWorkFactory
{
IUnitOfWork Create();
}
interface IUnitOfWork : IDisposable
{
void Commit();
void Rollback(); // should get called by Dispose if Commit was never called.
}
Also, ideally your IUnitOfWork
should extend IDisposable
, and Dispose
should be the same as Rollback
.
这篇关于时间耦合VS工作单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!