1. 程式人生 > >工作單元模式(UnitOfWork)學習總結

工作單元模式(UnitOfWork)學習總結

複製程式碼
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using Jack.gao.UnitOfWork.Domain;
  7 using Jack.Gao.UnitOfWork.Infrastructure;
  8 using System.Data.SqlClient;
  9 
 10 namespace Jack.gao.UnitOfWork.Persistence
 11 {
 12     public class AccountRepository:IAccountRepository,IUnitOfWorkRepository
 13     {
 14         #region Field
 15 
 16         private const string _connectionString = @"Data Source=T57649\MSSQLSERVER2012;Initial Catalog=DB_Customer;Integrated Security=True";
 17 
 18         private IUnitOfWork _unitOfWork;
 19 
 20         #endregion
 21 
 22         #region Constructor
 23 
 24         public AccountRepository(IUnitOfWork unitOfWork)
 25         {
 26             this._unitOfWork = unitOfWork;
 27         }
 28 
 29         #endregion
 30 
 31         #region Implement interface IAccountRepository,IUnitOfWorkRepository
 32 
 33         public void Save(BankAccount account)
 34         {
 35             _unitOfWork.RegisterChangeded(account,this);
 36         }
 37 
 38         public void Add(BankAccount account)
 39         {
 40             _unitOfWork.RegisterAdded(account,this);
 41         }
 42 
 43         public void Remove(BankAccount account)
 44         {
 45             _unitOfWork.RegisterRemoved(account,this);
 46         }
 47 
 48         public void PersistNewItem(EntityBase entityBase)
 49         {
 50             BankAccount account = (BankAccount)entityBase;
 51 
 52             string insertAccountSql = string.Format("insert into DT_Account(balance,Id) values({0},{1})", account.Balance, account.Id);
 53 
 54             SqlConnection sqlConnection = new SqlConnection(_connectionString);
 55 
 56             try
 57             {
 58                 sqlConnection.Open();
 59 
 60                 SqlCommand sqlCommand = new SqlCommand(insertAccountSql, sqlConnection);
 61 
 62                 sqlCommand.ExecuteNonQuery();
 63             }
 64             catch (Exception ex)
 65             {
 66                 throw ex;
 67             }
 68             finally
 69             {
 70                 sqlConnection.Close();
 71             }
 72         }
 73 
 74         public void PersistUpdatedItem(EntityBase entityBase)
 75         {
 76             BankAccount account = (BankAccount)entityBase;
 77 
 78             string updateAccountSql = string.Format("update DT_Account set balance={0} where Id={1}", account.Balance,account.Id);
 79 
 80             SqlConnection sqlConnection = new SqlConnection(_connectionString);
 81 
 82             try
 83             {
 84                 sqlConnection.Open();
 85 
 86                 SqlCommand sqlCommand = new SqlCommand(updateAccountSql, sqlConnection);
 87 
 88                 sqlCommand.ExecuteNonQuery();
 89             }
 90             catch (Exception ex)
 91             {
 92                 throw ex;
 93             }
 94             finally
 95             {
 96                 sqlConnection.Close();
 97             }
 98         }
 99 
100         public void PersistDeletedItem(EntityBase entityBase)
101         {
102             BankAccount account = (BankAccount)entityBase;
103 
104             string deleteAccountSql = string.Format("delete from DT_Account where Id={0}", account.Id);
105 
106             SqlConnection sqlConnection = new SqlConnection(_connectionString);
107 
108             try
109             {
110                 sqlConnection.Open();
111 
112                 SqlCommand sqlCommand = new SqlCommand(deleteAccountSql, sqlConnection);
113 
114                 sqlCommand.ExecuteNonQuery();
115             }
116             catch (Exception ex)
117             {
118                 throw ex;
119             }
120             finally
121             {
122                 sqlConnection.Close();
123             }
124         }
125 
126         #endregion
127 
128         #region Method
129 
130         public BankAccount GetAccount(BankAccount account)
131         {
132             account.Balance = 100;
133             return account;
134         }
135 
136         #endregion
137     }
138 }
複製程式碼