Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.50 KB | None | 0 0
  1. using System;
  2. using SlateCore.DAL.Interfaces;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5.  
  6.  
  7. namespace SlateCore.DAL.Repositories
  8. {
  9.     public class UnitOfWork : IUnitOfWork
  10.     {
  11.         private IDbConnection _connection;
  12.         private IDbTransaction _transaction;
  13.         private bool _disposed;
  14.  
  15.  
  16.         public UnitOfWork(string connectionString)
  17.         {
  18.             _connection = new SqlConnection(connectionString);
  19.             _connection.Open();
  20.             _transaction = _connection.BeginTransaction();
  21.         }
  22.  
  23.         private IUserRepository _userRepository;
  24.         public IUserRepository UserRepository
  25.         {
  26.             get
  27.             {
  28.                 if (_userRepository == null)
  29.                 {
  30.                     _userRepository = new UserRepository(_transaction);
  31.                 }
  32.  
  33.                 return _userRepository;
  34.             }
  35.         }
  36.  
  37.         private ISessionRepository _sessionRepository;
  38.         public ISessionRepository SessionRepository
  39.         {
  40.             get
  41.             {
  42.                 if (_sessionRepository == null)
  43.                 {
  44.                     _sessionRepository = new SessionRepository(_transaction);
  45.                 }
  46.  
  47.                 return _sessionRepository;
  48.             }
  49.         }
  50.  
  51.         private IBookRepository _bookRepository;
  52.         public IBookRepository BookRepository
  53.         {
  54.             get
  55.             {
  56.                 if (_bookRepository == null)
  57.                 {
  58.                     _bookRepository = new BookRepository(_transaction);
  59.                 }
  60.  
  61.                 return _bookRepository;
  62.             }
  63.         }
  64.  
  65.         private ILibraryRepository _libraryRepository;
  66.         public ILibraryRepository LibraryRepository
  67.         {
  68.             get
  69.             {
  70.                 if (_libraryRepository == null)
  71.                 {
  72.                     _libraryRepository = new LibraryRepository(_transaction);
  73.                 }
  74.  
  75.                 return _libraryRepository;
  76.             }
  77.         }
  78.  
  79.         private IHistoryRepository _historyRepository;
  80.         public IHistoryRepository HistoryRepository
  81.         {
  82.             get
  83.             {
  84.                 if (_historyRepository == null)
  85.                 {
  86.                     _historyRepository = new HistoryRepository(_transaction);
  87.                 }
  88.  
  89.                 return _historyRepository;
  90.             }
  91.         }
  92.  
  93.         private IBookmarkRepository _bookmarkRepository;
  94.         public IBookmarkRepository BookmarkRepository
  95.         {
  96.             get
  97.             {
  98.                 if(_bookmarkRepository == null)
  99.                 {
  100.                     _bookmarkRepository = new BookmarkRepository(_transaction);
  101.                 }
  102.  
  103.                 return _bookmarkRepository;
  104.             }
  105.         }
  106.  
  107.         private IRecommendationRepository _recommendationRepository;
  108.         public IRecommendationRepository RecommendationRepository
  109.         {
  110.             get
  111.             {
  112.                 if(_recommendationRepository == null)
  113.                 {
  114.                     _recommendationRepository = new RecommendationRepository(_transaction);
  115.                 }
  116.  
  117.                 return _recommendationRepository;
  118.             }
  119.         }
  120.  
  121.         private ITestRepository _testRepository;
  122.         public ITestRepository TestRepository
  123.         {
  124.             get
  125.             {
  126.                 if(_testRepository == null)
  127.                 {
  128.                     _testRepository = new TestRepository(_transaction);
  129.                 }
  130.  
  131.                 return _testRepository;
  132.             }
  133.         }
  134.  
  135.         private ISectionRepository _sectionRepository;
  136.         public ISectionRepository SectionRepository
  137.         {
  138.             get
  139.             {
  140.                 if(_sectionRepository == null)
  141.                 {
  142.                     _sectionRepository = new SectionRepository(_transaction);
  143.                 }
  144.  
  145.                 return _sectionRepository;
  146.             }
  147.         }
  148.  
  149.  
  150.         private IGenreRepository _genreRepository;
  151.         public IGenreRepository GenreRepository
  152.         {
  153.             get
  154.             {
  155.                 if(_genreRepository == null)
  156.                 {
  157.                     _genreRepository = new GenreRepository(_transaction);
  158.                 }
  159.  
  160.                 return _genreRepository;
  161.             }
  162.         }
  163.  
  164.         private IUserGenreRepository _userGenreRepository;
  165.         public IUserGenreRepository UserGenreRepository
  166.         {
  167.             get
  168.             {
  169.                 if(_userGenreRepository == null)
  170.                 {
  171.                     _userGenreRepository = new UserGenreRepository(_transaction);
  172.                 }
  173.  
  174.                 return _userGenreRepository;
  175.             }
  176.         }
  177.  
  178.         private IPurchaseRepository _purchaseRepository;
  179.         public IPurchaseRepository PurchaseRepository
  180.         {
  181.             get
  182.             {
  183.                 if (_purchaseRepository == null)
  184.                 {
  185.                     _purchaseRepository = new PurchaseRepository(_transaction);
  186.                 }
  187.  
  188.                 return _purchaseRepository;
  189.             }
  190.         }
  191.  
  192.         private IChapterRepository _chapterRepository;
  193.         public IChapterRepository ChapterRepository
  194.         {
  195.             get
  196.             {
  197.                 if (_chapterRepository == null)
  198.                 {
  199.                     _chapterRepository = new ChapterRepository(_transaction);
  200.                 }
  201.  
  202.                 return _chapterRepository;
  203.             }
  204.         }
  205.  
  206.  
  207.         private IUserAdminRepository _userAdminRepository;
  208.         public IUserAdminRepository UserAdminRepository
  209.         {
  210.             get
  211.             {
  212.                 if (_userAdminRepository == null)
  213.                 {
  214.                     _userAdminRepository = new UserAdminRepository(_transaction);
  215.                 }
  216.  
  217.                 return _userAdminRepository;
  218.             }
  219.         }
  220.  
  221.         private IPartnerRepository _partnerRepository;
  222.         public IPartnerRepository PartnerRepository
  223.         {
  224.             get
  225.             {
  226.                 if (_partnerRepository == null)
  227.                 {
  228.                     _partnerRepository = new PartnerRepository(_transaction);
  229.                 }
  230.  
  231.                 return _partnerRepository;
  232.             }
  233.         }
  234.  
  235.         /// <summary>
  236.         /// The method makes commit, if problems didn't arise and
  237.         /// makes rollback of transaction, if problems arised
  238.         /// </summary>
  239.         public void Commit()
  240.         {
  241.             try
  242.             {
  243.                 _transaction.Commit();
  244.             }
  245.             catch
  246.             {
  247.                 _transaction.Rollback();
  248.                 throw;                          //TO DO
  249.             }
  250.             finally
  251.             {
  252.                 _transaction.Dispose();
  253.                 _transaction = _connection.BeginTransaction();
  254.                 resetRepositories();
  255.             }
  256.         }
  257.  
  258.  
  259.         /// <summary>
  260.         /// The method resets of repositories, which usings in UoW
  261.         /// </summary>
  262.         private void resetRepositories()
  263.         {
  264.             _userRepository = null;
  265.             _sessionRepository = null;
  266.             _bookRepository = null;
  267.             _libraryRepository = null;
  268.             _historyRepository = null;
  269.             _bookmarkRepository = null;
  270.             _recommendationRepository = null;
  271.             _testRepository = null;
  272.             _sectionRepository = null;
  273.             _genreRepository = null;
  274.             _userGenreRepository = null;
  275.             _purchaseRepository = null;
  276.             _chapterRepository = null;
  277.             _userAdminRepository = null;
  278.             _partnerRepository = null;
  279.         }
  280.  
  281.         #region IDisposable Support
  282.         private void Dispose(bool disposing)
  283.         {
  284.             if (_disposed)
  285.             {
  286.                 return;
  287.             }
  288.             if (disposing)
  289.             {
  290.                 if (_transaction != null)
  291.                 {
  292.                     _transaction.Dispose();
  293.                     _transaction = null;
  294.                 }
  295.                 if (_connection != null)
  296.                 {
  297.                     _connection.Dispose();
  298.                     _connection = null;
  299.                 }
  300.             }
  301.         }
  302.  
  303.  
  304.  
  305.         public void Dispose()
  306.         {
  307.             Dispose(true);
  308.             GC.SuppressFinalize(this);
  309.         }
  310.  
  311.         ~UnitOfWork()
  312.         {
  313.             Dispose(false);
  314.         }
  315.         #endregion
  316.     }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement