SmartSql 動態代理倉儲,一個高生產力的組件。該組件看似很難懂,實際上僅做了映射Statement,轉發請求的功能。但卻意義重大。 SmartSql提供了一個通用泛型倉儲介面 SmartSql.DyRepository.IRepository 定義倉儲介面 註入依賴 註入依賴後將動態實現倉儲接 ...
SmartSql 動態代理倉儲,一個高生產力的組件。該組件看似很難懂,實際上僅做了映射Statement,轉發請求的功能。但卻意義重大。
SmartSql提供了一個通用泛型倉儲介面
SmartSql.DyRepository.IRepository<TEntity, TPrimary>
public interface IRepository<TEntity, TPrimary> : IRepository
{
int Insert(TEntity entity);
int Delete(object reqParams);
[Statement(Id = "Delete")]
int DeleteById([Param("Id")]TPrimary id);
int Update(TEntity entity);
[Statement(Id = "Update")]
int DyUpdate(object dyObj);
IEnumerable<TEntity> Query(object reqParams);
IEnumerable<TEntity> QueryByPage(object reqParams);
[Statement(Execute = ExecuteBehavior.ExecuteScalar)]
int GetRecord(object reqParams);
TEntity GetEntity(object reqParams);
[Statement(Id = "GetEntity")]
TEntity GetById([Param("Id")]TPrimary id);
[Statement(Execute = ExecuteBehavior.ExecuteScalar)]
bool IsExist(object reqParams);
}
定義倉儲介面
/// <summary>
/// 屬性可選: [SqlMap(Scope = "User")] ,不設置 則預設 Scope 模板:I{Scope}Repository
/// 可傳入自定義模板
/// RepositoryBuilder builder=new RepositoryBuilder("I{Scope}DAL");
/// </summary>
public interface IUserRepository : IRepository<User, string>
{
/// <summary>
/// 屬性可選 [Statement(Execute = ExecuteBehavior.Auto,Id = "Query")]
/// 預設 Execute:Auto ,自動判斷 執行類型
/// 預設 Id : 方法名
/// </summary>
/// <param name="reqParams"></param>
/// <returns></returns>
[Statement(Sql = "Select Top(@taken) T.* From User T With(NoLock);")]
IEnumerable<User> QueryBySql(int taken);
}
註入依賴
註入依賴後將動態實現倉儲介面,並映射好相應SqlMap
services.AddSmartSql();
services.AddRepositoryFactory();
services.AddRepositoryFromAssembly((options) =>
{
options.AssemblyString = "SmartSql.Starter.Repository";
});
Attribute
倉儲介面,函數特性(StatementAttribute)
public class StatementAttribute : Attribute
{
/// <summary>
/// 定義 SmartSqlMap.Scope 該屬性可選,預設使用倉儲介面的Scope
/// </summary>
public string Scope { get; set; }
/// <summary>
/// 可選,預設使用函數名作為 Statement.Id
/// </summary>
public string Id { get; set; }
/// <summary>
/// 可選, 預設 Execute:Auto ,自動判斷 執行類型
/// </summary>
public ExecuteBehavior Execute { get; set; } = ExecuteBehavior.Auto;
/// <summary>
/// 可選,當不使用 SmartSqlMap.Statement 時可直接定義 Sql
/// </summary>
public string Sql { get; set; }
}
/// <summary>
/// 執行行為
/// </summary>
public enum ExecuteBehavior
{
/// <summary>
/// 自動判斷執行類型
/// </summary>
Auto = 0,
/// <summary>
/// 返回受影響行數
/// </summary>
Execute = 1,
/// <summary>
/// 返回結果的第一行第一列的值,主要用於返回主鍵
/// </summary>
ExecuteScalar = 2,
/// <summary>
/// 查詢枚舉對象,List
/// </summary>
Query = 3,
/// <summary>
/// 查詢單個對象
/// </summary>
QuerySingle = 4,
/// <summary>
/// 返回DataTable
/// </summary>
GetDataTable = 5,
/// <summary>
/// 返回DataSet
/// </summary>
GetDataSet = 6
}
倉儲介面特性(SqlMapAttribute)
public class SqlMapAttribute : Attribute
{
/// <summary>
/// SmartSqlMapConfig.Scope 映射
/// </summary>
public string Scope { get; set; }
}
函數參數特性(ParamAttribute)
public class ParamAttribute : Attribute
{
public ParamAttribute(string name = "")
{
Name = name;
}
/// <summary>
/// DbDataParameter.Name
/// </summary>
public String Name { get; set; }