Db.CreateContext

This commit is contained in:
sunkaixuan 2022-05-23 23:48:27 +08:00
parent b9c5395dae
commit f98bb577fd
6 changed files with 87 additions and 48 deletions

View File

@ -14,59 +14,55 @@ namespace OrmTest
Console.WriteLine("");
Console.WriteLine("#### DemoM_UnitOfWork ####");
DbContext.Db.UseTran(() =>
var db=NewUnitTest.Db;
using (var uow = db.CreateContext<MyDbContext>())
{
var id = DbContext.CustomDal.InsertReturnIdentity(new Custom() { Id = 1, Name = "guid" });
var id2 = DbContext.OrderDal.InsertReturnIdentity(new Order() { Name = "guid2", Price = 0, CreateTime = DateTime.Now, CustomId = 1 });
throw new Exception("");
},
e =>
var o = uow.GetRepository<Order>();
var o2 = uow.GetMyRepository<DbSet<Order>>();
var list = o.GetList();//默认仓储
var list2 = o2.CommQuery();//自定义仓储
var list3 = uow.Orders1.GetList();//MyDbContext中的默认仓储
var list4 = uow.Orders2.GetList();//MyDbContext中的自定义仓储
uow.Commit();
}
var d = DateTime.Now;
for (int i = 0; i < 100000; i++)
{
//throw e;
});
Console.WriteLine("");
db.CreateContext<MyDbContext>();
}
Console.WriteLine("CreateContext 100000" + (DateTime.Now-d).TotalMilliseconds+"ms");
Console.WriteLine("#### Saveable End ####");
}
public class DbContext
/// <summary>
/// 自定义DbContext
/// </summary>
public class MyDbContext : SugarUnitOfWork
{
public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
{
DbType = SqlSugar.DbType.SqlServer,
ConnectionString = Config.ConnectionString,
IsAutoCloseConnection = true
}, db => {
//单例参数配置,所有上下文生效
db.Aop.OnLogExecuting = (s, p) =>
{
Console.WriteLine(s);
};
});
public static DbSet<Order> OrderDal => new DbSet<Order>();
public static DbSet<Custom> CustomDal => new DbSet<Custom>();
/// <summary>
/// 原生仓储
/// </summary>
public SimpleClient<Order> Orders1 { get; set; }
/// <summary>
///自定义仓储
/// </summary>
public DbSet<Order> Orders2 { get; set; }
}
/// <summary>
/// 自定义仓储
/// </summary>
/// <typeparam name="T"></typeparam>
public class DbSet<T> : SimpleClient<T> where T : class, new()
{
public DbSet(ISqlSugarClient context = null) : base(context)//需要有构造参数
{
base.Context = DbContext.Db;
}
/// <summary>
/// 扩展方法,自带方法不能满足的时候可以添加新方法
/// 仓储自定义方法
/// </summary>
/// <returns></returns>
public List<T> CommQuery(string json)
public List<T> CommQuery()
{
//base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做复杂操作
return null;
return base.Context.Queryable<T>().ToList();
}
}
}
}
}

View File

@ -956,6 +956,11 @@ namespace SqlSugar
#endregion
#region SimpleClient
public T CreateContext<T>(bool isTran) where T : SugarUnitOfWork, new()
{
Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContextchildDb不能使用");
return null;
}
public SugarUnitOfWork CreateContext(bool isTran = true)
{
Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContextchildDb不能使用");

View File

@ -188,6 +188,11 @@ namespace SqlSugar
{
return ScopedContext.GetDate();
}
public T CreateContext<T>(bool isTran) where T : SugarUnitOfWork, new()
{
Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContextchildDb不能使用");
return null;
}
public SugarUnitOfWork CreateContext(bool isTran = true)
{
Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContextchildDb不能使用");

View File

@ -44,6 +44,7 @@ namespace SqlSugar
#endregion
#region Other methods
T CreateContext<T>(bool isTran=true) where T : SugarUnitOfWork, new();
SugarUnitOfWork CreateContext(bool isTran = true);
SplitTableContext SplitHelper<T>() where T : class, new();
SplitTableContextResult<T> SplitHelper<T>(T data) where T : class, new();

View File

@ -67,17 +67,45 @@ namespace SqlSugar
#endregion
#region SimpleClient
public T CreateContext<T>(bool isTran=true) where T : SugarUnitOfWork, new()
{
T result = new T();
_CreateContext(isTran, result);
var type = typeof(T);
var ps = type.GetProperties();
var cacheKey = "SugarUnitOfWork" + typeof(T).FullName + typeof(T).GetHashCode();
var properies = new ReflectionInoCacheService().GetOrCreate(cacheKey,
() =>
ps.Where(it =>
(it.PropertyType.BaseType != null && it.PropertyType.BaseType.Name.StartsWith("SimpleClient`"))
||
it.PropertyType.Name.StartsWith("SimpleClient`")
));
foreach (var item in properies)
{
var value = Activator.CreateInstance(item.PropertyType);
value.GetType().GetProperty("Context").SetValue(value, this);
item.SetValue(result, value);
}
return result;
}
public SugarUnitOfWork CreateContext(bool isTran = true)
{
SugarUnitOfWork sugarUnitOf = new SugarUnitOfWork();
return _CreateContext(isTran, sugarUnitOf);
}
private SugarUnitOfWork _CreateContext(bool isTran, SugarUnitOfWork sugarUnitOf)
{
sugarUnitOf.Db = this;
sugarUnitOf.Tenant = this;
sugarUnitOf.IsTran = true;
this.Open();
if (isTran)
{
this.BeginTran();
}
return sugarUnitOf;
}
public SimpleClient<T> GetSimpleClient<T>() where T : class, new()

View File

@ -175,6 +175,10 @@ namespace SqlSugar
return ScopedContext.GetDate();
}
public T CreateContext<T>(bool isTran = true) where T : SugarUnitOfWork, new()
{
return ScopedContext.CreateContext<T>(isTran);
}
public SugarUnitOfWork CreateContext(bool isTran = true)
{
return ScopedContext.CreateContext(isTran);