Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. public class AdoNetUnitOfWork : IUnitOfWork
  2. {
  3. public AdoNetUnitOfWork(IDbConnection connection, bool ownsConnection)
  4. {
  5. _connection = connection;
  6. _ownsConnection=ownsConnection;
  7. _transaction = connection.BeginTransaction();
  8. }
  9.  
  10. public IDbCommand CreateCommand()
  11. {
  12. var command = _connection.CreateCommand();
  13. command.Transaction = _transaction;
  14. return command;
  15. }
  16.  
  17. public void SaveChanges()
  18. {
  19. if (_transaction == null)
  20. throw new InvalidOperationException("Transaction have already been commited. Check your transaction handling.");
  21.  
  22. _transaction.Commit();
  23. _transaction = null;
  24. }
  25.  
  26. public void Dispose()
  27. {
  28. if (_transaction != null)
  29. {
  30. _transaction.Rollback();
  31. _transaction = null;
  32. }
  33.  
  34. if (_connection != null && _ownsConnection)
  35. {
  36. _connection.Close();
  37. _connection = null;
  38. }
  39. }
  40. }
  41.  
  42. public DomainTable Get(int id)
  43. {
  44. DomainTable table;
  45.  
  46. using (var commandTable = _unitOfWork.CreateCommand())
  47. {
  48. commandTable.CommandType = CommandType.StoredProcedure;
  49. //This stored procedure contains just a simple SELECT statement
  50. commandTable.CommandText = "up_DomainTable_GetById";
  51.  
  52. commandTable.Parameters.Add(commandTable.CreateParameter("@pId", id));
  53.  
  54. table = ToList(commandTable).FirstOrDefault();
  55. }
  56.  
  57. return table;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement