我应该在下面的代码中的哪一行提交我的工作单元?

Pur*_*ome 9 .net c# transactions unit-of-work

我有以下代码在事务中.我不确定我应该在何时/何时提交我的工作单位.

故意,我没有提到我正在使用什么类型的Respoistory - 例如.Linq-To-Sql,实体框架4,NHibernate等

如果有人知道在哪里,他们可以解释为什么他们说,在哪里?(我试图通过示例来理解模式,而不是让我的代码工作).

这是我得到的: -

using
(
    TransactionScope transactionScope =
        new TransactionScope
        (
            TransactionScopeOption.RequiresNew,
            new TransactionOptions
                { IsolationLevel = IsolationLevel.ReadUncommitted }
        )
)
{
    _logEntryRepository.InsertOrUpdate(logEntry);
    //_unitOfWork.Commit();  // Here, commit #1 ?

    // Now, if this log entry was a NewConnection or an LostConnection,
    // then we need to make sure we update the ConnectedClients.
    if (logEntry.EventType == EventType.NewConnection)
    {
        _connectedClientRepository.Insert(
            new ConnectedClient { LogEntryId = logEntry.LogEntryId });
        //_unitOfWork.Commit(); // Here, commit #2 ?
    }

    // A (PB) BanKick does _NOT_ register a lost connection,
    // so we need to make sure we handle those scenario's as a LostConnection.
    if (logEntry.EventType == EventType.LostConnection ||
        logEntry.EventType == EventType.BanKick)
    {
        _connectedClientRepository.Delete(
            logEntry.ClientName, logEntry.ClientIpAndPort);
        //_unitOfWork.Commit(); // Here, commit #3 ?
    }

    _unitOfWork.Commit(); // Here, commit #4 ?
    transactionScope.Complete();
}
Run Code Online (Sandbox Code Playgroud)

Eve*_*ien 0

对所有存储库进行所有操作后,在 #4 处提交。如果您提前提交,则不会提交该调用之后所做的更改。