帕森斯为什么叫高富帅,神鬼八卦图,林宝麟
最近有个想法,想不写 sql 语句,做一个类似于 orm 的东西,自己解析表达式树,生成要执行的 sql 语句,最后再执行 sql 语句,返回相应结果。
常用的 sql 语句基本都有一定的模式,就是 insert
/delete
/update
/select
,我把公用的部分给抽离出来,把查询/更新/删除条件和指定字段通过表达式树来指出,解析传入的表达式树对象来获取条件以及要更新的字段。
源码链接
请看使用示例:
安装 nuget 包 wihanli.common
public static class repositorytest { public static void maintest() { var connectionpool = new dbconnectionpool(new dbconnectionpoolpolicy(configurationhelper.connectionstring("testdb"))); var repo = new repository<testentity>(() => connectionpool.get()); repo.insert(new testentity { token = "1233", createdtime = datetime.utcnow }); var entity = repo.fetch(t => t.pkid == 1); system.console.writeline(entity.token); repo.update(t => t.pkid == 1, t => t.token, 1); entity = repo.fetch(t => t.pkid == 1); system.console.writeline(entity.token); repo.delete(t => t.pkid == 1); entity = repo.fetch(t => t.pkid == 1); system.console.writeline($"delete operation {(entity == null ? "success" : "failed")}"); repo.execute("truncate table dbo.tabtestentity"); console.writeline("finished."); } public class dbconnectionpool : defaultobjectpool<dbconnection> { public dbconnectionpool(ipooledobjectpolicy<dbconnection> policy) : base(policy) { } public dbconnectionpool(ipooledobjectpolicy<dbconnection> policy, int maximumretained) : base(policy, maximumretained) { } } public class dbconnectionpoolpolicy : ipooledobjectpolicy<dbconnection> { private readonly string _connstring; public dbconnectionpoolpolicy(string connstring) { _connstring = connstring; } public dbconnection create() { return new sqlconnection(_connstring); } public bool return(dbconnection obj) { return obj.connectionstring.isnotnullorwhitespace(); } } [table("tabtestentity")] internal class testentity { [databasegenerated(databasegeneratedoption.identity)] public int pkid { get; set; } public string token { get; set; } public datetime createdtime { get; set; } } }
示例使用了 objectpool 实现了一个数据库连接池,repository
实例化需要一个获取 dbconnection 对象的委托,而数据库连接就是从这数据库连接池中获取。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论