<分区>
背景:
类似于this question ,我希望将 IDisposable 用于其设计用途之外的用途。
目标:
这个应用程序不是很相关,但只是一个很好的例子:我有一个 Windows 窗体应用程序,我在其中实现了撤消设计模式。通常,这是通过拦截来自 UI 元素的“Value Changed”类事件来完成的。对于 DataGridView
、CellEndEdit
等等。然而,在某些情况下,我以编程方式更改数据,并且我想做每个撤消操作跟踪的事情,而不是跟踪它们。
到目前为止:
我有办法做到这一切,但我管理我的“我应该撤消”逻辑吗:
private int _undoOverrides = 0;
public bool ShouldUndo { get { return _undoOverrides < 1; } }
public void DoNotUndo() { ++_undoOverrides; }
public void ResumeUndo() { --_undoOverrides; }
现在,这很有效,但您必须记住在以 DoNotUndo()
开头的此类业务逻辑末尾调用 ResumeUndo()
。我想:
Maybe I'm not too much of an idiot to screw that up, but what if I had to expose this interface in code I pass down? I would like if the compiler could take care of this for me, if possible.
想法:
我正在考虑为此使用实现 IDisposable
的类。这样,我的代码的用户就可以使用 using
block ,而不必担心家务活。这是我目前所拥有的:
private static int _refCount = 0;
public static int ReferenceCount { get { return _refCount; } }
class HallPass : IDisposable
{
protected bool bActive;
public HallPass()
{
++Program._refCount;
bActive = true;
Console.WriteLine("Acquired hallpass!");
}
public void Dispose()
{
if (bActive)
--Program._refCount;
bActive = false;
Console.WriteLine("Hallpass expired!");
}
}
我包含了一个 bool 值,这样我确定我不会在 Dispose()
上重复倒计时。因此,要使用 HallPass
,您只需:
using (new HallPass())
{
// do things...
}
问题是:
这是个好主意吗?为什么这可能是个坏主意?我应该知道的任何陷阱?
此外,我对此感到很愚蠢,但我很确定引用计数 不是正确的术语。它就像一个引用计数,但没有管理的引用或释放的内存。 编辑: 可能是,只是现在不行。
它就像一个互斥体或一个关键部分,因为你试图在一个部分中对规则进行异常(另一个用词不当,因为我不是说你throw
的那种)代码,但它不是其中任何一个,因为它们在一个范围内是相互排斥的——如果您愿意,这被设计为以嵌套方式完成。这就是为什么它是一个计数,而不是一个 bool 值。