UnitOfWorkContext : IUnitOfWorkContext,IDisposable 2 { 数据库连接字符串标识 Key { get; } SqlConnection connection; SqlConnection Connection 11 { { 14 if (connection == null) 15 { 16 ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[Key]; 17 connection = new SqlConnection(settings.ConnectionString); 18 } 19 return connection; 20 } 21 } 注册新对象到事务 ExecuteNonQuery(string commandText, IDictionary<string, object> parameters = null) 29 { 30 Func<SqlCommand, int> excute = (commend) => 31 { 32 return commend.ExecuteNonQuery(); 33 }; 34 return CreateDbCommondAndExcute<int>(commandText, parameters, excute); 35 } 查询对象集合 List<T> ReadValues<T>(string commandText, IDictionary<string, object> parameters = null, Func<IDataReader, T> load = null) where T : class,new() 47 { 48 Func<SqlCommand, List<T>> excute = (dbCommand) => 49 { 50 List<T> result = new List<T>(); 51 using (IDataReader reader = dbCommand.ExecuteReader()) 52 { 53 while (reader.Read()) 54 { 55 if (load == null) 56 { 57 load = (s) => { return s.GetReaderData<T>(); }; 58 } 59 var item = load(reader); 60 result.Add(item); 61 } 62 return result; 63 } 64 }; 65 return CreateDbCommondAndExcute(commandText, parameters, excute); 66 } 查询对象集合 List<object> ReadValues(string commandText, Type type, IDictionary<string, object> parameters = null, Action<dynamic> setItem = null) 76 { 77 Func<SqlCommand, List<object>> excute = (dbCommand) => 78 { 79 var result = new List<object>(); (IDataReader dataReader = dbCommand.ExecuteReader()) 82 { 83 while (dataReader.Read()) 84 { 85 var item = dataReader.GetReaderData(type); 86 if (setItem != null) 87 { 88 setItem(item); 89 } 90 result.Add(item); 91 } 92 } 93 return result; 94 }; 95 return CreateDbCommondAndExcute<List<object>>(commandText, parameters, 96 excute); 97 } 查询对象 T ExecuteReader<T>(string commandText, IDictionary<string, object> parameters = null, Func<IDataReader, T> load = null) where T : class,new() 108 { 109 Func<SqlCommand, T> excute = (dbCommand) => 110 { 111 var result = default(T); 112 using (IDataReader reader = dbCommand.ExecuteReader()) 113 { 114 while (reader.Read()) 115 { 116 if (load == null) 117 { 118 load = (s) => { return s.GetReaderData<T>(); }; 119 } 120 result = load(reader); 121 } 122 return result; 123 } 124 }; 125 return CreateDbCommondAndExcute<T>(commandText, parameters, excute); 126 } 查询数量 ExecuteScalar(string commandText, IDictionary<string, object> parameters = null) 135 { 136 Func<SqlCommand, object> excute = (dbCommand) => 137 { 138 return dbCommand.ExecuteScalar(); 139 }; 140 return CreateDbCommondAndExcute(commandText, parameters, excute); 141 } 创建命令并执行 TValue CreateDbCommondAndExcute<TValue>(string commandText, 152 IDictionary<string, object> parameters, Func<SqlCommand, TValue> excute) 153 { 154 if (Connection.State == ConnectionState.Closed) { Connection.Open(); }; 155 using (SqlCommand command = new SqlCommand()) 156 { 157 command.CommandType = CommandType.Text; 158 command.CommandText = commandText;; 159 command.Connection = Connection; 160 command.SetParameters(parameters); 161 return excute(command); 162 } 163 } 关闭连接 Dispose() 169 { 170 if (connection != null) 171 { } 174 } 175 }
View Code在调用方法时需要注意,一个事务涉及多个聚合时,需要保证传递同一工作单元,并在方法的最后调用Commit() 方法。