使用 UnitOfWork 模式提交更改的最佳策略是什么?
What is the best strategy to commit the changes using the UnitOfWork pattern?
使用 UnitOfWork 模式提交更改的最佳策略是什么?在 try-catch 中进行?如果我需要回滚,在这种情况下 catch 是最好的地方吗?
public void Commit() {
_context.SaveChanges();
}
public void Rollback() {
_context
.ChangeTracker
.Entries()
.Where(entry => entry.State != EntityState.Unchanged)
.ToList()
.ForEach(entry => {
switch (entry.State)
{
// Under the covers, changing the state of an entity from
// Modified to Unchanged first sets the values of all
// properties to the original values that were read from
// the database when it was queried, and then marks the
// entity as Unchanged. This will also reject changes to
// FK relationships since the original value of the FK
// will be restored.
case EntityState.Modified:
entry.State = EntityState.Unchanged;
break;
case EntityState.Added:
entry.State = EntityState.Detached;
break;
// If the EntityState is the Deleted, reload the date from the database.
case EntityState.Deleted:
entry.Reload();
break;
default: break;
}
});
}
try {
UnitOfWorkRPOMain.Commit();
} catch (Exception ex) {
UnitOfWorkRPOMain.Rollback();
Logger.Error(baseLog + "Error - ex.Message).");
}
try/catch 没问题。但是,我建议不要将 DbContext 包装在工作单元中。 Entity Framework 已经实现了工作单元和存储库模式。具体来说,DbContext 是工作单元,每个 DbSet 是一个存储库。
考虑到这一点,您的代码可能如下所示:
样本
using (var context = new YourContext())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
// your code
context.SaveChanges();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
// some logging
}
}
}
有关 EF 交易的更多信息:
https://www.entityframeworktutorial.net/entityframework6/transaction-in-entity-framework.aspx
使用 UnitOfWork 模式提交更改的最佳策略是什么?在 try-catch 中进行?如果我需要回滚,在这种情况下 catch 是最好的地方吗?
public void Commit() {
_context.SaveChanges();
}
public void Rollback() {
_context
.ChangeTracker
.Entries()
.Where(entry => entry.State != EntityState.Unchanged)
.ToList()
.ForEach(entry => {
switch (entry.State)
{
// Under the covers, changing the state of an entity from
// Modified to Unchanged first sets the values of all
// properties to the original values that were read from
// the database when it was queried, and then marks the
// entity as Unchanged. This will also reject changes to
// FK relationships since the original value of the FK
// will be restored.
case EntityState.Modified:
entry.State = EntityState.Unchanged;
break;
case EntityState.Added:
entry.State = EntityState.Detached;
break;
// If the EntityState is the Deleted, reload the date from the database.
case EntityState.Deleted:
entry.Reload();
break;
default: break;
}
});
}
try {
UnitOfWorkRPOMain.Commit();
} catch (Exception ex) {
UnitOfWorkRPOMain.Rollback();
Logger.Error(baseLog + "Error - ex.Message).");
}
try/catch 没问题。但是,我建议不要将 DbContext 包装在工作单元中。 Entity Framework 已经实现了工作单元和存储库模式。具体来说,DbContext 是工作单元,每个 DbSet 是一个存储库。
考虑到这一点,您的代码可能如下所示:
样本
using (var context = new YourContext())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
// your code
context.SaveChanges();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
// some logging
}
}
}
有关 EF 交易的更多信息:
https://www.entityframeworktutorial.net/entityframework6/transaction-in-entity-framework.aspx