# yrjw.ORM.Chimp **Repository Path**: yuespirit/yrjw.ORM.Chimp ## Basic Information - **Project Name**: yrjw.ORM.Chimp - **Description**: It is not the encapsulation of ORM,a based on EF + dapper + Autofac, is repository and unitofwork - **Primary Language**: C# - **License**: ISC - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 5 - **Created**: 2021-09-29 - **Last Updated**: 2021-09-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # yrjw.ORM.Chimp #### 介绍 It is not the encapsulation of ORM,a based on EF + dapper + Autofac, is repository and unitofwork #### 安装教程 1. 程序包管理器控制台,使用 NuGet命令安装。 PM> Install-Package yrjw.ORM.Chimp 2. 或者直接在项目文件.csproj中引入包 #### 使用说明 1. 创建实体对象,继承IEntity ``` /// /// 学生信息表 /// [Table("StudentInfo")] public class StudentInfo: IEntity { [Key] public virtual int Id { get; set; } /// /// 学生姓名 /// [Required] [Column(TypeName = "varchar(50)")] public string Name { get; set; } /// /// 性别 /// public int Sex { get; set; } /// /// 民族 /// public int NationId { get; set; } /// /// 电话 /// public string Phone { get; set; } } ``` 2. 创建上下文,继承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(); //种子数据 } } ``` 3. 添加扩展方法,初始化种子数据。 ``` public static class ModelBuilderExtensions { /// /// 种子数据 /// /// public static void Seed(this ModelBuilder modelBuilder) { modelBuilder.Entity().HasData( new StudentInfo { Id = 1, Name = "张三", Sex = 1, NationId = 1, Phone="13902451189"} ); } } ``` 4. 创建仓储接口和实现类,service继承IDependency接口,可支持通过属性依赖注入方式(使用Autofac依赖注入)。 ``` public interface IStudentInfoService { IUnitOfWork UnitOfWork { get; } Task QueryList(); } ``` ``` public class StudentInfoService: IStudentInfoService, IDependency { private readonly Lazy _mapper; private readonly Lazy> repStudentInfo; public IUnitOfWork UnitOfWork { get; } public StudentInfoService(Lazy mapper, IUnitOfWork unitOfWork, Lazy> repStudentInfo) { this._mapper = mapper; this.UnitOfWork = unitOfWork; this.repStudentInfo = repStudentInfo; } public async Task QueryList() { var list = await repStudentInfo.Value.TableNoTracking.ProjectTo(_mapper.Value.ConfigurationProvider).ToListAsync(); return ResultModel.Success>(list); } } ``` 5. 添加控制器Controller ``` [Description("学生信息")] [Route("api/[controller]/[action]")] public class StudentInfoController : ControllerBase { private readonly ILogger _logger; public Lazy StudentInfoService { get; set; } public StudentInfoController(ILogger logger) { _logger = logger; } [Description("获取学生列表")] [ResponseCache(Duration = 0)] [HttpGet] public Task QueryList() { return StudentInfoService.Value.QueryList(); } } ``` 6. 最关键一步,Startup.cs中注入服务,setting.AssemblyName为当前运行的api程序集命名空间。 ``` public virtual void ConfigureServices(IServiceCollection services) { if (setting.DbType == yrjw.ORM.Chimp.DbType.MYSQL) { services.AddChimp(opt => opt.UseMySql(setting.ConnectionString, b => b.MigrationsAssembly(setting.AssemblyName))); } else { services.AddChimp(opt => opt.UseSqlServer(setting.ConnectionString, b => b.MigrationsAssembly(setting.AssemblyName))); } } ``` #### 详细使用说明 [使用说明](https://github.com/longxianghui/chimp) #### 关于Chimp 在Leo.Chimp包基础上添加了Autofac依赖注入,封装返回接口模型IResultModel,支持.net core 3.1版本上使用。