I have a lock class, that handels a database lock. It's not important how.
It is used in the beginning of a large operation, here is how it is used today:
public void LargeOperation()
{
try
{
MyLock.DoLock("SomeId");
// Do lots of stuff that might thorw exceptions
}
finally
{
MyLock.ReleaseLock("SomeId");
}
}
I would like to use it like this instead:
public void LargeOperation()
{
using(new MyLock("someId"))
{
// Do lots of stuff that might thorw exceptions
}
}
this works fine, but im not sure wether it is a bad idea. I have implemented IDisposable on my class MyLock
but it doesn't dispose.. instead it releases the lock.
Here is how the class look:
public class MyLock : IDisposable
{
private string _id;
public MyLock(string id)
{
DoLock(id);
}
public static void DoLock(string id)
{
//..code omitted
}
public static void ReleaseLock(string id)
{
//..code omitted
}
public void Dispose()
{
ReleaseLock(_id);
}
}
Is this abuse of the IDisposable interface?