Calling Dispose more than once on the UnitOfWork throws an exception.
MS best practice notes for implementing Dispose state:
> To help ensure that resources are always cleaned up appropriately, a Dispose method should be callable multiple times without throwing an exception.
The exception is thrown due to the connection state after the first Dispose. The exception message is:
> System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Example code to reproduce is as follows:
```
using (var ctx = new TestDbContext())
{
var uow = new UnitOfWork(ctx);
var repo = new Repository<TestItem>(ctx, uow);
var svc = new TestItemService(repo);
uow.BeginTransaction();
svc.Insert(new TestItem("test" + Guid.NewGuid().ToString()));
uow.SaveChanges();
uow.Commit();
// Deliberately call uow.Dispose twice
uow.Dispose();
uow.Dispose();
}
```
Comments: Hi Le, I'm not in front of it at the moment, but I was using the codeplex git repo rather than a release, so it was probably [ba90dbae](https://genericunitofworkandrepositories.codeplex.com/SourceControl/changeset/ba90dbae3dc9bdd98cf77b120d025fcf34c16a05). If I recall the error is thrown on the first line of your `Dispose` method above. I assume that this is because `Dispose(true)` calls `_dataContext.Dispose()`, which sets the internal connection of the context to `null`. If so then that means future calls to `_objectContext.Connection` throw an exception. I fixed it temporarily by moving `if (_objectContext ...Close();` to only be called `if (!_disposed && disposing)` but I already think it's insufficient. `ObjectContext` has a finalizer in order to ensure disposal of its transaction handler, so in theory I guess this implies that, despite the performance expense, `UnitOfWork` also needs a finalizer, and should dispose of `_objectContext` in there regardless of `disposing`. Your thoughts? (I have not missed the irony that the best practice to "not throw exceptions on multiple Dispose calls" goes against the best practice ["CA2202: Do not dispose objects multiple times"](http://msdn.microsoft.com/en-us/library/ms182334.aspx), but it might be better to ensure that multiple calls work, and so does the finalizer). Andrew
MS best practice notes for implementing Dispose state:
> To help ensure that resources are always cleaned up appropriately, a Dispose method should be callable multiple times without throwing an exception.
The exception is thrown due to the connection state after the first Dispose. The exception message is:
> System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Example code to reproduce is as follows:
```
using (var ctx = new TestDbContext())
{
var uow = new UnitOfWork(ctx);
var repo = new Repository<TestItem>(ctx, uow);
var svc = new TestItemService(repo);
uow.BeginTransaction();
svc.Insert(new TestItem("test" + Guid.NewGuid().ToString()));
uow.SaveChanges();
uow.Commit();
// Deliberately call uow.Dispose twice
uow.Dispose();
uow.Dispose();
}
```
Comments: Hi Le, I'm not in front of it at the moment, but I was using the codeplex git repo rather than a release, so it was probably [ba90dbae](https://genericunitofworkandrepositories.codeplex.com/SourceControl/changeset/ba90dbae3dc9bdd98cf77b120d025fcf34c16a05). If I recall the error is thrown on the first line of your `Dispose` method above. I assume that this is because `Dispose(true)` calls `_dataContext.Dispose()`, which sets the internal connection of the context to `null`. If so then that means future calls to `_objectContext.Connection` throw an exception. I fixed it temporarily by moving `if (_objectContext ...Close();` to only be called `if (!_disposed && disposing)` but I already think it's insufficient. `ObjectContext` has a finalizer in order to ensure disposal of its transaction handler, so in theory I guess this implies that, despite the performance expense, `UnitOfWork` also needs a finalizer, and should dispose of `_objectContext` in there regardless of `disposing`. Your thoughts? (I have not missed the irony that the best practice to "not throw exceptions on multiple Dispose calls" goes against the best practice ["CA2202: Do not dispose objects multiple times"](http://msdn.microsoft.com/en-us/library/ms182334.aspx), but it might be better to ensure that multiple calls work, and so does the finalizer). Andrew