## v0.3.19

- 兼容 GetTableByEntity 有可能因为传入数组类型的错误;
- 修复 UnitOfWork 事务创建逻辑 bug;
- 增加 FreeSql.DbContext 扩展包;
- 调整 UnitOfWork、DbContext 不提交时默认会回滚;
This commit is contained in:
28810
2019-03-20 11:47:04 +08:00
parent 1dccf99bdb
commit 3fd971b78b
21 changed files with 488 additions and 15 deletions

View File

@ -0,0 +1,94 @@
using SafeObjectPool;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Data.Common;
using System.Linq;
using System.Reflection;
namespace FreeSql {
public abstract class DbContext : IDisposable {
internal IFreeSql _orm;
internal IFreeSql _fsql => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
Object<DbConnection> _conn;
DbTransaction _tran;
static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
protected DbContext() {
var builder = new DbContextOptionsBuilder();
OnConfiguring(builder);
_orm = builder._fsql;
var props = _dicGetDbSetProps.GetOrAdd(this.GetType(), tp =>
tp.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
.Where(a => a.PropertyType.IsGenericType &&
a.PropertyType == typeof(DbSet<>).MakeGenericType(a.PropertyType.GenericTypeArguments[0])).ToArray());
foreach (var prop in props) {
var set = this.Set(prop.PropertyType.GenericTypeArguments[0]);
prop.SetValue(this, set);
AllSets.Add(prop, set);
}
}
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
}
public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
public object Set(Type entityType) => Activator.CreateInstance(typeof(BaseDbSet<>).MakeGenericType(entityType), this);
protected Dictionary<PropertyInfo, object> AllSets => new Dictionary<PropertyInfo, object>();
public void SaveChanges() {
Commit();
}
void ReturnObject() {
_fsql.Ado.MasterPool.Return(_conn);
_tran = null;
_conn = null;
}
internal DbTransaction GetOrBeginTransaction(bool isCreate = true) {
if (_tran != null) return _tran;
if (isCreate == false) return null;
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
_conn = _fsql.Ado.MasterPool.Get();
try {
_tran = _conn.Value.BeginTransaction();
} catch {
ReturnObject();
throw;
}
return _tran;
}
void Commit() {
if (_tran != null) {
try {
_tran.Commit();
} finally {
ReturnObject();
}
}
}
void Rollback() {
if (_tran != null) {
try {
_tran.Rollback();
} finally {
ReturnObject();
}
}
}
public void Dispose() {
this.Rollback();
}
}
}

View File

@ -0,0 +1,17 @@
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
namespace FreeSql {
public class DbContextOptionsBuilder {
internal IFreeSql _fsql;
public DbContextOptionsBuilder UseFreeSql(IFreeSql orm) {
_fsql = orm;
return this;
}
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace FreeSql {
public abstract class DbSet<TEntity> where TEntity : class {
protected DbContext _ctx;
public ISelect<TEntity> Select => _ctx._fsql.Select<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction(false));
public IInsert<TEntity> Insert(TEntity source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
public IInsert<TEntity> Insert(TEntity[] source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
public IInsert<TEntity> Insert(IEnumerable<TEntity> source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
public IUpdate<TEntity> Update => _ctx._fsql.Update<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction());
public IDelete<TEntity> Delete => _ctx._fsql.Delete<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction());
}
internal class BaseDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
public BaseDbSet(DbContext ctx) {
_ctx = ctx;
}
}
}

View File

@ -0,0 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
namespace FreeSql {
public static class DbContextDependencyInjection {
public static IServiceCollection AddFreeDbContext<TDbContext>(this IServiceCollection services, Action<DbContextOptionsBuilder> options) where TDbContext : DbContext {
services.AddScoped<TDbContext>(sp => {
var ctx = Activator.CreateInstance<TDbContext>();
if (ctx._orm == null) {
var builder = new DbContextOptionsBuilder();
options(builder);
ctx._orm = builder._fsql;
}
return ctx;
});
return services;
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.3.19</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
<PackageTags>FreeSql ORM</PackageTags>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
</ItemGroup>
</Project>