diff --git a/Src/Asp.Net/SqlServerTest/Demo/DemoG_SimpleClient.cs b/Src/Asp.Net/SqlServerTest/Demo/DemoG_SimpleClient.cs index ad454efc5..dc278f68b 100644 --- a/Src/Asp.Net/SqlServerTest/Demo/DemoG_SimpleClient.cs +++ b/Src/Asp.Net/SqlServerTest/Demo/DemoG_SimpleClient.cs @@ -22,8 +22,10 @@ namespace OrmTest } public class OrderDal:Repository { - public void MyTest() { - + public void MyTest() + { + base.CommQuery("1=1"); + base.ChangeRepository>().CommQuery("1=1"); } } public class Repository : SimpleClient where T : class, new() @@ -32,13 +34,18 @@ namespace OrmTest { if (context == null) { - base.Context = new SqlSugarClient(new ConnectionConfig() + var db = new SqlSugarClient(new ConnectionConfig() { DbType = SqlSugar.DbType.SqlServer, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConnectionString = Config.ConnectionString }); + base.Context = db; + db.Aop.OnLogExecuting = (s, p) => + { + Console.WriteLine(s); + }; } } @@ -46,10 +53,10 @@ namespace OrmTest /// 扩展方法,自带方法不能满足的时候可以添加新方法 /// /// - public List CommQuery(string json) + public List CommQuery(string sql) { //base.Context.Queryable().ToList();可以拿到SqlSugarClient 做复杂操作 - return null; + return base.Context.Queryable().Where(sql).ToList(); } } diff --git a/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs b/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs index 7d1a8f31c..9a3880200 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs @@ -8,6 +8,7 @@ namespace SqlSugar public interface ISimpleClient where T : class, new() { SimpleClient Change() where ChangeType : class, new(); + RepositoryType ChangeRepository() where RepositoryType : ISugarRepository ; IDeleteable AsDeleteable(); IInsertable AsInsertable(List insertObjs); IInsertable AsInsertable(T insertObj); diff --git a/Src/Asp.Net/SqlSugar/SimpleClient.cs b/Src/Asp.Net/SqlSugar/SimpleClient.cs index 7a4be1987..358e3b3a3 100644 --- a/Src/Asp.Net/SqlSugar/SimpleClient.cs +++ b/Src/Asp.Net/SqlSugar/SimpleClient.cs @@ -2,16 +2,17 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Reflection; using System.Text; using System.Threading.Tasks; namespace SqlSugar { - public partial class SimpleClient : ISimpleClient where T : class, new() + public partial class SimpleClient : ISugarRepository,ISimpleClient where T : class, new() { #region Interface - protected ISqlSugarClient Context { get; set; } + public ISqlSugarClient Context { get; set; } public ITenant AsTenant() { @@ -34,6 +35,17 @@ namespace SqlSugar { return this.Context.GetSimpleClient(); } + public RepositoryType ChangeRepository() where RepositoryType : ISugarRepository + { + Type type = typeof(RepositoryType); + object o = Activator.CreateInstance(type,new string[]{ null}); + var result= (RepositoryType)o; + if (result.Context == null) + { + result.Context = this.Context; + } + return result; + } public ISugarQueryable AsQueryable() { return Context.Queryable(); @@ -291,4 +303,9 @@ namespace SqlSugar [Obsolete("Use AsSugarClient()")] public ISqlSugarClient FullClient { get { return this.Context; } } } + + public interface ISugarRepository + { + ISqlSugarClient Context { get; set; } + } }