Minimal Api<三>创建仓储层

一、创建IRepository相关接口

 public interface IRepositoryBase : IDisposable
    {
        IRepositoryBase BeginTrans();
        int Commit();
        int Insert<TEntity>(TEntity entity) where TEntity : class;
        int Insert<TEntity>(List<TEntity> entitys) where TEntity : class;
        int Update<TEntity>(TEntity entity) where TEntity : class;
        int Delete<TEntity>(TEntity entity) where TEntity : class;
        int Delete<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class;
        TEntity FindEntity<TEntity>(object keyValue) where TEntity : class;
        TEntity FindEntity<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class;
        IQueryable<TEntity> IQueryable<TEntity>() where TEntity : class;
        IQueryable<TEntity> IQueryable<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class;
     }
 public  interface ISysUserRepository:IRepositoryBase
    {
         
    }

 二、实现接口,新建一个类库命名后缀是Repository,结构如下

 public class SysUserRepository<TDbContext> :RepositoryBase<TDbContext>, ISysUserRepository where TDbContext : DbContext
    {
        public SysUserRepository(TDbContext dbcontext):base(dbcontext)
        {            
        }
    }
 /// <summary>
    /// 仓储实现
    /// </summary>
    public class RepositoryBase<TDbcontext> : IRepositoryBase, IDisposable where TDbcontext : DbContext
    {
        private TDbcontext DBcontext { get; set; }

        public RepositoryBase(TDbcontext context)
        {
            DBcontext = context;
        }
       
        private DbTransaction dbTransaction { get; set; }
        public IRepositoryBase BeginTrans()
        {
            DbConnection dbConnection = DBcontext.Database.GetDbConnection();
            if (dbConnection.State == ConnectionState.Closed)
            {
                dbConnection.Open();
            }
            dbTransaction = dbConnection.BeginTransaction();
            return this;
        }
        public int Commit()
        {
            try
            {
                var returnValue = DBcontext.SaveChanges();
                if (dbTransaction != null)
                {
                    dbTransaction.Commit();
                }
                return returnValue;
            }
            catch (Exception)
            {
                if (dbTransaction != null)
                {
                    this.dbTransaction.Rollback();
                }
                throw;
            }
            finally
            {
                this.Dispose();
            }
        }
        public void Dispose()
        {
            if (dbTransaction != null)
            {
                this.dbTransaction.Dispose();
            }
            this.DBcontext.Dispose();
        }
        public int Insert<TEntity>(TEntity entity) where TEntity : class
        {
            DBcontext.Entry<TEntity>(entity).State = EntityState.Added;
            return dbTransaction == null ? this.Commit() : 0;
        }
        public int Insert<TEntity>(List<TEntity> entitys) where TEntity : class
        {
            foreach (var entity in entitys)
            {
                DBcontext.Entry<TEntity>(entity).State = EntityState.Added;
            }
            return dbTransaction == null ? this.Commit() : 0;
        }
        public int Update<TEntity>(TEntity entity) where TEntity : class
        {
            DBcontext.Set<TEntity>().Attach(entity);
            PropertyInfo[] props = entity.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.GetValue(entity, null) != null)
                {
                    if (prop.GetValue(entity, null).ToString() == "&nbsp;")
                        DBcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
                    DBcontext.Entry(entity).Property(prop.Name).IsModified = true;
                }
            }
            return dbTransaction == null ? this.Commit() : 0;
        }
        public int Delete<TEntity>(TEntity entity) where TEntity : class
        {
            DBcontext.Set<TEntity>().Attach(entity);
            DBcontext.Entry<TEntity>(entity).State = EntityState.Deleted;
            return dbTransaction == null ? this.Commit() : 0;
        }
        public int Delete<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            var entitys = DBcontext.Set<TEntity>().Where(predicate).ToList();
            entitys.ForEach(m => DBcontext.Entry<TEntity>(m).State = EntityState.Deleted);
            return dbTransaction == null ? this.Commit() : 0;
        }
        public TEntity FindEntity<TEntity>(object keyValue) where TEntity : class
        {
            return DBcontext.Set<TEntity>().Find(keyValue);
        }
        public TEntity FindEntity<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            return DBcontext.Set<TEntity>().FirstOrDefault(predicate);
        }
        public IQueryable<TEntity> IQueryable<TEntity>() where TEntity : class
        {
            return DBcontext.Set<TEntity>();
        }
        public IQueryable<TEntity> IQueryable<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            return DBcontext.Set<TEntity>().Where(predicate);
        }
    }
 public static class RepositoryMapExtensions
    {
        public static IServiceCollection AddRepository<TDbContext>(this IServiceCollection services) where TDbContext : DbContext
        {
            services.AddScoped<IRepositoryBase,RepositoryBase<TDbContext>>();
            services.AddScoped<ISysUserRepository, SysUserRepository<TDbContext>>();
            return services;
        }
    }

四、将repository类注入管道,在api项目的program.cs中加入下列参数。

builder.Services.AddRepository<AidenDbContext>();

 

posted @ 2022-03-13 22:24  许轩霖  阅读(264)  评论(0编辑  收藏  举报