mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
## v0.1.13
- 修改 连接池内部 Ping Timeout 值暂定 5秒; - 优化 初始化时若数据库超时,则放弃预热; - FreeSql.Repository 下增加 ISelect.FromRepository 扩展方法,实现分表的多表查询; - 增加 FreeSql.Repository Autofac 泛型注入,可利用实现全局过滤+分表分库; - 补充 GuidRepository 插入数据时,根据 filter 参数设定进行数据验证;
This commit is contained in:
parent
766fe901d7
commit
428220e754
@ -1,5 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using FreeSql;
|
||||||
using repository_01.Repositorys;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using restful.Entitys;
|
using restful.Entitys;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -8,14 +8,27 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace restful.Controllers {
|
namespace restful.Controllers {
|
||||||
|
|
||||||
|
|
||||||
[Route("restapi/[controller]")]
|
[Route("restapi/[controller]")]
|
||||||
public class SongsController : Controller {
|
public class SongsController : Controller {
|
||||||
|
|
||||||
SongRepository _songRepository;
|
BaseRepository<Song, int> _songRepository;
|
||||||
|
|
||||||
public SongsController(IFreeSql fsql) {
|
public class xxxx {
|
||||||
_songRepository = new SongRepository(fsql);
|
public int Id { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public SongsController(IFreeSql fsql,
|
||||||
|
GuidRepository<Song> repos1,
|
||||||
|
GuidRepository<xxxx> repos2,
|
||||||
|
|
||||||
|
DefaultRepository<Song, int> repos11,
|
||||||
|
DefaultRepository<xxxx, int> repos21,
|
||||||
|
|
||||||
|
BaseRepository<Song> repos3, BaseRepository<Song, int> repos4,
|
||||||
|
IBasicRepository<Song> repos31, IBasicRepository<Song, int> repos41,
|
||||||
|
IReadOnlyRepository<Song> repos311, IReadOnlyRepository<Song, int> repos411
|
||||||
|
) {
|
||||||
|
_songRepository = repos4;
|
||||||
|
|
||||||
//test code
|
//test code
|
||||||
var curd1 = fsql.GetRepository<Song, int>();
|
var curd1 = fsql.GetRepository<Song, int>();
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
"windowsAuthentication": false,
|
"windowsAuthentication": false,
|
||||||
"anonymousAuthentication": true,
|
"anonymousAuthentication": true,
|
||||||
"iisExpress": {
|
"iisExpress": {
|
||||||
"applicationUrl": "http://localhost:54379/",
|
"applicationUrl": "http://localhost:64150/",
|
||||||
"sslPort": 0
|
"sslPort": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -21,7 +21,7 @@
|
|||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
},
|
},
|
||||||
"applicationUrl": "http://localhost:54383/"
|
"applicationUrl": "http://localhost:64154/"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,13 +0,0 @@
|
|||||||
using FreeSql;
|
|
||||||
using restful.Entitys;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace repository_01.Repositorys {
|
|
||||||
public class SongRepository : DefaultRepository<Song, int> {
|
|
||||||
public SongRepository(IFreeSql fsql) : base(fsql, null) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +1,11 @@
|
|||||||
using FreeSql;
|
using Autofac;
|
||||||
|
using Autofac.Extensions.DependencyInjection;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using restful.Entitys;
|
||||||
using Swashbuckle.AspNetCore.Swagger;
|
using Swashbuckle.AspNetCore.Swagger;
|
||||||
using System;
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -23,8 +25,10 @@ namespace repository_01 {
|
|||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
public IFreeSql Fsql { get; }
|
public IFreeSql Fsql { get; }
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services) {
|
public IServiceProvider ConfigureServices(IServiceCollection services) {
|
||||||
|
|
||||||
services.AddSingleton<IFreeSql>(Fsql);
|
services.AddSingleton<IFreeSql>(Fsql);
|
||||||
|
//services.AddTransient(s => s.)
|
||||||
|
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
services.AddSwaggerGen(options => {
|
services.AddSwaggerGen(options => {
|
||||||
@ -34,6 +38,16 @@ namespace repository_01 {
|
|||||||
});
|
});
|
||||||
//options.IncludeXmlComments(xmlPath);
|
//options.IncludeXmlComments(xmlPath);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var builder = new ContainerBuilder();
|
||||||
|
|
||||||
|
builder.RegisterFreeRepository<Song>(a => a.Id == 1);
|
||||||
|
builder.RegisterFreeGuidRepository<Song>(a => a.Id == 1, oldname => $"{oldname}_{DateTime.Now.Year}");
|
||||||
|
|
||||||
|
builder.Populate(services);
|
||||||
|
var container = builder.Build();
|
||||||
|
|
||||||
|
return new AutofacServiceProvider(container);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
|
||||||
|
@ -16,8 +16,4 @@
|
|||||||
<ProjectReference Include="..\..\FreeSql.Repository\FreeSql.Repository.csproj" />
|
<ProjectReference Include="..\..\FreeSql.Repository\FreeSql.Repository.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Properties\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -9,35 +9,54 @@ namespace FreeSql {
|
|||||||
where TEntity : class {
|
where TEntity : class {
|
||||||
|
|
||||||
protected IFreeSql _fsql;
|
protected IFreeSql _fsql;
|
||||||
protected Expression<Func<TEntity, bool>> _filter;
|
|
||||||
protected Func<TEntity, bool> _filterCompile;
|
Expression<Func<TEntity, bool>> _filterVal;
|
||||||
protected Func<string, string> _asTable;
|
protected Expression<Func<TEntity, bool>> Filter {
|
||||||
protected Func<Type, string, string> _asTableSelect => _asTable == null ? null : new Func<Type, string, string>((a, b) => a == _entityType ? _asTable(b) : null);
|
get => _filterVal;
|
||||||
protected Type _entityType = typeof(TEntity);
|
set {
|
||||||
|
_filterVal = value;
|
||||||
|
FilterCompile = value?.Compile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
internal Expression<Func<TEntity, bool>> FilterInternal => Filter;
|
||||||
|
protected Func<TEntity, bool> FilterCompile { get; private set; }
|
||||||
|
internal Func<TEntity, bool> FilterCompileInternal => FilterCompile;
|
||||||
|
|
||||||
|
Func<string, string> _asTableVal;
|
||||||
|
protected Func<string, string> AsTable {
|
||||||
|
get => _asTableVal;
|
||||||
|
set {
|
||||||
|
_asTableVal = value;
|
||||||
|
AsTableSelect = value == null ? null : new Func<Type, string, string>((a, b) => a == EntityType ? value(b) : null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected Func<Type, string, string> AsTableSelect { get; private set; }
|
||||||
|
internal Func<Type, string, string> AsTableSelectInternal => AsTableSelect;
|
||||||
|
|
||||||
|
protected Type EntityType { get; } = typeof(TEntity);
|
||||||
|
|
||||||
protected BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base() {
|
protected BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base() {
|
||||||
_fsql = fsql ?? throw new NullReferenceException("fsql 参数不可为空");
|
_fsql = fsql ?? throw new NullReferenceException("fsql 参数不可为空");
|
||||||
_filter = filter;
|
Filter = filter;
|
||||||
_filterCompile = filter?.Compile();
|
AsTable = asTable;
|
||||||
_asTable = asTable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ISelect<TEntity> Select => _fsql.Select<TEntity>().Where(_filter).AsTable(_asTableSelect);
|
public ISelect<TEntity> Select => _fsql.Select<TEntity>().Where(Filter).AsTable(AsTableSelect);
|
||||||
|
|
||||||
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>().Where(_filter).AsTable(_asTable);
|
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>().Where(Filter).AsTable(AsTable);
|
||||||
|
|
||||||
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).AsTable(_asTable).ExecuteAffrows();
|
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(Filter).Where(predicate).AsTable(AsTable).ExecuteAffrows();
|
||||||
|
|
||||||
public int Delete(TEntity entity) {
|
public int Delete(TEntity entity) {
|
||||||
ValidatorEntityAndThrow(entity);
|
ValidatorEntityAndThrow(entity);
|
||||||
return _fsql.Delete<TEntity>(entity).Where(_filter).AsTable(_asTable).ExecuteAffrows();
|
return _fsql.Delete<TEntity>(entity).Where(Filter).AsTable(AsTable).ExecuteAffrows();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).AsTable(_asTable).ExecuteAffrowsAsync();
|
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(Filter).Where(predicate).AsTable(AsTable).ExecuteAffrowsAsync();
|
||||||
|
|
||||||
public Task<int> DeleteAsync(TEntity entity) {
|
public Task<int> DeleteAsync(TEntity entity) {
|
||||||
ValidatorEntityAndThrow(entity);
|
ValidatorEntityAndThrow(entity);
|
||||||
return _fsql.Delete<TEntity>(entity).Where(_filter).AsTable(_asTable).ExecuteAffrowsAsync();
|
return _fsql.Delete<TEntity>(entity).Where(Filter).AsTable(AsTable).ExecuteAffrowsAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual TEntity Insert(TEntity entity) {
|
public virtual TEntity Insert(TEntity entity) {
|
||||||
@ -45,7 +64,7 @@ namespace FreeSql {
|
|||||||
switch (_fsql.Ado.DataType) {
|
switch (_fsql.Ado.DataType) {
|
||||||
case DataType.SqlServer:
|
case DataType.SqlServer:
|
||||||
case DataType.PostgreSQL:
|
case DataType.PostgreSQL:
|
||||||
return _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteInserted().FirstOrDefault();
|
return _fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteInserted().FirstOrDefault();
|
||||||
case DataType.MySql:
|
case DataType.MySql:
|
||||||
case DataType.Oracle:
|
case DataType.Oracle:
|
||||||
case DataType.Sqlite:
|
case DataType.Sqlite:
|
||||||
@ -59,7 +78,7 @@ namespace FreeSql {
|
|||||||
switch (_fsql.Ado.DataType) {
|
switch (_fsql.Ado.DataType) {
|
||||||
case DataType.SqlServer:
|
case DataType.SqlServer:
|
||||||
case DataType.PostgreSQL:
|
case DataType.PostgreSQL:
|
||||||
return _fsql.Insert<TEntity>().AppendData(entitys).AsTable(_asTable).ExecuteInserted();
|
return _fsql.Insert<TEntity>().AppendData(entitys).AsTable(AsTable).ExecuteInserted();
|
||||||
case DataType.MySql:
|
case DataType.MySql:
|
||||||
case DataType.Oracle:
|
case DataType.Oracle:
|
||||||
case DataType.Sqlite:
|
case DataType.Sqlite:
|
||||||
@ -73,7 +92,7 @@ namespace FreeSql {
|
|||||||
switch (_fsql.Ado.DataType) {
|
switch (_fsql.Ado.DataType) {
|
||||||
case DataType.SqlServer:
|
case DataType.SqlServer:
|
||||||
case DataType.PostgreSQL:
|
case DataType.PostgreSQL:
|
||||||
return (await _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteInsertedAsync()).FirstOrDefault();
|
return (await _fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteInsertedAsync()).FirstOrDefault();
|
||||||
case DataType.MySql:
|
case DataType.MySql:
|
||||||
case DataType.Oracle:
|
case DataType.Oracle:
|
||||||
case DataType.Sqlite:
|
case DataType.Sqlite:
|
||||||
@ -87,7 +106,7 @@ namespace FreeSql {
|
|||||||
switch (_fsql.Ado.DataType) {
|
switch (_fsql.Ado.DataType) {
|
||||||
case DataType.SqlServer:
|
case DataType.SqlServer:
|
||||||
case DataType.PostgreSQL:
|
case DataType.PostgreSQL:
|
||||||
return _fsql.Insert<TEntity>().AppendData(entitys).AsTable(_asTable).ExecuteInsertedAsync();
|
return _fsql.Insert<TEntity>().AppendData(entitys).AsTable(AsTable).ExecuteInsertedAsync();
|
||||||
case DataType.MySql:
|
case DataType.MySql:
|
||||||
case DataType.Oracle:
|
case DataType.Oracle:
|
||||||
case DataType.Sqlite:
|
case DataType.Sqlite:
|
||||||
@ -98,18 +117,18 @@ namespace FreeSql {
|
|||||||
|
|
||||||
public int Update(TEntity entity) {
|
public int Update(TEntity entity) {
|
||||||
ValidatorEntityAndThrow(entity);
|
ValidatorEntityAndThrow(entity);
|
||||||
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).AsTable(_asTable).ExecuteAffrows();
|
return _fsql.Update<TEntity>().SetSource(entity).Where(Filter).AsTable(AsTable).ExecuteAffrows();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<int> UpdateAsync(TEntity entity) {
|
public Task<int> UpdateAsync(TEntity entity) {
|
||||||
ValidatorEntityAndThrow(entity);
|
ValidatorEntityAndThrow(entity);
|
||||||
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).AsTable(_asTable).ExecuteAffrowsAsync();
|
return _fsql.Update<TEntity>().SetSource(entity).Where(Filter).AsTable(AsTable).ExecuteAffrowsAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void ValidatorEntityAndThrow(TEntity entity) => ValidatorEntityAndThrow(new[] { entity });
|
protected void ValidatorEntityAndThrow(TEntity entity) => ValidatorEntityAndThrow(new[] { entity });
|
||||||
protected virtual void ValidatorEntityAndThrow(IEnumerable<TEntity> entitys) {
|
protected virtual void ValidatorEntityAndThrow(IEnumerable<TEntity> entitys) {
|
||||||
foreach (var entity in entitys) {
|
foreach (var entity in entitys) {
|
||||||
if (_filterCompile?.Invoke(entity) == false) throw new Exception($"FreeSql.Repository Insert 失败,因为设置了 {_filter},插入的数据不符合");
|
if (FilterCompile?.Invoke(entity) == false) throw new Exception($"FreeSql.Repository Insert 失败,因为设置了 {Filter},插入的数据不符合");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,13 +139,13 @@ namespace FreeSql {
|
|||||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable) {
|
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).AsTable(_asTable).ExecuteAffrows();
|
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).Where(Filter).AsTable(AsTable).ExecuteAffrows();
|
||||||
|
|
||||||
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).AsTable(_asTable).ExecuteAffrowsAsync();
|
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).Where(Filter).AsTable(AsTable).ExecuteAffrowsAsync();
|
||||||
|
|
||||||
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).AsTable(_asTableSelect).ToOne();
|
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).Where(Filter).AsTable(AsTableSelect).ToOne();
|
||||||
|
|
||||||
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).AsTable(_asTableSelect).ToOneAsync();
|
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).Where(Filter).AsTable(AsTableSelect).ToOneAsync();
|
||||||
|
|
||||||
public TEntity Get(TKey id) => Find(id);
|
public TEntity Get(TKey id) => Find(id);
|
||||||
|
|
||||||
|
@ -9,6 +9,10 @@ namespace FreeSql {
|
|||||||
BaseRepository<TEntity, TKey>
|
BaseRepository<TEntity, TKey>
|
||||||
where TEntity : class {
|
where TEntity : class {
|
||||||
|
|
||||||
|
public DefaultRepository(IFreeSql fsql) : base(fsql, null, null) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter, null) {
|
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter, null) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
82
FreeSql.Repository/DependencyInjection.cs
Normal file
82
FreeSql.Repository/DependencyInjection.cs
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
using Autofac;
|
||||||
|
using FreeSql;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
public static class FreeSqlRepositoryAutofacDependencyInjection {
|
||||||
|
|
||||||
|
public static void RegisterFreeGuidRepository<TEntity>(this ContainerBuilder builder, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable) =>
|
||||||
|
builder.RegisterRepository<TEntity>(filter, asTable, 1);
|
||||||
|
public static void RegisterFreeRepository<TEntity>(this ContainerBuilder builder, Expression<Func<TEntity, bool>> filter) =>
|
||||||
|
builder.RegisterRepository<TEntity>(filter, null, 2);
|
||||||
|
|
||||||
|
static void RegisterRepository<TEntity>(this ContainerBuilder builder, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable, int regType) {
|
||||||
|
|
||||||
|
Func<Type, LambdaExpression> reposFunc = type => {
|
||||||
|
var entityType = type.GenericTypeArguments[0];
|
||||||
|
var filterParameter1 = Expression.Parameter(entityType, filter.Parameters[0].Name);
|
||||||
|
var convertFilter = Expression.Lambda(
|
||||||
|
typeof(Func<,>).MakeGenericType(entityType, typeof(bool)),
|
||||||
|
new ReplaceVisitor<TEntity>().Modify(filter.Body, filterParameter1),
|
||||||
|
filterParameter1
|
||||||
|
);
|
||||||
|
var repos = Expression.Parameter(type);
|
||||||
|
var blocks = new List<Expression>();
|
||||||
|
if (filter != null) blocks.Add(Expression.Call(repos, type.GetMethod("set_Filter", BindingFlags.Instance | BindingFlags.NonPublic), Expression.Constant(convertFilter)));
|
||||||
|
if (asTable != null) blocks.Add(Expression.Call(repos, type.GetMethod("set_AsTable", BindingFlags.Instance | BindingFlags.NonPublic), Expression.Constant(asTable)));
|
||||||
|
return Expression.Lambda(
|
||||||
|
//Expression.New(
|
||||||
|
// typeof(GuidRepository<>).MakeGenericType(type.GenericTypeArguments).GetConstructors()[1],
|
||||||
|
// Expression.Constant(a.Context.Resolve<IFreeSql>()),
|
||||||
|
// Expression.Constant(convertFilter),
|
||||||
|
// Expression.Constant(asTable)
|
||||||
|
//)
|
||||||
|
Expression.Block(blocks),
|
||||||
|
repos
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (regType == 1)
|
||||||
|
builder.RegisterGeneric(typeof(GuidRepository<>)).As(
|
||||||
|
typeof(GuidRepository<>),
|
||||||
|
typeof(BaseRepository<>), typeof(BaseRepository<,>),
|
||||||
|
typeof(IBasicRepository<>), typeof(IBasicRepository<,>),
|
||||||
|
typeof(IReadOnlyRepository<>), typeof(IReadOnlyRepository<,>)
|
||||||
|
).OnActivating(a => {
|
||||||
|
if (filter != null)
|
||||||
|
_dicAddGuidRepositoryFunc.GetOrAdd(a.Instance.GetType(), t => reposFunc(t).Compile()).DynamicInvoke(a.Instance);
|
||||||
|
}).InstancePerDependency();
|
||||||
|
|
||||||
|
|
||||||
|
if (regType == 2)
|
||||||
|
builder.RegisterGeneric(typeof(DefaultRepository<,>)).As(
|
||||||
|
typeof(DefaultRepository<,>),
|
||||||
|
typeof(BaseRepository<,>),
|
||||||
|
typeof(IBasicRepository<,>),
|
||||||
|
typeof(IReadOnlyRepository<,>)
|
||||||
|
).OnActivating(a => {
|
||||||
|
if (filter != null)
|
||||||
|
_dicAddGuidRepositoryFunc.GetOrAdd(a.Instance.GetType(), t => reposFunc(t).Compile()).DynamicInvoke(a.Instance);
|
||||||
|
}).InstancePerDependency();
|
||||||
|
}
|
||||||
|
|
||||||
|
static ConcurrentDictionary<Type, Delegate> _dicAddGuidRepositoryFunc = new ConcurrentDictionary<Type, Delegate>();
|
||||||
|
|
||||||
|
class ReplaceVisitor<TEntity1> : ExpressionVisitor {
|
||||||
|
private ParameterExpression parameter;
|
||||||
|
|
||||||
|
public Expression Modify(Expression expression, ParameterExpression parameter) {
|
||||||
|
this.parameter = parameter;
|
||||||
|
return Visit(expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Expression VisitMember(MemberExpression node) {
|
||||||
|
if (node.Expression?.NodeType == ExpressionType.Parameter)
|
||||||
|
return Expression.Property(parameter, node.Member.Name);
|
||||||
|
return base.VisitMember(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<Version>0.1.12</Version>
|
<Version>0.1.13</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>
|
||||||
@ -10,6 +10,10 @@
|
|||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.4.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -10,26 +10,33 @@ namespace FreeSql {
|
|||||||
BaseRepository<TEntity, Guid>
|
BaseRepository<TEntity, Guid>
|
||||||
where TEntity : class {
|
where TEntity : class {
|
||||||
|
|
||||||
|
public GuidRepository(IFreeSql fsql) : this(fsql, null, null) {
|
||||||
|
|
||||||
|
}
|
||||||
public GuidRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable) : base(fsql, filter, asTable) {
|
public GuidRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable) : base(fsql, filter, asTable) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public override List<TEntity> Insert(IEnumerable<TEntity> entity) {
|
public override List<TEntity> Insert(IEnumerable<TEntity> entity) {
|
||||||
_fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrows();
|
base.ValidatorEntityAndThrow(entity);
|
||||||
|
_fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteAffrows();
|
||||||
return entity.ToList();
|
return entity.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
async public override Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entity) {
|
async public override Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entity) {
|
||||||
await _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrowsAsync();
|
base.ValidatorEntityAndThrow(entity);
|
||||||
|
await _fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteAffrowsAsync();
|
||||||
return entity.ToList();
|
return entity.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override TEntity Insert(TEntity entity) {
|
public override TEntity Insert(TEntity entity) {
|
||||||
_fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrows();
|
base.ValidatorEntityAndThrow(entity);
|
||||||
|
_fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteAffrows();
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
async public override Task<TEntity> InsertAsync(TEntity entity) {
|
async public override Task<TEntity> InsertAsync(TEntity entity) {
|
||||||
await _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrowsAsync();
|
base.ValidatorEntityAndThrow(entity);
|
||||||
|
await _fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteAffrowsAsync();
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,4 +42,16 @@ public static class IFreeSqlExtenssions {
|
|||||||
.GetOrAdd(typeof(TEntity), key1 => new GuidRepository<TEntity>(that, null, null)) as GuidRepository<TEntity>;
|
.GetOrAdd(typeof(TEntity), key1 => new GuidRepository<TEntity>(that, null, null)) as GuidRepository<TEntity>;
|
||||||
}
|
}
|
||||||
static ConcurrentDictionary<Type, IRepository> dicGetGuidRepository = new ConcurrentDictionary<Type, IRepository>();
|
static ConcurrentDictionary<Type, IRepository> dicGetGuidRepository = new ConcurrentDictionary<Type, IRepository>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 合并两个仓储的设置,以便查询
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <typeparam name="T2"></typeparam>
|
||||||
|
/// <param name="that"></param>
|
||||||
|
/// <param name="repos"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static ISelect<TEntity> FromRepository<TEntity, T2>(this ISelect<TEntity> that, BaseRepository<T2> repos) where TEntity : class where T2 : class {
|
||||||
|
return that.AsTable(repos.AsTableSelectInternal).Where<T2>(repos.FilterInternal);
|
||||||
|
}
|
||||||
}
|
}
|
@ -548,6 +548,24 @@ namespace FreeSql.Tests.Sqlite {
|
|||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void AsTable() {
|
public void AsTable() {
|
||||||
|
|
||||||
|
var tenantId = 1;
|
||||||
|
var reposTopic = g.sqlite.GetGuidRepository<Topic>(null, oldname => $"{oldname}_{tenantId}");
|
||||||
|
var reposType = g.sqlite.GetGuidRepository<TestTypeInfo>(null, oldname => $"{oldname}_{tenantId}");
|
||||||
|
|
||||||
|
//reposTopic.Delete(Guid.Empty);
|
||||||
|
//reposTopic.Find(Guid.Empty);
|
||||||
|
//reposTopic.Update(new Topic { TestTypeInfoGuid = 1 });
|
||||||
|
var sql11 = reposTopic.Select
|
||||||
|
|
||||||
|
.FromRepository(reposType)
|
||||||
|
.From<TestTypeInfo, TestTypeParentInfo>((s,b,c) => s)
|
||||||
|
|
||||||
|
.LeftJoin(a => a.TestTypeInfoGuid == a.Type.Guid)
|
||||||
|
.ToSql();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Func<Type, string, string> tableRule = (type, oldname) => {
|
Func<Type, string, string> tableRule = (type, oldname) => {
|
||||||
if (type == typeof(Topic)) return oldname + "AsTable1";
|
if (type == typeof(Topic)) return oldname + "AsTable1";
|
||||||
else if (type == typeof(TestTypeInfo)) return oldname + "AsTable2";
|
else if (type == typeof(TestTypeInfo)) return oldname + "AsTable2";
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<Version>0.1.12</Version>
|
<Version>0.1.13</Version>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Authors>YeXiangQin</Authors>
|
<Authors>YeXiangQin</Authors>
|
||||||
<Description>打造 .NETCore 最方便的 ORM,DbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite,读写分离、分表分库。</Description>
|
<Description>打造 .NETCore 最方便的 ORM,DbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite,读写分离、分表分库。</Description>
|
||||||
|
@ -196,6 +196,13 @@ namespace FreeSql {
|
|||||||
/// 多表条件查询
|
/// 多表条件查询
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T2"></typeparam>
|
/// <typeparam name="T2"></typeparam>
|
||||||
|
/// <param name="exp">lambda表达式</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
ISelect<T1> Where<T2>(Expression<Func<T2, bool>> exp) where T2 : class;
|
||||||
|
/// <summary>
|
||||||
|
/// 多表条件查询
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T2"></typeparam>
|
||||||
/// <typeparam name="T3"></typeparam>
|
/// <typeparam name="T3"></typeparam>
|
||||||
/// <param name="exp">lambda表达式</param>
|
/// <param name="exp">lambda表达式</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
@ -108,6 +108,7 @@ namespace FreeSql.Internal.CommonProvider {
|
|||||||
public ISelect<T1> Where(Expression<Func<T1, bool>> exp) => this.InternalWhere(exp?.Body);
|
public ISelect<T1> Where(Expression<Func<T1, bool>> exp) => this.InternalWhere(exp?.Body);
|
||||||
|
|
||||||
public ISelect<T1> Where<T2>(Expression<Func<T1, T2, bool>> exp) where T2 : class => this.InternalWhere(exp?.Body);
|
public ISelect<T1> Where<T2>(Expression<Func<T1, T2, bool>> exp) where T2 : class => this.InternalWhere(exp?.Body);
|
||||||
|
public ISelect<T1> Where<T2>(Expression<Func<T2, bool>> exp) where T2 : class => this.InternalWhere(exp?.Body);
|
||||||
|
|
||||||
public ISelect<T1> Where<T2, T3>(Expression<Func<T1, T2, T3, bool>> exp) where T2 : class where T3 : class => this.InternalWhere(exp?.Body);
|
public ISelect<T1> Where<T2, T3>(Expression<Func<T1, T2, T3, bool>> exp) where T2 : class where T3 : class => this.InternalWhere(exp?.Body);
|
||||||
|
|
||||||
|
@ -141,7 +141,7 @@ namespace FreeSql.MySql {
|
|||||||
|
|
||||||
static DbCommand PingCommand(DbConnection conn) {
|
static DbCommand PingCommand(DbConnection conn) {
|
||||||
var cmd = conn.CreateCommand();
|
var cmd = conn.CreateCommand();
|
||||||
cmd.CommandTimeout = 1;
|
cmd.CommandTimeout = 5;
|
||||||
cmd.CommandText = "select 1";
|
cmd.CommandText = "select 1";
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
@ -155,7 +155,7 @@ namespace FreeSql.Oracle {
|
|||||||
|
|
||||||
static DbCommand PingCommand(DbConnection conn) {
|
static DbCommand PingCommand(DbConnection conn) {
|
||||||
var cmd = conn.CreateCommand();
|
var cmd = conn.CreateCommand();
|
||||||
cmd.CommandTimeout = 1;
|
cmd.CommandTimeout = 5;
|
||||||
cmd.CommandText = "select 1 from dual";
|
cmd.CommandText = "select 1 from dual";
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
@ -150,7 +150,7 @@ namespace FreeSql.PostgreSQL {
|
|||||||
|
|
||||||
static DbCommand PingCommand(DbConnection conn) {
|
static DbCommand PingCommand(DbConnection conn) {
|
||||||
var cmd = conn.CreateCommand();
|
var cmd = conn.CreateCommand();
|
||||||
cmd.CommandTimeout = 1;
|
cmd.CommandTimeout = 5;
|
||||||
cmd.CommandText = "select 1";
|
cmd.CommandText = "select 1";
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ namespace FreeSql.SqlServer {
|
|||||||
|
|
||||||
static DbCommand PingCommand(DbConnection conn) {
|
static DbCommand PingCommand(DbConnection conn) {
|
||||||
var cmd = conn.CreateCommand();
|
var cmd = conn.CreateCommand();
|
||||||
cmd.CommandTimeout = 1;
|
cmd.CommandTimeout = 5;
|
||||||
cmd.CommandText = "select 1";
|
cmd.CommandText = "select 1";
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
@ -151,7 +151,7 @@ namespace FreeSql.Sqlite {
|
|||||||
|
|
||||||
static DbCommand PingCommand(DbConnection conn) {
|
static DbCommand PingCommand(DbConnection conn) {
|
||||||
var cmd = conn.CreateCommand();
|
var cmd = conn.CreateCommand();
|
||||||
cmd.CommandTimeout = 1;
|
cmd.CommandTimeout = 5;
|
||||||
cmd.CommandText = "select 1";
|
cmd.CommandText = "select 1";
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ FreeSql是一个功能强大的NETStandard库,用于对象关系映射程序(O
|
|||||||
- [x] 支持丰富的表达式函数;
|
- [x] 支持丰富的表达式函数;
|
||||||
- [x] 支持导航属性查询,和延时加载;
|
- [x] 支持导航属性查询,和延时加载;
|
||||||
- [x] 支持同步/异步数据库操作方法,丰富多彩的链式查询方法;
|
- [x] 支持同步/异步数据库操作方法,丰富多彩的链式查询方法;
|
||||||
- [x] 支持读写分离、分表分库;
|
- [x] 支持读写分离、分表分库,租户设计;
|
||||||
- [x] 支持多种数据库,MySql/SqlServer/PostgreSQL/Oracle/Sqlite;
|
- [x] 支持多种数据库,MySql/SqlServer/PostgreSQL/Oracle/Sqlite;
|
||||||
|
|
||||||
[《Select查询数据文档》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2) | [《Update更新数据文档》](https://github.com/2881099/FreeSql/wiki/%e4%bf%ae%e6%94%b9) | [《Insert插入数据文档》](https://github.com/2881099/FreeSql/wiki/%e6%b7%bb%e5%8a%a0) | [《Delete删除数据文档》](https://github.com/2881099/FreeSql/wiki/%e5%88%a0%e9%99%a4)
|
[《Select查询数据文档》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2) | [《Update更新数据文档》](https://github.com/2881099/FreeSql/wiki/%e4%bf%ae%e6%94%b9) | [《Insert插入数据文档》](https://github.com/2881099/FreeSql/wiki/%e6%b7%bb%e5%8a%a0) | [《Delete删除数据文档》](https://github.com/2881099/FreeSql/wiki/%e5%88%a0%e9%99%a4)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user