Skip to content

Commit

Permalink
升级到 支持efcore3.1
Browse files Browse the repository at this point in the history
增加sqlite支持
  • Loading branch information
luodaoyi committed Aug 30, 2020
1 parent 502b279 commit 778df97
Show file tree
Hide file tree
Showing 23 changed files with 277 additions and 103 deletions.
4 changes: 2 additions & 2 deletions Leo.Chimp.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.705
# Visual Studio Version 16
VisualStudioVersion = 16.0.30413.136
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leo.Chimp", "src\Leo.Chimp\Leo.Chimp.csproj", "{AE57FE9E-110F-4BBD-AF58-243A37E334C3}"
EndProject
Expand Down
52 changes: 25 additions & 27 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
## 背景
17年开始,公司开始向DotNet Core转型,面对ORM工具的选型,当时围绕Dapper和EF发生了激烈的讨论。项目团队更加关注快速交付,他们主张使用EF这种能快速开发的ORM工具而在线业务团队对性能有更高的要求,他们更希望使用能直接执行Sql语句的Dapper,这样可控性更高。而对于架构团队来说,满足开发团队的各种需求,提高他们的开发效率是最核心的价值所在,所以当时决定做一个混合型的既支持EF又支持dapper的数据仓储。
#### 背景
17年开始,公司开始向DotNet Core转型,面对ORM工具的选型,当时围绕Dapper和EF发生了激烈的讨论。项目团队更加关注快速交付,他们主张使用EF这种能快速开发的ORM工具;而在线业务团队对性能有更高的要求,他们更希望使用能直接执行Sql语句的Dapper这样可控性更高。而对于架构团队来说,满足开发团队的各种需求,提高他们的开发效率是最核心的价值所在,所以当时决定做一个混合型的既支持EF又支持dapper的数据仓储。

## 为什么选择EF+Dapper
目前来说EF和Dapper是.NET平台最主流的ORM工具,团队成员的接受程度很高,相关的资料非常齐全,学习成本很低,各种坑也最少。
#### 为什么选择EF+Dapper
目前来说EF和Dapper是.NET平台最主流的ORM工具,团队成员的接受程度很高,学习成本最低,因为主主流,所以相关的资料非常齐全,各种坑也最少。

## 介绍
#### 介绍
1. 它不是一个ORM工具,它不做任何关于数据底层的操作
2. 它是一个简易封装的数据库仓储和工作单元模型
3. 能帮助你快速的构建项目的数据访问层
4. 经过了2年多时间,10个项目组,大小近100多个线上项目的考验
5. 支持EF和Dapper,可以在项目中随意切换使用
6. 支持工作单元模式,也支持传统事务
6. 支持工作单元模式,也支持事务
7. 支持Mysql和Mssql
8. 支持同步和异步操作,推荐使用异步

> PS: 简单操作使用EF,复杂sql操作使用Dapper是快速开发的秘诀。
## 使用方法
#### 使用方法
引入nuget
```
<PackageReference Include="Leo.Chimp" Version="2.1.2" />
Expand Down Expand Up @@ -83,7 +82,7 @@ public class ValuesController : ControllerBase
}
}
```
## 详细使用说明
#### 详细使用说明
查询
```
//根据主键查询
Expand Down Expand Up @@ -116,20 +115,20 @@ _unitOfWork.QueryPagedListAsync<School>(1, 10, "select * from school order by id
```
//新增,支持批量新增
_schoolRepository.Insert(school);
await _unitOfWork.SaveChangesAsync();
_unitOfWork.SaveChanges();
```
```
//sql语句新增
await _unitOfWork.ExecuteAsync("insert school(id,name) values(@Id,@Name)",school);
await _unitOfWork.SaveChangesAsync();
_unitOfWork.ExecuteAsync("insert school(id,name) values(@Id,@Name)",
school);
```
编辑
```
//编辑,支持批量编辑
var school = await _schoolRepository.GetByIdAsync(Id);
var school = _schoolRepository.GetByIdAsync(Id);
school.Name="newschool";
_schoolRepository.Update(school);
await _unitOfWork.SaveChangesAsync();
_unitOfWork.SaveChanges();
```
```
//编辑,不用先查询
Expand All @@ -139,29 +138,29 @@ var school = new School
Name = "newschool"
};
_schoolRepository.Update(school, x => x.Name);
await _unitOfWork.SaveChangesAsync();
_unitOfWork.SaveChanges();
```
```
//sql语句编辑
await _unitOfWork.ExecuteAsync("update school set name=@Name where id=@Id",school);
await _unitOfWork.SaveChangesAsync();
_unitOfWork.ExecuteAsync("update school set name=@Name where id=@Id",
school);
```
删除
```
//删除,支持批量删除
_schoolRepository.Delete(school);
await _unitOfWork.SaveChangesAsync();
_unitOfWork.SaveChanges();
```
```
//根据lambda删除
_schoolRepository.Delete(x => x.Id == Id);
await _unitOfWork.SaveChangesAsync();
_unitOfWork.SaveChanges();
```
事务
```
//工作单元模式使用事务
await _schoolRepository.InsertAsync(school1);
await _schoolRepository.InsertAsync(school2);
await _schoolRepository.InsertAsync(school1);
await _unitOfWork.SaveChangesAsync();
```
```
Expand Down Expand Up @@ -204,12 +203,11 @@ using (var tran = _unitOfWork.BeginTransaction())
高级用法
```
//通过GetConnection可以使用更多dapper扩展的方法
await _unitOfWork.GetConnection().QueryAsync("select * from school");
_unitOfWork.GetConnection().QueryAsync("select * from school");
```
## 写在最后
Chimp核心是基于EF和Dapper的,所以EF和Dapper的功能都可以使用。比如导航属性,字段映射等等。这个库是线上项目核心依赖,会长期更新维护,希望大家能提出更好的意见。
QQ群:687800650 有问题可以加群交流
#### 写在最后
Chimp核心是基于EF和Dapper的,所以EF和Dapper一切功能都可以使用。比如导航属性,字段映射等等。这个库是线上项目核心依赖,会长期更新维护,希望大家能提出更好的意见。

## 项目地址
数据库脚本在根目录的sqlscript文件夹里面
[github地址](https://github.com/longxianghui/chimp.git)
#### 项目地址
数据库脚本在根目录的sqlscript文件夹里面
[github地址](https://github.com/longxianghui/chimp.git)
5 changes: 4 additions & 1 deletion example/Leo.Chimp.Domain/Entities/School.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace Leo.Chimp.Domain.Entities
{
[Table("school")]
public class School
{
public School()
{
Students = new List<Student>();
}

[Key]
public Guid Id { get; set; }
public string Name { get; set; }

Expand Down
4 changes: 4 additions & 0 deletions example/Leo.Chimp.Domain/Entities/Student.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
using System.Text.RegularExpressions;

namespace Leo.Chimp.Domain.Entities
{
[Table("student")]
public class Student : IEntity
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public Guid SchoolId { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion example/Leo.Chimp.Domain/Leo.Chimp.Domain.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 1 addition & 6 deletions example/Leo.Chimp.Example/Leo.Chimp.Example.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Leo.Chimp.Domain\Leo.Chimp.Domain.csproj" />
</ItemGroup>
Expand Down
10 changes: 7 additions & 3 deletions example/Leo.Chimp.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Leo.Chimp.Example
Expand All @@ -17,8 +18,11 @@ public static void Main(string[] args)
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static IHostBuilder CreateWebHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
17 changes: 13 additions & 4 deletions example/Leo.Chimp.Example/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand All @@ -26,21 +27,29 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddControllersWithViews();
services.AddChimp<ChimpDbContext>(
opt => opt.UseSqlServer("Server=10.0.0.99;Database=Chimp;Uid=sa;Pwd=Fuluerp123")
/*opt => opt.UseSqlServer("Server=10.0.0.99;Database=Chimp;Uid=sa;Pwd=Fuluerp123")*/
opt => opt.UseMySql("Server=192.168.5.5;Database=Chimp;Uid=root;Pwd='luodaoyi';SslMode=none")
);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseMvc();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
});
}
}
}
23 changes: 23 additions & 0 deletions sqlscript/sqllite/script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

-- ----------------------------
-- Table structure for school
-- ----------------------------
DROP TABLE IF EXISTS School;
CREATE TABLE School (
Id CHAR(32) PRIMARY KEY
NOT NULL,
Name VARCHAR (255)
);

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS Student;
CREATE TABLE Student (
Id CHAR(32) NOT NULL
PRIMARY KEY,
Name VARCHAR (255),
Age INT,
Birthday DATETIME,
SchoolId CHAR (32)
);
16 changes: 16 additions & 0 deletions src/Leo.Chimp/DapperAdapter/SqliteAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Leo.Chimp.DapperAdapter
{
public class SqliteAdapter : ISqlAdapter
{
public virtual string PagingBuild(ref PartedSql partedSql, object args, long skip, long take)
{
var pageSql = $"{partedSql.Raw} LIMIT {take} OFFSET {skip}";
return pageSql;
}

}
}
5 changes: 3 additions & 2 deletions src/Leo.Chimp/EfCoreRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Data;
Expand Down Expand Up @@ -31,7 +32,7 @@ public TEntity GetById(object id)

return Entities.Find(id);
}
public Task<TEntity> GetByIdAsync(object id)
public ValueTask<TEntity> GetByIdAsync(object id)
{
if (id == null)
throw new ArgumentNullException("id");
Expand All @@ -56,7 +57,7 @@ public void Insert(IEnumerable<TEntity> entities)
Entities.AddRange(entities);
}

public Task InsertAsync(TEntity entity)
public ValueTask<EntityEntry<TEntity>> InsertAsync(TEntity entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
Expand Down
5 changes: 3 additions & 2 deletions src/Leo.Chimp/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Query;

namespace Leo.Chimp
Expand All @@ -30,7 +31,7 @@ public interface IRepository<TEntity> where TEntity : class
/// </summary>
/// <param name="id">主键</param>
/// <returns>Entity</returns>
Task<TEntity> GetByIdAsync(object id);
ValueTask<TEntity> GetByIdAsync(object id);

/// <summary>
/// Table Tracking
Expand Down Expand Up @@ -60,7 +61,7 @@ public interface IRepository<TEntity> where TEntity : class
/// 添加实体
/// </summary>
/// <param name="entity">Inserted entity</param>
Task InsertAsync(TEntity entity);
ValueTask<EntityEntry<TEntity>> InsertAsync(TEntity entity);

/// <summary>
/// InsertAsync
Expand Down
3 changes: 2 additions & 1 deletion src/Leo.Chimp/KingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class KingOptions
public enum DbType
{
MSSQL,
MYSQL
MYSQL,
SQLITE
}

}
21 changes: 11 additions & 10 deletions src/Leo.Chimp/Leo.Chimp.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageProjectUrl>https://github.com/longxianghui/chimp</PackageProjectUrl>
<TargetFramework>netstandard2.1</TargetFramework>
<PackageProjectUrl>https://github.com/luodaoyi/chimp</PackageProjectUrl>
<LangVersion>7.3</LangVersion>
<Version>2.1.2</Version>
<Authors>longxianghui</Authors>
<Version>3.0.0</Version>
<Authors>luodaoyi</Authors>
<Description>ef and dapper repository.</Description>
<PackageTags>repository;unit of work;ef;dapper;</PackageTags>
<RepositoryUrl>https://github.com/longxianghui/chimp</RepositoryUrl>
<RepositoryUrl>https://github.com/luodaoyi/chimp</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.30" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.11" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.4" />
<PackageReference Include="Dapper" Version="2.0.35" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.1.6" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.2" />
</ItemGroup>

</Project>
Loading

0 comments on commit 778df97

Please sign in to comment.