一、功能变化
(一)、强化特性支持
1、部分类型拥有复杂属性;
2、有些属性不是来源于数据库
3、用户在原来的代码中使用 SqlRepoEx ,减少字段与数据库字段之间的冲突;
4、为支持新的特性及优化属性读写,增加了 SimpleWritablePropertyMatcher;
5、增加SqlRepoDbFieldAttribute特性后,如果用户程序仍然为POJO类型,不必标识SqlRepoDbFieldAttribute时,用SimpleWritablePropertyMatcher
6、如果明确要区分,就用WritablePropertyMatcher ;
string ConnectionString = "Data Source=(Local);Initial Catalog=Northwind;User ID=test;Password=test";
var connectionProvider = new ConnectionStringConnectionProvider(ConnectionString);
MsSqlRepoFactory.UseConnectionProvider(connectionProvider);
// 使用新的属性读写器
MsSqlRepoFactory.UseWritablePropertyMatcher(new SimpleWritablePropertyMatcher());
var repository = MsSqlRepoFactory.Create<Customers>();
类型代码(使用明确特定 使用 WritablePropertyMatcher )
1 public class ToDo 2 { 3 [NonDatabaseField] 4 public string Remark { get; set; } 5 6 [SqlRepoDbField] 7 public DateTime CreatedDate { get; set; } 8 9 [SqlRepoDbField] 10 public bool IsCompleted { get; set; } 11 12 [SqlRepoDbField] 13 public string Task { get; set; } 14 15 [IdentityField] 16 public int Id { get; set; } 17 }
类型代码(不指定 使用 SimpleWritablePropertyMatcher)
1 public class ToDo 2 { 3 [NonDatabaseField] 4 public string Remark { get; set; } 5 public DateTime CreatedDate { get; set; } 6 public bool IsCompleted { get; set; } 7 public string Task { get; set; } 8 9 [IdentityField] 10 public int Id { get; set; } 11 }
(二)事务支持
1、在初始仓储时如果使用
MsSqlRepoFactory.UseStatementTransactionExecutor();
2、代码中使用使用方法 repository.GetConnectionProvider.BeginTransaction() 获取事务控制
1 public void DoIt2() 2 { 3 var repository = MySqlRepoFactory.Create<ToDo>(); 4 using (var tranc = repository.GetConnectionProvider.BeginTransaction()) 5 { 6 repository.Update().Set(c => c.Task, "A31").Where(c => c.Id == 1).Go();// A1 7 repository.Update().Set(c => c.Task, "B31").Where(c => c.Id == 2).Go();// B2 8 9 tranc.Rollback(); 10 } 11 }
(三)、增加生成 @ 参数 的语句支持
返回形如下列SQL语句:
var repository = MsSqlRepoFactory.Create<ToDo>();
var results = repository.Query().Where(c => c.Id == 6).Go().FirstOrDefault();
Console.WriteLine(resultinsert.ParamSql());
1、 INSERT ToDo ( CreatedDate , IsCompleted , Task )
VALUES(@CreatedDate,@IsCompleted,@Task);
var resultinsert = repository.Update().For(results);
Console.WriteLine(resultinsert.ParamSql());
2、 UPDATE ToDo
SET CreatedDate = @CreatedDate, IsCompleted = @IsCompleted, Task = @Task
WHERE Id = @Id;
以解决储如Dapper等ORM工具需要参数类型字符串需求
SqlRepoEx中是可以与 Dapper 同时并存,意味着初始化SqlRepoEx后,
1、可以直接从 SqlRepoEx 中操作返回结果;
2、可以通过 SqlRepoEx 来生成SQL语句,另外Dapper 主要是基于SqlMapper ,SqlMapper中定义了基于 IDbConnection 接口的操作,你可以通过SqlRepoEx 的 IConnectionProvider 接口来获取一个 DbConnection 有两种方法
(1)、 var connectionProvider = new AppConfigFirstConnectionProvider();
IDbConnection dbConnection = connectionProvider.GetDbConnection;
(2)、 var repository = MsSqlRepoFactory.Create<ToDo>();
IDbConnection dbConnection = repository.GetConnectionProvider.GetDbConnection;
1 public static void TestMySqlUpdate() 2 { 3 string ConnectionString = "datasource=127.0.0.1;username=test;password=test;database=sqlrepotest;charset=gb2312;SslMode = none;"; 4 var connectionProvider = new MySQLP.ConnectionStringConnectionProvider(ConnectionString); 5 MySqlRepoFactory.UseConnectionProvider(connectionProvider); 6 MySqlRepoFactory.UseStatementExecutor(new DapperStatementExecutor(connectionProvider)); 7 MySqlRepoFactory.UseDataReaderEntityMapper(new DapperEntityMapper()); 8 MySqlRepoFactory.UseWritablePropertyMatcher(new SimpleWritablePropertyMatcher()); 9 var repository11 = MySqlRepoFactory.Create<ToDo>(); 10 var results11 = repository11.Query().Where(c => c.Id == 2).Go().FirstOrDefault(); 11 12 results11.Task = "B21"; 13 14 var rest2= repository11.Update().For(results11); 15 16 Console.WriteLine(rest2.ParamSql()); 17 18 var rest3 = rest2.ParamSqlWithEntity(); 19 20 IDbConnection dbConnection = repository11.GetConnectionProvider.GetDbConnection; 21 22 dbConnection.Execute(rest3.paramsql, rest3.entity); 23 }
二、注意事项
1、如果返回的SQL中没有字段名返回,如 insert Doto() values() ,这种情况,当前使用了WritablePropertyMatcher 属性读写器,而DTO类又未设置特性
你可以
(1)、增加相应字段特性
1 [SqlRepoDbField] 2 public string Task { get; set; } 3 4 [IdentityField] 5 public int Id { get; set; }
(2)、使用 SimpleWritablePropertyMatcher属性读写器
MsSqlRepoFactory.UseWritablePropertyMatcher(new SimpleWritablePropertyMatcher());
2、事务支持,事务支持需使用MsSqlRepoFactory.UseStatementTransactionExecutor();,如果是自定义的读写器,一定不在内部关闭 connection;