From 5943fc6247e6cd37b66e6cec1326a3f8e205b306 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Fri, 20 Nov 2020 14:59:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E9=80=9A=E8=BF=87FalconSPRet?= =?UTF-8?q?urnTypeAttribute=E7=89=B9=E6=80=A7=E5=9C=A8=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E8=BF=87=E7=A8=8B=E5=8F=82=E6=95=B0=E6=A8=A1=E5=9E=8B=E4=B8=AD?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=BF=94=E5=9B=9E=E7=BB=93=E6=9E=9C=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.ModelSP.Test/ModelSPTest.cs | 14 ++-- Falcon.ModelSP/DataExtend.cs | 82 ++++++++++++++++++- Falcon.ModelSP/Falcon.ModelSP.csproj | 10 +-- .../FalconSPProcuderNameAttribute.cs | 1 + Falcon.ModelSP/FalconSPReturnTypeAttribute.cs | 21 +++++ Falcon.ModelSP/FalconSPRuner.cs | 24 ++++++ Falcon.ModelSP/IFalconSPRuner.cs | 18 ++++ 7 files changed, 156 insertions(+), 14 deletions(-) create mode 100644 Falcon.ModelSP/FalconSPReturnTypeAttribute.cs diff --git a/Falcon.ModelSP.Test/ModelSPTest.cs b/Falcon.ModelSP.Test/ModelSPTest.cs index 72ea8b8..2a15200 100644 --- a/Falcon.ModelSP.Test/ModelSPTest.cs +++ b/Falcon.ModelSP.Test/ModelSPTest.cs @@ -49,13 +49,13 @@ namespace Falcon.ModelSP.Test public void InMemoryDbTest() { //内存数据库无法使用关系模型,测试无法进行 return; - using(var db = TestDbContext.GetDbInMemory()) { - var re = db.Pr_AddOne(new Pr_AddOne { A = 1 }); - Assert.IsTrue(re != null); - Assert.IsTrue(re.Any()); - var first = re.First(); - Assert.IsTrue(first.Aa == 2); - } + //using(var db = TestDbContext.GetDbInMemory()) { + // var re = db.Pr_AddOne(new Pr_AddOne { A = 1 }); + // Assert.IsTrue(re != null); + // Assert.IsTrue(re.Any()); + // var first = re.First(); + // Assert.IsTrue(first.Aa == 2); + //} } } } diff --git a/Falcon.ModelSP/DataExtend.cs b/Falcon.ModelSP/DataExtend.cs index 1d00054..3f4ecd6 100644 --- a/Falcon.ModelSP/DataExtend.cs +++ b/Falcon.ModelSP/DataExtend.cs @@ -6,6 +6,7 @@ using System.Reflection; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using System; +using Microsoft.EntityFrameworkCore.Storage; namespace Falcon.ModelSP { @@ -42,6 +43,61 @@ namespace Falcon.ModelSP return db.Database.SqlQuery(getProcuderName(),parms.ToArray()).ToList(); } + /// + /// 閫氳繃瀛樺偍杩囩▼鍙傛暟妯″瀷鍙婂弬鏁版ā鍨嬪畾涔夌殑杩斿洖绫诲瀷鎵ц鏌ヨ + /// + /// 瀛樺偍杩囩▼鍙傛暟绫诲瀷 + /// 鏁版嵁搴撲笂涓嬫枃 + /// 鍙傛暟鏁版嵁 + /// 瀛樺偍杩囩▼缁撴灉闆 + public static IEnumerable RunSPQuery(this DbContext db,T data) { + var attr = typeof(T).GetCustomAttribute(false); + if(attr == null) { + throw new Exception("鍙傛暟绫诲繀椤诲畾涔塅alconSPReturnTypeAttribute鐗规э紝鍚﹀垯闇瑕佹槑纭寚瀹氳繑鍥炲肩被鍨"); + } + var type = attr.ReturnType; + return RunSPQuery(db,data,typeof(T),type); + } + + /// + /// 閫氳繃鎻愪緵鐨勫弬鏁帮紝鍙傛暟绫诲瀷鍜岃繑鍥炵被鍨嬫墽琛屽瓨鍌ㄨ繃绋 + /// + /// 鏁版嵁搴撲笂涓嬫枃 + /// + /// 鍙傛暟绫诲瀷 + /// 杩斿洖绫诲瀷 + /// + public static IEnumerable RunSPQuery(DbContext db,object data,Type parasType,Type returnType) { + var pm = getProcuderName(parasType); + var paras = getParams(data).ToArray(); + var connection = db.Database.GetDbConnection(); + using(var cmd = connection.CreateCommand()) { + cmd.CommandText = pm; + cmd.CommandType = System.Data.CommandType.StoredProcedure; + cmd.Parameters.AddRange(paras); + connection.Open(); + var dr = cmd.ExecuteReader(); + var result = new List(); + if(!dr.CanGetColumnSchema()) + return result; + while(dr.Read()) { + var item = returnType.Assembly.CreateInstance(returnType.FullName); + var columnSchema = dr.GetColumnSchema(); + for(var i = 0;i < columnSchema.Count;i++) { + var name = dr.GetName(i); + var value = dr.IsDBNull(i) ? null : dr.GetValue(i); + var pi = parasType.getProperty(name); + if(pi == null || !pi.CanWrite) + continue; + pi.SetValue(item,value); + } + result.Add(item); + } + connection.Close(); + return result; + } + } + /// /// 鑾峰彇瀛樺偍杩囩▼鍙傛暟鏋氫妇 /// @@ -88,6 +144,18 @@ namespace Falcon.ModelSP return typeof(T).Name; } + /// + /// 鑾峰彇瀛樺偍杩囩▼鍚 + /// + /// 绫诲瀷 + public static string getProcuderName(Type type) { + var attr = type.GetCustomAttribute(true); + if(attr != null && attr is FalconSPProcuderNameAttribute pna && !string.IsNullOrEmpty(pna.ProcuderName)) { + return pna.ProcuderName; + } + return type.Name; + } + /// /// 鏍规嵁浼犲叆鐨剆ql璇彞鍜屽弬鏁版灇涓炬墽琛屽瓨鍌ㄨ繃绋嬶紝骞朵笖杩斿洖绫诲瀷鏋氫妇 /// @@ -125,12 +193,22 @@ namespace Falcon.ModelSP return result; } } + + /// /// 蹇界暐澶у皬鍐欒幏鍙栫被鍨嬬殑灞炴 /// private static PropertyInfo getProperty(string name) { - var type = typeof(T); - foreach(var item in type.GetProperties()) { + return getProperty(typeof(T),name); + } + /// + /// 蹇界暐澶у皬鍐欒幏鍙栫被鍨嬬殑灞炴 + /// + /// 绫诲瀷 + /// 灞炴у悕绉 + /// 灞炴 + private static PropertyInfo getProperty(this Type t,string name) { + foreach(var item in t.GetProperties()) { if(item.Name.ToLower() == name.ToLower()) { return item; } diff --git a/Falcon.ModelSP/Falcon.ModelSP.csproj b/Falcon.ModelSP/Falcon.ModelSP.csproj index 67f0817..80cccd0 100644 --- a/Falcon.ModelSP/Falcon.ModelSP.csproj +++ b/Falcon.ModelSP/Falcon.ModelSP.csproj @@ -3,12 +3,12 @@ netstandard2.0;netstandard2.1 true - 1.0.3.0 - 1.0.3.0 - 1.0.3 + 1.0.4.0 + 1.0.4.0 + 1.0.4 bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml - http://39.105.71.191/Falcon/Falcon.ModelSP - http://39.105.71.191/Falcon/Falcon.ModelSP + http://106.14.65.137/Falcon/Falcon.ModelSP + http://106.14.65.137/Falcon/Falcon.ModelSP EF Core 瀛樺偍杩囩▼鎵ц缁勪欢锛屽彲浠ユā鍨嬪寲鎵ц瀛樺偍杩囩▼ Falcon diff --git a/Falcon.ModelSP/FalconSPProcuderNameAttribute.cs b/Falcon.ModelSP/FalconSPProcuderNameAttribute.cs index e5b83c2..2938feb 100644 --- a/Falcon.ModelSP/FalconSPProcuderNameAttribute.cs +++ b/Falcon.ModelSP/FalconSPProcuderNameAttribute.cs @@ -5,6 +5,7 @@ namespace Falcon.ModelSP /// /// 瀹氫箟瀛樺偍杩囩▼鍚嶇О /// + [AttributeUsage(AttributeTargets.Class)] public class FalconSPProcuderNameAttribute:Attribute { /// diff --git a/Falcon.ModelSP/FalconSPReturnTypeAttribute.cs b/Falcon.ModelSP/FalconSPReturnTypeAttribute.cs new file mode 100644 index 0000000..566f1f8 --- /dev/null +++ b/Falcon.ModelSP/FalconSPReturnTypeAttribute.cs @@ -0,0 +1,21 @@ +锘縰sing System; + +namespace Falcon.ModelSP +{ + /// + /// 瀹氫箟瀛樺偍杩囩▼杩斿洖鍊肩被鍨 + /// + [AttributeUsage(AttributeTargets.Class,AllowMultiple =false,Inherited =false)] + public class FalconSPReturnTypeAttribute:Attribute + { + /// + /// 杩斿洖鐨勬暟鎹被鍨 + /// + public Type ReturnType { get; set; } + /// + /// 瀹氫箟瀛樺偍杩囩▼鍚嶇О + /// + /// 瀛樺偍杩囩▼杩斿洖鍊肩被鍨 + public FalconSPReturnTypeAttribute(Type t) => this.ReturnType = t; + } +} diff --git a/Falcon.ModelSP/FalconSPRuner.cs b/Falcon.ModelSP/FalconSPRuner.cs index 625627a..7f869c5 100644 --- a/Falcon.ModelSP/FalconSPRuner.cs +++ b/Falcon.ModelSP/FalconSPRuner.cs @@ -27,8 +27,32 @@ namespace Falcon.ModelSP /// 瀛樺偍杩囩▼鍙傛暟 /// 杩斿洖鏁版嵁鏋氫妇 public IEnumerable RunSP(DbContext db,TPrarmType data) + where TResultType : class, new() { + return RunSPQuery(db,data); + } + + /// + /// 鎵ц瀛樺偍杩囩▼ + /// + /// 鍙傛暟绫诲瀷 + /// 杩斿洖鍊兼灇涓剧殑绫诲瀷 + /// 鏁版嵁涓婁笅鏂 + /// 瀛樺偍杩囩▼鍙傛暟 + /// 杩斿洖鏁版嵁鏋氫妇 + public IEnumerable RunSPQuery(DbContext db,TPrarmType data) where TResultType : class, new() { return db.RunProcuder(data); } + + /// + /// 鏍规嵁妯″瀷瀹氫箟鍙傛暟鎵ц瀛樺偍杩囩▼杩涜鏌ヨ锛屽弬鏁扮被鍨嬪繀椤诲畾涔塅alconSPReturnTypeAttribute鐗规 + /// + /// 瀛樺偍杩囩▼鍙傛暟绫诲瀷 + /// 鏁版嵁涓婁笅鏂 + /// 瀛樺偍杩囩▼鍙傛暟 + /// 杩斿洖绫诲瀷鏋氫妇FalconSPReturnTypeAttribute瀹氫箟鐨勭被鍨嬫灇涓俱 + public IEnumerable RunSPQuery(DbContext db,TPrarmType data) { + return db.RunSPQuery(data); + } } } diff --git a/Falcon.ModelSP/IFalconSPRuner.cs b/Falcon.ModelSP/IFalconSPRuner.cs index 2967d4b..0ee0e8e 100644 --- a/Falcon.ModelSP/IFalconSPRuner.cs +++ b/Falcon.ModelSP/IFalconSPRuner.cs @@ -24,5 +24,23 @@ namespace Falcon.ModelSP /// 鏁版嵁涓婁笅鏂 /// 鍙傛暟鏁版嵁 IEnumerable RunSP(DbContext db,TPrarmType data) where TResultType : class, new(); + + /// + /// 閫氳繃鏁版嵁搴撲笂涓嬫枃鎵ц瀛樺偍杩囩▼锛屽苟杩斿洖鏌ヨ缁撴灉 + /// + /// 鍙傛暟绫诲瀷 + /// 杩斿洖缁撴灉椤圭被鍨 + /// 鏁版嵁涓婁笅鏂 + /// 鍙傛暟鏁版嵁 + IEnumerable RunSPQuery(DbContext db,TPrarmType data) where TResultType : class, new(); + + /// + /// 鏍规嵁妯″瀷瀹氫箟鍙傛暟鎵ц瀛樺偍杩囩▼杩涜鏌ヨ锛屽弬鏁扮被鍨嬪繀椤诲畾涔塅alconSPReturnTypeAttribute鐗规 + /// + /// 瀛樺偍杩囩▼鍙傛暟绫诲瀷 + /// 鏁版嵁涓婁笅鏂 + /// 瀛樺偍杩囩▼鍙傛暟 + /// 杩斿洖绫诲瀷鏋氫妇FalconSPReturnTypeAttribute瀹氫箟鐨勭被鍨嬫灇涓俱 + IEnumerable RunSPQuery(DbContext db,TPrarmType data); } }