优化 FreeSql.Repository 使用方法

This commit is contained in:
28810 2019-02-28 20:38:26 +08:00
parent f034d4194d
commit 8e4d3c03a5
9 changed files with 74 additions and 13 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using repository_01.Repositorys;
using restful.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -15,6 +16,12 @@ namespace restful.Controllers {
public SongsController(IFreeSql fsql) {
_songRepository = new SongRepository(fsql);
//test code
var curd1 = fsql.GetRepository<Song, int>();
var curd2 = fsql.GetRepository<Song, string>();
var curd3 = fsql.GetRepository<Song, Guid>();
var curd4 = fsql.GetGuidRepository<Song>();
}
[HttpGet]

View File

@ -40,7 +40,7 @@ namespace FreeSql {
}
}
public virtual List<TEntity> Insert(List<TEntity> entity) {
public virtual List<TEntity> Insert(IEnumerable<TEntity> entity) {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
@ -66,7 +66,7 @@ namespace FreeSql {
}
}
public virtual Task<List<TEntity>> InsertAsync(List<TEntity> entity) {
public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entity) {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.1.5</Version>
<Version>0.1.5.3</Version>
<Authors>YeXiangQin</Authors>
<Description>FreeSql 通用仓库层现实,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。</Description>
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -11,14 +12,14 @@ namespace FreeSql {
public GuidRepository(IFreeSql fsql) : base(fsql) {
}
public override List<TEntity> Insert(List<TEntity> entity) {
public override List<TEntity> Insert(IEnumerable<TEntity> entity) {
_fsql.Insert<TEntity>().AppendData(entity).ExecuteAffrows();
return entity;
return entity.ToList();
}
async public override Task<List<TEntity>> InsertAsync(List<TEntity> entity) {
async public override Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entity) {
await _fsql.Insert<TEntity>().AppendData(entity).ExecuteAffrowsAsync();
return entity;
return entity.ToList();
}
public override TEntity Insert(TEntity entity) {

View File

@ -6,11 +6,11 @@ namespace FreeSql {
where TEntity : class {
TEntity Insert(TEntity entity);
List<TEntity> Insert(List<TEntity> entity);
List<TEntity> Insert(IEnumerable<TEntity> entity);
Task<TEntity> InsertAsync(TEntity entity);
Task<List<TEntity>> InsertAsync(List<TEntity> entity);
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entity);
int Update(TEntity entity);

View File

@ -1,6 +1,7 @@
using FreeSql;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Text;
public static class IFreeSqlExtenssions {
@ -12,10 +13,12 @@ public static class IFreeSqlExtenssions {
/// <typeparam name="TKey"></typeparam>
/// <param name="that"></param>
/// <returns></returns>
public static IRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that) where TEntity : class {
public static DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that) where TEntity : class {
return new DefaultRepository<TEntity, TKey>(that);
return dicGetRepository.GetOrAdd(typeof(TEntity), type1 => new ConcurrentDictionary<Type, IRepository>())
.GetOrAdd(typeof(TKey), type2 => new DefaultRepository<TEntity, TKey>(that)) as DefaultRepository<TEntity, TKey>;
}
static ConcurrentDictionary<Type, ConcurrentDictionary<Type, IRepository>> dicGetRepository = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, IRepository>>();
/// <summary>
/// 返回仓库类,适用 Insert 方法无须返回插入的数据
@ -23,8 +26,9 @@ public static class IFreeSqlExtenssions {
/// <typeparam name="TEntity"></typeparam>
/// <param name="that"></param>
/// <returns></returns>
public static IRepository<TEntity, Guid> GetGuidRepository<TEntity>(this IFreeSql that) where TEntity : class {
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that) where TEntity : class {
return new GuidRepository<TEntity>(that);
return dicGetGuidRepository.GetOrAdd(typeof(TEntity), type1 => new GuidRepository<TEntity>(that)) as GuidRepository<TEntity>;
}
static ConcurrentDictionary<Type, IRepository> dicGetGuidRepository = new ConcurrentDictionary<Type, IRepository>();
}

View File

@ -13,6 +13,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FreeSql.Repository\FreeSql.Repository.csproj" />
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
</ItemGroup>

View File

@ -9,8 +9,55 @@ using Xunit;
namespace FreeSql.Tests.Sqlite {
public class SqliteCodeFirstTest {
class Topic {
public Guid Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime CreateTime { get; set; }
}
[Table(Name = "xxxtb.Comment")]
class Comment {
public Guid Id { get; set; }
public Guid TopicId { get; set; }
public Topic Topic { get; set; }
public string Nickname { get; set; }
public string Content { get; set; }
public DateTime CreateTime { get; set; }
}
[Fact]
public void AddField() {
//秀一波 FreeSql.Repository 扩展包dotnet add package FreeSql.Repository
var topicRepository = g.sqlite.GetGuidRepository<Topic>();
var commentRepository = g.sqlite.GetGuidRepository<Comment>();
//添加测试文章
var topicId = FreeUtil.NewMongodbId();
topicRepository.Insert(new Topic {
Id = FreeUtil.NewMongodbId(),
Title = "文章标题1",
Content = "文章内容1",
CreateTime = DateTime.Now
});
//添加10条测试评论
var comments = Enumerable.Range(0, 10).Select(a => new Comment {
Id = FreeUtil.NewMongodbId(),
TopicId = topicId,
Nickname = $"昵称{a}",
Content = $"评论内容{a}",
CreateTime = DateTime.Now
});
var affrows = commentRepository.Insert(comments);
var find = commentRepository.Select.Where(a => a.Topic.Title == "文章标题1").ToList();
var sql = g.sqlite.CodeFirst.GetComparisonDDLStatements<TopicAddField>();
var id = g.sqlite.Insert<TopicAddField>().AppendData(new TopicAddField { }).ExecuteIdentity();

View File

@ -34,6 +34,7 @@ IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseAutoSyncStructure(true) //自动同步实体结构到数据库
.UseSyncStructureToLower(true) //转小写同步结构
.UseSyncStructureToUpper(true) //转大写同步结构
.UseLazyLoading(true) //延时加载导航属性对象,导航属性需要声明 virtual
.Build();