トランザクションにある次のコードがあります。自分の仕事の単位をどこで/いつコミットすべきかわかりません。
わざと、私が使用しているRespoistoryのタイプについては触れていません。Linq-To-Sql、Entity Framework 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();
}