Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/UnitOfWork.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 1.58 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/UnitOfWork.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/UnitOfWork.cs
-
curl -L -o UnitOfWork.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/UnitOfWork.cs
1.58 kB
| using System.Data; | |
| using SCM.Application.Common; | |
| namespace SCM.Infrastructure.Persistence; | |
| /// <summary> | |
| /// UoW com escopo de requisição. Abre a conexão de forma preguiçosa e expõe a | |
| /// transação corrente para os repositórios. | |
| /// </summary> | |
| public sealed class UnitOfWork : IUnitOfWork | |
| { | |
| private readonly Lazy<IDbConnection> _connection; | |
| private IDbTransaction? _transaction; | |
| private bool _disposed; | |
| public UnitOfWork(IDbConnectionFactory factory) | |
| { | |
| _connection = new Lazy<IDbConnection>(() => | |
| { | |
| var connection = factory.CreateConnection(); | |
| connection.Open(); | |
| return connection; | |
| }); | |
| } | |
| public IDbConnection Connection => _connection.Value; | |
| public IDbTransaction? CurrentTransaction => _transaction; | |
| public void BeginTransaction() | |
| => _transaction ??= Connection.BeginTransaction(); | |
| public void Commit() | |
| { | |
| try | |
| { | |
| _transaction?.Commit(); | |
| } | |
| finally | |
| { | |
| DisposeTransaction(); | |
| } | |
| } | |
| public void Rollback() | |
| { | |
| try | |
| { | |
| _transaction?.Rollback(); | |
| } | |
| finally | |
| { | |
| DisposeTransaction(); | |
| } | |
| } | |
| private void DisposeTransaction() | |
| { | |
| _transaction?.Dispose(); | |
| _transaction = null; | |
| } | |
| public void Dispose() | |
| { | |
| if (_disposed) return; | |
| DisposeTransaction(); | |
| if (_connection.IsValueCreated) | |
| _connection.Value.Dispose(); | |
| _disposed = true; | |
| } | |
| } | |