如何用Dependency injection + async,不知道是否正确, 不知是否能自动关闭connection 返回

求助
2 138
该叫什么 kim 发布于2019/4/12
悬赏:0 飞吻


大神们 帮忙看看下面代码


Models:

    [SugarTable("User")]
    public partial class User
    {
           public User(){
           }
           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:False
           /// </summary>           
           [SugarColumn(IsPrimaryKey=true,IsIdentity=true)]
           public int Id {get;set;}
           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public string Name {get;set;}
    }


Interface DBContext:


  public interface IDbContext: IDisposable
    {
        SqlSugarClient Db { get; set;}
        SimpleClient<User> User{ get; }
    }


Class  DbConext: 


public class DbContext: IDbContext
{
    private bool disposed = false;
    private readonly IConfiguration _configuration;
    public DbContext(IConfiguration configuration)
    {
        this._configuration = configuration;
        this.Db = new SqlSugarClient(new ConnectionConfig()
        {
            ConnectionString = this._configuration["ConnectionString:CSPEntity"],
            DbType = DbType.SqlServer,
            InitKeyType = InitKeyType.Attribute,
            IsAutoCloseConnection = true,// open auto close connection
        });
        //It is used for debug 
        this.Db.Aop.OnLogExecuting = (sql, pars) =>
        {
            Console.WriteLine(sql + "\r\n" +
                this.Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            Console.WriteLine();
        };
    }
    public SqlSugarClient Db { get; set;}
    public SimpleClient<User> UserDb => new SimpleClient<User>(this.Db);
    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                // Free any other managed objects here.
                //              
            }
            // Free any unmanaged objects here.
            //
            this.Db.Dispose();
            this.disposed = true;
        }
    }
    ~DbContext()
    {
        this.Dispose(false);
    }
    public void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }
}


Repository Interface:


    public interface IUserRepository: IDisposable
    {
        Task<int> AddUser(User user);
    }

Repository Class:


    public class UserRepository: IUserRepository
    {
        private bool disposed = false;
        private readonly IDbContext _dbContext;
        public UserRepository(IDbContext dbContext) => this._dbContext = dbContext;
        public async Task<int> AddVet(User user) => await Task.Run(() => {
            return this._dbContext.VetsDb.InsertReturnIdentity(vet);
            });
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    // Free any other managed objects here.
                    //              
                }
                // Free any unmanaged objects here.
                //
                this._dbContext.Dispose();
                this.disposed = true;
            }
        }
        ~UserRepository()
        {
            this.Dispose(false);
        }
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }
    }


热忱回答2

  •   IsAutoCloseConnection = true,// open auto close connection
            });

    这一句就好了 注入都不需要

    0 回复
  • kim kim VIP0
    2019/4/12

    @fate stay night:TY

    0 回复

版块

学习文档

最新会员

发布达人

回贴达人