Skip to content

Commit

Permalink
v1.9.0 - 移除 .net45 及以下 .Change API;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jul 5, 2024
1 parent 015b1ea commit 1cfc24a
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 73 deletions.
7 changes: 7 additions & 0 deletions FreeSql.Cloud.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "netcore31_tcc_saga", "examp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "net60_webapi", "examples\net60_webapi\net60_webapi.csproj", "{10029FBE-B53F-4E08-9C8B-0C15545A4DB9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp45", "examples\ConsoleApp45\ConsoleApp45.csproj", "{D6601BFC-DD3A-42EC-AC97-216A1BA34A14}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -46,6 +48,10 @@ Global
{10029FBE-B53F-4E08-9C8B-0C15545A4DB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{10029FBE-B53F-4E08-9C8B-0C15545A4DB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{10029FBE-B53F-4E08-9C8B-0C15545A4DB9}.Release|Any CPU.Build.0 = Release|Any CPU
{D6601BFC-DD3A-42EC-AC97-216A1BA34A14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6601BFC-DD3A-42EC-AC97-216A1BA34A14}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6601BFC-DD3A-42EC-AC97-216A1BA34A14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6601BFC-DD3A-42EC-AC97-216A1BA34A14}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -55,6 +61,7 @@ Global
{5DE53692-4AE5-4DA5-B2ED-2FA9AEA06AA0} = {12598115-AC36-430B-98D7-3C27D29201D7}
{82D6631D-A5D1-4AA6-B326-13376B6CB7D4} = {12598115-AC36-430B-98D7-3C27D29201D7}
{10029FBE-B53F-4E08-9C8B-0C15545A4DB9} = {12598115-AC36-430B-98D7-3C27D29201D7}
{D6601BFC-DD3A-42EC-AC97-216A1BA34A14} = {12598115-AC36-430B-98D7-3C27D29201D7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {932A1C6F-1A3C-4545-BB25-76B228D02D85}
Expand Down
6 changes: 6 additions & 0 deletions examples/ConsoleApp45/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>
58 changes: 58 additions & 0 deletions examples/ConsoleApp45/ConsoleApp45.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D6601BFC-DD3A-42EC-AC97-216A1BA34A14}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ConsoleApp45</RootNamespace>
<AssemblyName>ConsoleApp45</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FreeSql.Cloud">
<Version>1.8.3</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
17 changes: 17 additions & 0 deletions examples/ConsoleApp45/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp45
{
internal class Program
{
static void Main(string[] args)
{

}
}
}
36 changes: 36 additions & 0 deletions examples/ConsoleApp45/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("ConsoleApp45")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ConsoleApp45")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("d6601bfc-dd3a-42ec-ac97-216a1ba34a14")]

// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
16 changes: 11 additions & 5 deletions src/FreeSql.Cloud/Abstract/FreeSqlCloudBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ namespace FreeSql.Cloud.Abstract
{
public abstract class FreeSqlCloudBase
{
#if !net40
internal abstract string GetDBKey();
public abstract IFreeSql Use(DBKeyString dbkey);
public abstract IFreeSql Change(DBKeyString dbkey);
#endif
public abstract IFreeSql Use(DBKeyString dbkey);
}

public class DBKeyString
Expand Down Expand Up @@ -56,11 +58,15 @@ public void SetValue(T value)
_rawValueChanged = true;
_rawValue = value;
}
}
#if net40
ThreadLocal<ValueHolder> _asyncLocal = new ThreadLocal<ValueHolder>();
#else
}
#if !net40
AsyncLocal<ValueHolder> _asyncLocal = new AsyncLocal<ValueHolder>();
#else
AsyncLocalFake<ValueHolder> _asyncLocal = new AsyncLocalFake<ValueHolder>();
class AsyncLocalFake<T2>
{
public T2 Value { get; set; }
}
#endif
}
}
4 changes: 2 additions & 2 deletions src/FreeSql.Cloud/FreeSql.Cloud.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard20;net40</TargetFrameworks>
<Version>1.8.3</Version>
<TargetFrameworks>netstandard20;net461;net40</TargetFrameworks>
<Version>1.9.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
<Description>提供跨数据库访问,分布式事务TCC、SAGA解决方案,支持 .NET Core 2.1+, .NET Framework 4.0+.</Description>
Expand Down
9 changes: 7 additions & 2 deletions src/FreeSql.Cloud/FreeSqlCloud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ namespace FreeSql
//public class FreeSqlCloud : FreeSqlCloud<string> { }
public partial class FreeSqlCloud<TDBKey> : FreeSqlCloudBase, IFreeSql
{
#if !net40
internal override string GetDBKey() => _dbkey.ToInvariantCultureToString();
public override IFreeSql Use(DBKeyString dbkey) => Use((TDBKey)typeof(TDBKey).FromObject(dbkey?.ToString()));
public override IFreeSql Change(DBKeyString dbkey) => Change((TDBKey)typeof(TDBKey).FromObject(dbkey?.ToString()));
#endif
public override IFreeSql Use(DBKeyString dbkey) => Use((TDBKey)typeof(TDBKey).FromObject(dbkey?.ToString()));

public string DistributeKey { get; }
public Action<string> DistributeTrace;
Expand Down Expand Up @@ -79,13 +81,15 @@ public FreeSqlCloud(string distributeKey)
if (string.IsNullOrWhiteSpace(DistributeKey)) DistributeKey = null;
_ib = new IdleBus<TDBKey, IFreeSql>(TimeSpan.FromMinutes(3));
_ib.Notice += (_, __) => { };

_dbkeyCurrent = new AsyncLocalAccessor<TDBKey>(() =>
{
if (typeof(TDBKey) == typeof(string) && _dbkeyMaster == null) return (TDBKey)typeof(TDBKey).FromObject("");
return _dbkeyMaster;
});
}
}

#if !net40
/// <summary>
/// 切换数据库(同一线程,或异步await 后续操作有效)<para></para>
/// 注意:单次有效请使用 Use(dbkey)
Expand All @@ -99,6 +103,7 @@ public IFreeSql Change(TDBKey dbkey)
_dbkeyCurrent.Value = dbkey;
return new FreeSqlCloundSnapshot<TDBKey>(this, dbkey, () => _dbkeyCurrent.Value = oldkey);
}
#endif
/// <summary>
/// 临时使用数据库(单次有效)
/// </summary>
Expand Down
18 changes: 0 additions & 18 deletions src/FreeSql.Cloud/FreesqlCloudGlobalExtensions.cs

This file was deleted.

108 changes: 62 additions & 46 deletions src/FreeSql.Cloud/RepositoryCloud/RepositoryCloud.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
using FreeSql.Cloud.Abstract;
using FreeSql;
using FreeSql.Cloud.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

#if !net40
public static class FreesqlCloudGlobalExtensions
{
/// <summary>
/// 创建特殊仓储对象,实现随时跟随 FreeSqlCloud Change 方法切换到对应的数据库<para></para>
/// _<para></para>
/// 区别说明:其他方式创建的仓储对象,初始化已经固定 IFreeSql(无法跟随切换)
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="that"></param>
/// <returns></returns>
public static IBaseRepository<TEntity> GetCloudRepository<TEntity>(this FreeSqlCloudBase that) where TEntity : class
{
return new RepositoryCloud<TEntity>(that);
}
}
namespace FreeSql
{
/// <summary>
Expand Down Expand Up @@ -141,8 +158,7 @@ public IUnitOfWork UnitOfWork
public int Delete(Expression<Func<TEntity, bool>> predicate) => CurrentRepository.Delete(predicate);
public List<object> DeleteCascadeByDatabase(Expression<Func<TEntity, bool>> predicate) => CurrentRepository.DeleteCascadeByDatabase(predicate);

#if net40
#else

public Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default) => CurrentRepository.InsertAsync(entity, cancellationToken);
public Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default) => CurrentRepository.InsertAsync(entitys, cancellationToken);
public Task<TEntity> InsertOrUpdateAsync(TEntity entity, CancellationToken cancellationToken = default) => CurrentRepository.InsertOrUpdateAsync(entity, cancellationToken);
Expand All @@ -155,48 +171,48 @@ public IUnitOfWork UnitOfWork
public Task<int> DeleteAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default) => CurrentRepository.DeleteAsync(entitys, cancellationToken);
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) => CurrentRepository.DeleteAsync(predicate, cancellationToken);
public Task<List<object>> DeleteCascadeByDatabaseAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) => CurrentRepository.DeleteCascadeByDatabaseAsync(predicate, cancellationToken);
#endif
}

// class RepositoryCloud<TDBKey, TEntity, TKey> : RepositoryCloud<TDBKey, TEntity>, IBaseRepository<TEntity, TKey>
// where TEntity : class
// {
// /// <summary>
// /// 跟随 cloud.Change 切换
// /// </summary>
// /// <param name="cloud"></param>
// public RepositoryCloud(FreeSqlCloud<TDBKey> cloud) : base(cloud) { }
// /// <summary>
// /// 跟随 uowManager.Begin 工作单元切换(优先)<para></para>
// /// 或者<para></para>
// /// 跟随 cloud.Change 切换
// /// </summary>
// /// <param name="cloud"></param>
// /// <param name="uowManager"></param>
// public RepositoryCloud(FreeSqlCloud<TDBKey> cloud, UnitOfWorkManagerCloud<TDBKey> uowManager) : base(cloud, uowManager) { }

// TEntity CheckTKeyAndReturnIdEntity(TKey id)
// {
// var repo = CurrentRepository;
// var tb = repo.Orm.CodeFirst.GetTableByEntity(repo.EntityType);
// if (tb.Primarys.Length != 1) throw new Exception(DbContextStrings.EntityType_PrimaryKeyIsNotOne(repo.EntityType.Name));
// if (tb.Primarys[0].CsType.NullableTypeOrThis() != typeof(TKey).NullableTypeOrThis()) throw new Exception(DbContextStrings.EntityType_PrimaryKeyError(repo.EntityType.Name, typeof(TKey).FullName));
// var obj = tb.Type.CreateInstanceGetDefaultValue();
// repo.Orm.SetEntityValueWithPropertyName(tb.Type, obj, tb.Primarys[0].CsName, id);
// var ret = obj as TEntity;
// if (ret == null) throw new Exception(DbContextStrings.EntityType_CannotConvert(repo.EntityType.Name, typeof(TEntity).Name));
// return ret;
// }

// public virtual TEntity Get(TKey id) => Select.WhereDynamic(CheckTKeyAndReturnIdEntity(id)).ToOne();
// public virtual TEntity Find(TKey id) => Select.WhereDynamic(CheckTKeyAndReturnIdEntity(id)).ToOne();
// public virtual int Delete(TKey id) => Delete(CheckTKeyAndReturnIdEntity(id));

//#if net40
//#else
// public Task<TEntity> GetAsync(TKey id, CancellationToken cancellationToken = default) => Select.WhereDynamic(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken);
// public Task<TEntity> FindAsync(TKey id, CancellationToken cancellationToken = default) => Select.WhereDynamic(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken);
// public Task<int> DeleteAsync(TKey id, CancellationToken cancellationToken = default) => DeleteAsync(CheckTKeyAndReturnIdEntity(id), cancellationToken);
//#endif
// }
// class RepositoryCloud<TDBKey, TEntity, TKey> : RepositoryCloud<TDBKey, TEntity>, IBaseRepository<TEntity, TKey>
// where TEntity : class
// {
// /// <summary>
// /// 跟随 cloud.Change 切换
// /// </summary>
// /// <param name="cloud"></param>
// public RepositoryCloud(FreeSqlCloud<TDBKey> cloud) : base(cloud) { }
// /// <summary>
// /// 跟随 uowManager.Begin 工作单元切换(优先)<para></para>
// /// 或者<para></para>
// /// 跟随 cloud.Change 切换
// /// </summary>
// /// <param name="cloud"></param>
// /// <param name="uowManager"></param>
// public RepositoryCloud(FreeSqlCloud<TDBKey> cloud, UnitOfWorkManagerCloud<TDBKey> uowManager) : base(cloud, uowManager) { }

// TEntity CheckTKeyAndReturnIdEntity(TKey id)
// {
// var repo = CurrentRepository;
// var tb = repo.Orm.CodeFirst.GetTableByEntity(repo.EntityType);
// if (tb.Primarys.Length != 1) throw new Exception(DbContextStrings.EntityType_PrimaryKeyIsNotOne(repo.EntityType.Name));
// if (tb.Primarys[0].CsType.NullableTypeOrThis() != typeof(TKey).NullableTypeOrThis()) throw new Exception(DbContextStrings.EntityType_PrimaryKeyError(repo.EntityType.Name, typeof(TKey).FullName));
// var obj = tb.Type.CreateInstanceGetDefaultValue();
// repo.Orm.SetEntityValueWithPropertyName(tb.Type, obj, tb.Primarys[0].CsName, id);
// var ret = obj as TEntity;
// if (ret == null) throw new Exception(DbContextStrings.EntityType_CannotConvert(repo.EntityType.Name, typeof(TEntity).Name));
// return ret;
// }

// public virtual TEntity Get(TKey id) => Select.WhereDynamic(CheckTKeyAndReturnIdEntity(id)).ToOne();
// public virtual TEntity Find(TKey id) => Select.WhereDynamic(CheckTKeyAndReturnIdEntity(id)).ToOne();
// public virtual int Delete(TKey id) => Delete(CheckTKeyAndReturnIdEntity(id));

//#if net40
//#else
// public Task<TEntity> GetAsync(TKey id, CancellationToken cancellationToken = default) => Select.WhereDynamic(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken);
// public Task<TEntity> FindAsync(TKey id, CancellationToken cancellationToken = default) => Select.WhereDynamic(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken);
// public Task<int> DeleteAsync(TKey id, CancellationToken cancellationToken = default) => DeleteAsync(CheckTKeyAndReturnIdEntity(id), cancellationToken);
//#endif
// }
}

#endif
2 changes: 2 additions & 0 deletions src/FreeSql.Cloud/RepositoryCloud/UnitOfWorkManagerCloud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace FreeSql
{
#if !net40
class UnitOfWorkManagerCloud
{
public FreeSqlCloudBase Cloud { get; }
Expand Down Expand Up @@ -44,4 +45,5 @@ public UnitOfWorkManager GetUnitOfWorkManager(string dbkey)
return uowm;
}
}
#endif
}

0 comments on commit 1cfc24a

Please sign in to comment.