using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace SqlSugar { public interface ISugarUnitOfWork where T : SugarUnitOfWork, new() { ISqlSugarClient Db { get; set; } T CreateContext(bool isTran=true); } public class SugarUnitOfWork : ISugarUnitOfWork where T : SugarUnitOfWork, new() { public SugarUnitOfWork(ISqlSugarClient db) { this.Db = db; } public ISqlSugarClient Db { get; set; } public T CreateContext(bool isTran=true) { return Db.CreateContext(isTran); } } public interface ISugarUnitOfWorkClear { RepositoryType GetMyRepository() where RepositoryType : new(); bool Commit(); } /// /// ISugarUnitOfWorkClear not exists SqlSugar method /// ISugarUnitOfWork exists SqlSugar method /// public interface ISugarUnitOfWork : ISugarUnitOfWorkClear { ISqlSugarClient Db { get; } ITenant Tenant { get; } SimpleClient GetRepository() where T : class, new(); } /// /// SugarUnitOfWork->ISugarUnitOfWork->ISaugarUnitOfWorkClear /// ISaugarUnitOfWorkClear not exists SqlSugar method /// ISugarUnitOfWork exists SqlSugar method /// public class SugarUnitOfWork : IDisposable, ISugarUnitOfWork { public ISqlSugarClient Db { get; internal set; } public ITenant Tenant { get; internal set; } public bool IsTran { get; internal set; } public bool IsCommit { get; internal set; } public bool IsClose { get; internal set; } public void Dispose() { if (this.IsTran && IsCommit == false) { this.Tenant.RollbackTran(); } if (this.Db.Ado.Transaction==null&&IsClose == false) { this.Db.Close(); } } public SimpleClient GetRepository() where T : class, new() { TenantAttribute tenantAttribute = typeof(T).GetCustomAttribute(); if (tenantAttribute == null) { return new SimpleClient(Db); } else { return new SimpleClient(Db.AsTenant().GetConnection(tenantAttribute.configId)); } } public RepositoryType GetMyRepository() where RepositoryType:new() { var result = (ISugarRepository)new RepositoryType(); var type = typeof(RepositoryType).GetGenericArguments()[0]; TenantAttribute tenantAttribute = type.GetCustomAttribute(); if (tenantAttribute == null) { result.Context = this.Db; } else { result.Context = this.Db.AsTenant().GetConnection(tenantAttribute.configId); } return (RepositoryType)result; } public bool Commit() { if (this.IsTran && this.IsCommit == false) { this.Tenant.CommitTran(); IsCommit = true; } if (this.Db.Ado.Transaction==null&&this.IsClose == false) { this.Db.Close(); IsClose = true; } return IsCommit; } } }