問題描述
我經常在 C++ 中使用的東西是讓一個類 A
通過 A
處理另一個類 B
的狀態進入和退出條件> 構造函數和析構函數,以確保如果該范圍內的某些內容引發異常,則 B 在退出范圍時將具有已知狀態.就首字母縮略詞而言,這并不是純粹的 RAII,但它仍然是一種既定模式.
Something I often used back in C++ was letting a class A
handle a state entry and exit condition for another class B
, via the A
constructor and destructor, to make sure that if something in that scope threw an exception, then B would have a known state when the scope was exited. This isn't pure RAII as far as the acronym goes, but it's an established pattern nevertheless.
在C#中,我經常想做的事
In C#, I often want to do
class FrobbleManager
{
...
private void FiddleTheFrobble()
{
this.Frobble.Unlock();
Foo(); // Can throw
this.Frobble.Fiddle(); // Can throw
Bar(); // Can throw
this.Frobble.Lock();
}
}
需要這樣做
private void FiddleTheFrobble()
{
this.Frobble.Unlock();
try
{
Foo(); // Can throw
this.Frobble.Fiddle(); // Can throw
Bar(); // Can throw
}
finally
{
this.Frobble.Lock();
}
}
如果我想保證 FiddleTheFrobble
返回時的 Frobble
狀態.代碼會更好
if I want to guarantee the Frobble
state when FiddleTheFrobble
returns. The code would be nicer with
private void FiddleTheFrobble()
{
using (var janitor = new FrobbleJanitor(this.Frobble))
{
Foo(); // Can throw
this.Frobble.Fiddle(); // Can throw
Bar(); // Can throw
}
}
FrobbleJanitor
看起來大致像
class FrobbleJanitor : IDisposable
{
private Frobble frobble;
public FrobbleJanitor(Frobble frobble)
{
this.frobble = frobble;
this.frobble.Unlock();
}
public void Dispose()
{
this.frobble.Lock();
}
}
這就是我想要做的.現在現實趕上了,因為我 想要 使用 需要 FrobbleJanitor
使用 with using代碼>.我可以認為這是一個代碼審查問題,但有些事情困擾著我.
And that's how I want to do it. Now reality catches up, since what I want to use requires that the FrobbleJanitor
is used with using
. I could consider this a code review issue, but something is nagging me.
問題:以上是否會被視為濫用using
和IDisposable
?
Question: Would the above be considered as abusive use of using
and IDisposable
?
推薦答案
我不這么認為,一定.IDisposable 在技術上 is 意味著用于具有非托管資源的事物,但是 using 指令只是實現 try .. finally { dispose }代碼>.
I don't think so, necessarily. IDisposable technically is meant to be used for things that have non-managed resources, but then the using directive is just a neat way of implementing a common pattern of try .. finally { dispose }
.
純粹主義者會爭辯說是的 - 這是濫用",而在純粹主義者的意義上它是;但我們大多數人不是從純粹主義的角度編碼,而是從半藝術的角度編碼.在我看來,以這種方式使用使用"結構確實很有藝術性.
A purist would argue 'yes - it's abusive', and in the purist sense it is; but most of us do not code from a purist perspective, but from a semi-artistic one. Using the 'using' construct in this way is quite artistic indeed, in my opinion.
您可能應該在 IDisposable 之上粘貼另一個接口以將其推得更遠,向其他開發人員解釋為什么該接口意味著 IDisposable.
You should probably stick another interface on top of IDisposable to push it a bit further away, explaining to other developers why that interface implies IDisposable.
還有很多其他的替代方法可以做到這一點,但最終,我想不出有什么比這更整潔,所以去吧!
There are lots of other alternatives to doing this but, ultimately, I can't think of any that will be as neat as this, so go for it!
這篇關于使用 IDisposable 和“使用"是否濫用?作為獲得“范圍行為"的一種手段為了異常安全?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!