src/Library/NetPro.Dapper/UnitOfWork.cs
(开头部分) 3KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using System;
using System.Data;
namespace NetPro.Dapper
{
public class UnitOfWork : IUnitOfWork
{
private IDbTransaction _transaction;
private readonly Action<UnitOfWork> _onCommit;
private readonly Action<UnitOfWork> _onRollback;
/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="transaction">The underlying <see cref="IDbTransaction"/> object used to either commit or roll back the statements that are being performed inside this unit of work.</param>
/// <param name="onCommitOrRollback">An <see cref="Action{NewUnitOfWork}"/> that will be executed when the unit of work is being committed or rolled back.</param>
public UnitOfWork(IDbTransaction transaction, Action<UnitOfWork> onCommitOrRollback) : this(transaction, onCommitOrRollback, onCommitOrRollback)
{
}
/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="transaction">The underlying <see cref="IDbTransaction"/> object used to either commit or roll back the statements that are being performed inside this unit of work.</param>
/// <param name="onCommit">An <see cref="Action{NewUnitOfWork}"/> that will be executed when the unit of work is being committed.</param>
/// <param name="onRollback">An <see cref="Action{NewUnitOfWork}"/> that will be executed when the unit of work is being rolled back.</param>
public UnitOfWork(IDbTransaction transaction, Action<UnitOfWork> onCommit, Action<UnitOfWork> onRollback)
{
_transaction = transaction;
_onCommit = onCommit;
_onRollback = onRollback;
}
/// <summary>
/// Retrieves the underlying <see cref="IDbTransaction"/> instance.
/// </summary>
public IDbTransaction Transaction
{
get { return _transaction; }
}
/// <summary>
/// SaveChanges will try and commit all statements that have been executed against the database inside this unit of work.
/// </summary>
/// <remarks>
/// If committing fails, the underlying <see cref="IDbTransaction"/> will be rolled back instead.
/// </remarks>
/// <exception cref="InvalidOperationException">Thrown if this unit of work has already been committed or rolled back.</exception>
public void SaveChanges()
{
if (_transaction == null)
throw new InvalidOperationException("This unit of work has already been saved or undone.");
try
{
_transaction.Commit();
_onCommit(this);
}
finally
{
后面还有 26 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。