Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using SlateCore.DAL.Interfaces;
- using System.Data;
- using System.Data.SqlClient;
- namespace SlateCore.DAL.Repositories
- {
- public class UnitOfWork : IUnitOfWork
- {
- private IDbConnection _connection;
- private IDbTransaction _transaction;
- private bool _disposed;
- public UnitOfWork(string connectionString)
- {
- _connection = new SqlConnection(connectionString);
- _connection.Open();
- _transaction = _connection.BeginTransaction();
- }
- private IUserRepository _userRepository;
- public IUserRepository UserRepository
- {
- get
- {
- if (_userRepository == null)
- {
- _userRepository = new UserRepository(_transaction);
- }
- return _userRepository;
- }
- }
- private ISessionRepository _sessionRepository;
- public ISessionRepository SessionRepository
- {
- get
- {
- if (_sessionRepository == null)
- {
- _sessionRepository = new SessionRepository(_transaction);
- }
- return _sessionRepository;
- }
- }
- private IBookRepository _bookRepository;
- public IBookRepository BookRepository
- {
- get
- {
- if (_bookRepository == null)
- {
- _bookRepository = new BookRepository(_transaction);
- }
- return _bookRepository;
- }
- }
- private ILibraryRepository _libraryRepository;
- public ILibraryRepository LibraryRepository
- {
- get
- {
- if (_libraryRepository == null)
- {
- _libraryRepository = new LibraryRepository(_transaction);
- }
- return _libraryRepository;
- }
- }
- private IHistoryRepository _historyRepository;
- public IHistoryRepository HistoryRepository
- {
- get
- {
- if (_historyRepository == null)
- {
- _historyRepository = new HistoryRepository(_transaction);
- }
- return _historyRepository;
- }
- }
- private IBookmarkRepository _bookmarkRepository;
- public IBookmarkRepository BookmarkRepository
- {
- get
- {
- if(_bookmarkRepository == null)
- {
- _bookmarkRepository = new BookmarkRepository(_transaction);
- }
- return _bookmarkRepository;
- }
- }
- private IRecommendationRepository _recommendationRepository;
- public IRecommendationRepository RecommendationRepository
- {
- get
- {
- if(_recommendationRepository == null)
- {
- _recommendationRepository = new RecommendationRepository(_transaction);
- }
- return _recommendationRepository;
- }
- }
- private ITestRepository _testRepository;
- public ITestRepository TestRepository
- {
- get
- {
- if(_testRepository == null)
- {
- _testRepository = new TestRepository(_transaction);
- }
- return _testRepository;
- }
- }
- private ISectionRepository _sectionRepository;
- public ISectionRepository SectionRepository
- {
- get
- {
- if(_sectionRepository == null)
- {
- _sectionRepository = new SectionRepository(_transaction);
- }
- return _sectionRepository;
- }
- }
- private IGenreRepository _genreRepository;
- public IGenreRepository GenreRepository
- {
- get
- {
- if(_genreRepository == null)
- {
- _genreRepository = new GenreRepository(_transaction);
- }
- return _genreRepository;
- }
- }
- private IUserGenreRepository _userGenreRepository;
- public IUserGenreRepository UserGenreRepository
- {
- get
- {
- if(_userGenreRepository == null)
- {
- _userGenreRepository = new UserGenreRepository(_transaction);
- }
- return _userGenreRepository;
- }
- }
- private IPurchaseRepository _purchaseRepository;
- public IPurchaseRepository PurchaseRepository
- {
- get
- {
- if (_purchaseRepository == null)
- {
- _purchaseRepository = new PurchaseRepository(_transaction);
- }
- return _purchaseRepository;
- }
- }
- private IChapterRepository _chapterRepository;
- public IChapterRepository ChapterRepository
- {
- get
- {
- if (_chapterRepository == null)
- {
- _chapterRepository = new ChapterRepository(_transaction);
- }
- return _chapterRepository;
- }
- }
- private IUserAdminRepository _userAdminRepository;
- public IUserAdminRepository UserAdminRepository
- {
- get
- {
- if (_userAdminRepository == null)
- {
- _userAdminRepository = new UserAdminRepository(_transaction);
- }
- return _userAdminRepository;
- }
- }
- private IPartnerRepository _partnerRepository;
- public IPartnerRepository PartnerRepository
- {
- get
- {
- if (_partnerRepository == null)
- {
- _partnerRepository = new PartnerRepository(_transaction);
- }
- return _partnerRepository;
- }
- }
- /// <summary>
- /// The method makes commit, if problems didn't arise and
- /// makes rollback of transaction, if problems arised
- /// </summary>
- public void Commit()
- {
- try
- {
- _transaction.Commit();
- }
- catch
- {
- _transaction.Rollback();
- throw; //TO DO
- }
- finally
- {
- _transaction.Dispose();
- _transaction = _connection.BeginTransaction();
- resetRepositories();
- }
- }
- /// <summary>
- /// The method resets of repositories, which usings in UoW
- /// </summary>
- private void resetRepositories()
- {
- _userRepository = null;
- _sessionRepository = null;
- _bookRepository = null;
- _libraryRepository = null;
- _historyRepository = null;
- _bookmarkRepository = null;
- _recommendationRepository = null;
- _testRepository = null;
- _sectionRepository = null;
- _genreRepository = null;
- _userGenreRepository = null;
- _purchaseRepository = null;
- _chapterRepository = null;
- _userAdminRepository = null;
- _partnerRepository = null;
- }
- #region IDisposable Support
- private void Dispose(bool disposing)
- {
- if (_disposed)
- {
- return;
- }
- if (disposing)
- {
- if (_transaction != null)
- {
- _transaction.Dispose();
- _transaction = null;
- }
- if (_connection != null)
- {
- _connection.Dispose();
- _connection = null;
- }
- }
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- ~UnitOfWork()
- {
- Dispose(false);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement