122 lines
4.6 KiB
C#
122 lines
4.6 KiB
C#
|
using System;
|
|||
|
using System.Data;
|
|||
|
using Traffic.Repository.Implements;
|
|||
|
using Traffic.Repository.Interfaces;
|
|||
|
|
|||
|
namespace Traffic.Repository.Infrastructures
|
|||
|
{
|
|||
|
public class UnitOfWork : IUnitOfWork
|
|||
|
{
|
|||
|
private IDbConnection _connection;
|
|||
|
private IDbTransaction _transaction;
|
|||
|
|
|||
|
private IAccountRepository _accountRepository;
|
|||
|
private ICompanyInformationRepository _companyInformationRepository;
|
|||
|
private IEventTypeRepository _eventTypeRepository;
|
|||
|
private IPoliceStationRepository _policeStationRepository;
|
|||
|
private IAnnounceRepository _announceRepository;
|
|||
|
private IRolePageRepository _roleRepository;
|
|||
|
private INotPunishRepository _notPunishRepository;
|
|||
|
private ISiteInformationRepository _siteInformationRepository;
|
|||
|
private IEventRepository _eventRepository;
|
|||
|
private IMalfunctionRepository _malfunctionRepository;
|
|||
|
private ITycgRepository _tycgRepository;
|
|||
|
private IRepairRepository _repairRepository;
|
|||
|
private bool _disposed;
|
|||
|
|
|||
|
public UnitOfWork(ConnectionFactory connectionFactory)
|
|||
|
{
|
|||
|
var factory = connectionFactory;
|
|||
|
|
|||
|
_connection = factory.CreateDbConn();
|
|||
|
_connection.Open();
|
|||
|
_transaction = _connection.BeginTransaction();
|
|||
|
}
|
|||
|
|
|||
|
public IAccountRepository AccountRepository => _accountRepository ??= new AccountRepository(_transaction);
|
|||
|
public ICompanyInformationRepository CompanyInformationRepository => _companyInformationRepository ??= new CompanyInformationRepository(_transaction);
|
|||
|
public IEventTypeRepository EventTypeRepository => _eventTypeRepository ??= new EventTypeRepository(_transaction);
|
|||
|
public IPoliceStationRepository PoliceStationRepository => _policeStationRepository ??= new PoliceStationRepository(_transaction);
|
|||
|
public IAnnounceRepository AnnounceRepository => _announceRepository ??= new AnnounceRepository(_transaction);
|
|||
|
public IRolePageRepository RolePageRepository => _roleRepository ??= new RolePageRepository(_transaction);
|
|||
|
public INotPunishRepository NotPunishRepository => _notPunishRepository ??= new NotPunishRepository(_transaction);
|
|||
|
public ISiteInformationRepository SiteInformationRepository => _siteInformationRepository ??= new SiteInformationRepository(_transaction);
|
|||
|
public IEventRepository EventRepository => _eventRepository ??= new EventRepository(_transaction);
|
|||
|
public IMalfunctionRepository MalfunctionRepository => _malfunctionRepository ??= new MalfunctionRepository(_transaction);
|
|||
|
public ITycgRepository TycgRepository => _tycgRepository ??= new TycgRepository(_transaction);
|
|||
|
public IRepairRepository RepairRepository => _repairRepository ??= new RepairRepository(_transaction);
|
|||
|
|
|||
|
public void Rollback()
|
|||
|
{
|
|||
|
_transaction.Rollback();
|
|||
|
}
|
|||
|
|
|||
|
public void Complete()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_transaction.Commit();
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
_transaction.Rollback();
|
|||
|
throw;
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
_transaction.Dispose();
|
|||
|
_transaction = _connection.BeginTransaction();
|
|||
|
ResetRepositories();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void ResetRepositories()
|
|||
|
{
|
|||
|
_accountRepository = null;
|
|||
|
_companyInformationRepository = null;
|
|||
|
_eventTypeRepository = null;
|
|||
|
_policeStationRepository = null;
|
|||
|
_announceRepository = null;
|
|||
|
_roleRepository = null;
|
|||
|
_notPunishRepository = null;
|
|||
|
_siteInformationRepository = null;
|
|||
|
_eventRepository = null;
|
|||
|
_malfunctionRepository = null;
|
|||
|
_tycgRepository = null;
|
|||
|
_repairRepository = null;
|
|||
|
}
|
|||
|
|
|||
|
public void Dispose()
|
|||
|
{
|
|||
|
Dispose(true);
|
|||
|
GC.SuppressFinalize(this);
|
|||
|
}
|
|||
|
|
|||
|
private void Dispose(bool disposing)
|
|||
|
{
|
|||
|
if (!_disposed)
|
|||
|
{
|
|||
|
if (disposing)
|
|||
|
{
|
|||
|
if (_transaction != null)
|
|||
|
{
|
|||
|
_transaction.Dispose();
|
|||
|
_transaction = null;
|
|||
|
}
|
|||
|
if (_connection != null)
|
|||
|
{
|
|||
|
_connection.Dispose();
|
|||
|
_connection = null;
|
|||
|
}
|
|||
|
}
|
|||
|
_disposed = true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
~UnitOfWork()
|
|||
|
{
|
|||
|
Dispose(false);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|