I currently have made a UnitOfWork implementation which wraps both the database connection and the transaction.
using (var uow = UnitOfWorkFactory.Create())
{
// do db operations here through repositories
uow.SaveChanges();
}
Rollback will be called 开发者_StackOverflow中文版if SaveChanges
haven't been called before the uow gets disposed.
Is it a bad design choice to let the uow handle both the connection and the transaction?
Let's say that I got a ASP.Net MVC website where most of the actions are just fetching information from the database. Are there a performance penalty for creating/committing transactions that aren't actually not doing anything in the database?
If you want to implement UoW then SaveChanges
should be the only place where a connection and a transaction will be used. UoW will just collect all commands that must be executed and execute them in a transaction when SaveChanges
is invoked.
UoW is domain specific. I had more-or-less the same implementation you use until someone pointed this out since database access should not really influence your domain.
So I ended up splitting the responsibilities. Since my UoW does make use of repositories that require a connection to a database I still require a connection.
So for instances where I do not require a UoW I will have this:
using (DatabaseConnectionFactory.Create()) { ... }
For transaction:
using (var connection = DatabaseConnectionFactory.Create().BeginTransaction())
{
// do stuff
connection.CommitTransaction();
}
If I require a UoW (typically with transaction):
using (var connection = DatabaseConnectionFactory.Create().BeginTransaction())
using (var uow = UnitOfWorkFactory.Create())
{
// do stuff
connection.CommitTransaction();
}
HTH
Did you implement the Unit of Work yourself (I'm assuming you did). Look into using ADO.NET EF with the Repository pattern.
There is a big camp that feels any database operation should be wrapped in a transaction. You should for sure wrap any Inserts / Updates / Deletes in a transaction.
That being said, wrap any of the database objects that implement IDisposable in a Using Block.
精彩评论