Asp.net Core 3.1 引用ORM工具包 yrjw.ORM.Chimp(EF + dapper + Autofac)使用教程

yrjw.ORM.Chimp

介紹

It is not the encapsulation of ORM,a based on EF + dapper + Autofac, is repository and unitofworkgit

安裝教程

  1. 程序包管理器控制檯,使用 NuGet命令安裝。 PM> Install-Package yrjw.ORM.Chimpgithub

  2. 或者直接在項目文件.csproj中引入包api

使用說明

  1. 建立實體對象,繼承IEntity
    /// <summary>
    /// 學生信息表
    /// </summary>
    [Table("StudentInfo")]
    public class StudentInfo: IEntity
    {
        [Key]
        public virtual int Id { get; set; }
        /// <summary>
        /// 學生姓名
        /// </summary>
        [Required]
        [Column(TypeName = "varchar(50)")]
        public string Name { get; set; }
        /// <summary>
        /// 性別
        /// </summary>
        public int Sex { get; set; }
        /// <summary>
        /// 民族
        /// </summary>
        public int NationId { get; set; }
        /// <summary>
        /// 電話
        /// </summary>
        public string Phone { get; set; }
    }
  1. 建立上下文,繼承BaseDbContext,使用base.OnModelCreating(),無需添加DbSet
    public class myDbContext: BaseDbContext
    {
        public myDbContext()
        {
        }

        public myDbContext(DbContextOptions options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Seed(); //種子數據
        }
    }
  1. 添加擴展方法,初始化種子數據。
    public static class ModelBuilderExtensions
    {
        /// <summary>
        /// 種子數據
        /// </summary>
        /// <param name="modelBuilder"></param>
        public static void Seed(this ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<StudentInfo>().HasData(
                new StudentInfo { Id = 1, Name = "張三", Sex = 1, NationId = 1, Phone="13902451189"}
                );
        }
    }
  1. 建立倉儲接口和實現類,service繼承IDependency接口,可支持經過屬性依賴注入方式(使用Autofac依賴注入)。
    public interface IStudentInfoService
    {
        IUnitOfWork UnitOfWork { get; }

        Task<IResultModel> QueryList();
    }
    public class StudentInfoService: IStudentInfoService, IDependency
    {
        private readonly Lazy<IMapper> _mapper;
        private readonly Lazy<IRepository<StudentInfo>> repStudentInfo;

        public IUnitOfWork UnitOfWork { get; }

        public StudentInfoService(Lazy<IMapper> mapper, IUnitOfWork unitOfWork, Lazy<IRepository<StudentInfo>> repStudentInfo)
        {
            this._mapper = mapper;
            this.UnitOfWork = unitOfWork;
            this.repStudentInfo = repStudentInfo;
        }

        public async Task<IResultModel> QueryList()
        {
            var list = await repStudentInfo.Value.TableNoTracking.ProjectTo<StudentInfoDTO>(_mapper.Value.ConfigurationProvider).ToListAsync();
            return ResultModel.Success<IList<StudentInfoDTO>>(list);
        }
    }
  1. 添加控制器Controller
    [Description("學生信息")]
    [Route("api/[controller]/[action]")]
    public class StudentInfoController : ControllerBase
    {
        private readonly ILogger<StudentInfoController> _logger;

        public Lazy<IStudentInfoService> StudentInfoService { get; set; }

        public StudentInfoController(ILogger<StudentInfoController> logger)
        {
            _logger = logger;
        }

        [Description("獲取學生列表")]
        [ResponseCache(Duration = 0)]
        [HttpGet]
        public Task<IResultModel> QueryList()
        {
            return StudentInfoService.Value.QueryList();
        }
    }

 

  1. 最關鍵一步,Startup.cs中注入服務,setting.AssemblyName爲當前運行的api程序集命名空間。
    public virtual void ConfigureServices(IServiceCollection services)
    {
        if (setting.DbType == yrjw.ORM.Chimp.DbType.MYSQL)
        {
            services.AddChimp<myDbContext>(opt => opt.UseMySql(setting.ConnectionString,
                b => b.MigrationsAssembly(setting.AssemblyName)));
        }
        else
        {
            services.AddChimp<myDbContext>(opt => opt.UseSqlServer(setting.ConnectionString,
                b => b.MigrationsAssembly(setting.AssemblyName)));
        }
    }

 

詳細使用說明

使用說明app

關於Chimp

在Leo.Chimp包基礎上添加了Autofac依賴注入,封裝返回接口模型IResultModel,支持.net core 3.1版本上使用。async

相關文章
相關標籤/搜索