mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
优化 FreeSql.Repository 使用方法
This commit is contained in:
parent
f034d4194d
commit
8e4d3c03a5
@ -1,6 +1,7 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using repository_01.Repositorys;
|
using repository_01.Repositorys;
|
||||||
using restful.Entitys;
|
using restful.Entitys;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -15,6 +16,12 @@ namespace restful.Controllers {
|
|||||||
|
|
||||||
public SongsController(IFreeSql fsql) {
|
public SongsController(IFreeSql fsql) {
|
||||||
_songRepository = new SongRepository(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]
|
[HttpGet]
|
||||||
|
@ -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) {
|
switch (_fsql.Ado.DataType) {
|
||||||
case DataType.SqlServer:
|
case DataType.SqlServer:
|
||||||
case DataType.PostgreSQL:
|
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) {
|
switch (_fsql.Ado.DataType) {
|
||||||
case DataType.SqlServer:
|
case DataType.SqlServer:
|
||||||
case DataType.PostgreSQL:
|
case DataType.PostgreSQL:
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<Version>0.1.5</Version>
|
<Version>0.1.5.3</Version>
|
||||||
<Authors>YeXiangQin</Authors>
|
<Authors>YeXiangQin</Authors>
|
||||||
<Description>FreeSql 通用仓库层现实,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。</Description>
|
<Description>FreeSql 通用仓库层现实,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。</Description>
|
||||||
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -11,14 +12,14 @@ namespace FreeSql {
|
|||||||
public GuidRepository(IFreeSql fsql) : base(fsql) {
|
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();
|
_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();
|
await _fsql.Insert<TEntity>().AppendData(entity).ExecuteAffrowsAsync();
|
||||||
return entity;
|
return entity.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override TEntity Insert(TEntity entity) {
|
public override TEntity Insert(TEntity entity) {
|
||||||
|
@ -6,11 +6,11 @@ namespace FreeSql {
|
|||||||
where TEntity : class {
|
where TEntity : class {
|
||||||
TEntity Insert(TEntity entity);
|
TEntity Insert(TEntity entity);
|
||||||
|
|
||||||
List<TEntity> Insert(List<TEntity> entity);
|
List<TEntity> Insert(IEnumerable<TEntity> entity);
|
||||||
|
|
||||||
Task<TEntity> InsertAsync(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);
|
int Update(TEntity entity);
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using FreeSql;
|
using FreeSql;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
public static class IFreeSqlExtenssions {
|
public static class IFreeSqlExtenssions {
|
||||||
@ -12,10 +13,12 @@ public static class IFreeSqlExtenssions {
|
|||||||
/// <typeparam name="TKey"></typeparam>
|
/// <typeparam name="TKey"></typeparam>
|
||||||
/// <param name="that"></param>
|
/// <param name="that"></param>
|
||||||
/// <returns></returns>
|
/// <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>
|
/// <summary>
|
||||||
/// 返回仓库类,适用 Insert 方法无须返回插入的数据
|
/// 返回仓库类,适用 Insert 方法无须返回插入的数据
|
||||||
@ -23,8 +26,9 @@ public static class IFreeSqlExtenssions {
|
|||||||
/// <typeparam name="TEntity"></typeparam>
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
/// <param name="that"></param>
|
/// <param name="that"></param>
|
||||||
/// <returns></returns>
|
/// <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>();
|
||||||
}
|
}
|
@ -13,6 +13,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FreeSql.Repository\FreeSql.Repository.csproj" />
|
||||||
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -9,8 +9,55 @@ using Xunit;
|
|||||||
namespace FreeSql.Tests.Sqlite {
|
namespace FreeSql.Tests.Sqlite {
|
||||||
public class SqliteCodeFirstTest {
|
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]
|
[Fact]
|
||||||
public void AddField() {
|
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 sql = g.sqlite.CodeFirst.GetComparisonDDLStatements<TopicAddField>();
|
||||||
|
|
||||||
var id = g.sqlite.Insert<TopicAddField>().AppendData(new TopicAddField { }).ExecuteIdentity();
|
var id = g.sqlite.Insert<TopicAddField>().AppendData(new TopicAddField { }).ExecuteIdentity();
|
||||||
|
@ -34,6 +34,7 @@ IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
|||||||
|
|
||||||
.UseAutoSyncStructure(true) //自动同步实体结构到数据库
|
.UseAutoSyncStructure(true) //自动同步实体结构到数据库
|
||||||
.UseSyncStructureToLower(true) //转小写同步结构
|
.UseSyncStructureToLower(true) //转小写同步结构
|
||||||
|
.UseSyncStructureToUpper(true) //转大写同步结构
|
||||||
|
|
||||||
.UseLazyLoading(true) //延时加载导航属性对象,导航属性需要声明 virtual
|
.UseLazyLoading(true) //延时加载导航属性对象,导航属性需要声明 virtual
|
||||||
.Build();
|
.Build();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user