這是一個可以幫助你快速開發Repository的lib。有點像Springdata JPA根據方法名、註解來自動生成查詢方法的功能。 對於一些簡單的查詢,只需要定義介面就行了,實現都不用。 ...
AgileRepository
這是一個可以幫助你快速開發Repository的lib。有點像SpringData JPA根據方法名、註解來自動生成查詢方法的功能。
對於一些簡單的查詢,只需要定義介面就行了,實現都不用。
Github: AgileRepository
依賴
AspectCore >= 0.2.4
Dapper >= 1.50.4
DapperExtensions >= 1.6.3
System.Threading.Tasks.Extensions >= 4.3.0
使用
public interface IUserRepository:IAgileRepository<Users>
{
[QueryByMethodName]
IEnumerable<Users> QueryByUserName(string userName);
}
var repository = AgileRepository.Proxy.SingletonInstance<IUserRepository>();
repository.QueryByUserName("admin");
配置
AgileRepository.SetConfig(new AgileRepositoryConfig()
{
SqlMonitor = (sql, paramters ) =>
{
Console.WriteLine(sql);
},
ConnectionName = "conn"
});
示例
根據sql查詢
[QueryBySql("SELECT * FROM USERS")]
IEnumerable<User> TestSql();
[QueryBySql("SELECT * FROM USERS where username=@userName")]
IEnumerable<User> TestSql1(string userName);
根據方法名稱查詢
[QueryByMethodName]
IEnumerable<User> QueryByUserName(string userName);
[QueryByMethodName]
IEnumerable<User> QueryByUserNameAndId(string userName, string id);
[QueryByMethodName]
IEnumerable<User> QueryByCreaterIsNull();
[QueryByMethodName]
IEnumerable<User> QueryByCreaterIsNotNull();
查詢所有
[QueryAll]
IEnumerable<User> QueryAll();
根據 sql Count
[CountBySql("Select count(*) from users")]
int TestCount();
[CountBySql("Select count(*) from users where userName=@userName")]
int TestCount1(string userName);
根據方法名Count
[CountByMethodName]
int CountByUserName(string userName);
[CountByMethodName]
int CountByIdAndUserName(string id, string userName);
Count所有
[CountAll]
int CountAll();
Insert
[Insert]
int Insert(User user);
[Insert]
int Insert(IEnumerable<User> users);
Update
[Update]
int Update(User user);
[Update]
int Update(IEnumerable<User> users);
[Delete]
int Delete(User user);
[Delete]
int Delete(IEnumerable<User> users);
[DeleteByMethodName]
int DeleteByUserName(string userName);
執行非查詢sql
[ExecuteBySql("Delete from [users] where id =@id ")]
int Execute(string id);
支持的where關鍵字
Key | Name | Where |
---|---|---|
And | QueryByUserNameAndId | where UserName=@UserName And Id=@Id |
Or | QueryByUserNameOrId | where UserName=@UserName Or Id=@Id |
IsNull | QueryByUserNameIsNull | where UserName Is Null |
IsNotNull | QueryByUserNameIsNotNull | where UserName Is Not Null |
GreaterThen | QueryByAgeGreaterThen | where Age>@Age |
GreaterEqual | QueryByAgeGreaterEqual | where Age>=@Age |
LessThen | QueryByAgeLessThen | where Age<@Age |
LessEqual | QueryByAgeLessEqual | where Age<=@Age |
Not | QueryByAgeNot | where Age!=@Age |
In | QueryByUserNameIn | where UserName in @UserName |
NotIn | QueryByUserNameNotIn | where UserName Not in @UserName |
Like | QueryByUserNameLike | where UserName Like @UserName |
NotLike | QueryByUserNameNotLike | where UserName Not Like @UserName |