update demo

This commit is contained in:
2881099 2022-09-18 22:22:26 +08:00
parent b0b7cb204c
commit 903a309c92
10 changed files with 122 additions and 92 deletions

View File

@ -0,0 +1,35 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using FreeSql;
using FreeSql.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace aspnetcore_transaction.Domain
{
public class SongRepository : DefaultRepository<Song, int>
{
public SongRepository(UnitOfWorkManager uowm) : base(uowm?.Orm, uowm) { }
}
[Description("123")]
public class Song
{
/// <summary>
/// 自增
/// </summary>
[Column(IsIdentity = true)]
[Description("自增id")]
public int Id { get; set; }
public string Title { get; set; }
}
public class Detail
{
[Column(IsIdentity = true)]
public int Id { get; set; }
public int SongId { get; set; }
public string Title { get; set; }
}
}

View File

@ -16,7 +16,6 @@ namespace aspnetcore_transaction
{ {
webBuilder.UseStartup<Startup>(); webBuilder.UseStartup<Startup>();
}) })
//.UseServiceProviderFactory(new FreeSql.DynamicProxyServiceProviderFactory())
; ;
} }
} }

View File

@ -1,27 +0,0 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:64513/",
"sslPort": 44328
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"dbcontext_01": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:35351/"
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using aspnetcore_transaction.Domain;
using FreeSql;
using FreeSql.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace aspnetcore_transaction.Services
{
public class SongService
{
BaseRepository<Song> _repoSong;
BaseRepository<Detail> _repoDetail;
SongRepository _repoSong2;
public SongService(BaseRepository<Song> repoSong, BaseRepository<Detail> repoDetail, SongRepository repoSong2)
{
var tb = repoSong.Orm.CodeFirst.GetTableByEntity(typeof(Song));
_repoSong = repoSong;
_repoDetail = repoDetail;
_repoSong2 = repoSong2;
}
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
public void Test1()
{
_repoSong.Insert(new Song());
_repoDetail.Insert(new Detail());
_repoSong2.Insert(new Song());
}
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
public Task Test11()
{
return Task.Delay(TimeSpan.FromSeconds(1)).ContinueWith(t =>
_repoSong.InsertAsync(new Song()));
}
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
public async Task Test2()
{
await _repoSong.InsertAsync(new Song());
await _repoDetail.InsertAsync(new Detail());
await _repoSong2.InsertAsync(new Song());
}
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
public async Task<object> Test3()
{
await _repoSong.InsertAsync(new Song());
await _repoDetail.InsertAsync(new Detail());
await _repoSong2.InsertAsync(new Song());
return "123";
}
}
}

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Text; using System.Text;
using aspnetcore_transaction.Controllers; using aspnetcore_transaction.Controllers;
using FreeSql; using FreeSql;
@ -35,7 +36,11 @@ namespace aspnetcore_transaction
services.AddSingleton<IFreeSql>(Fsql); services.AddSingleton<IFreeSql>(Fsql);
services.AddScoped<UnitOfWorkManager>(); services.AddScoped<UnitOfWorkManager>();
services.AddFreeRepository(null, typeof(Startup).Assembly);
//批量注入
foreach (var repo in typeof(Startup).Assembly.GetTypes()
.Where(a => a.IsAbstract == false && typeof(IBaseRepository).IsAssignableFrom(a)))
services.AddScoped(repo);
services.AddScoped<SongService>(); services.AddScoped<SongService>();
} }

View File

@ -1,54 +0,0 @@
//using FreeSql;
//using Microsoft.AspNetCore.Mvc.Filters;
//using System;
//using System.Collections.Generic;
//using System.Data;
//using System.Text;
//using System.Threading.Tasks;
//namespace FreeSql
//{
// /// <summary>
// /// 使用事务执行,请查看 Program.cs 代码开启动态代理
// /// </summary>
// [AttributeUsage(AttributeTargets.Method)]
// public class TransactionalAttribute : DynamicProxyAttribute, IActionFilter
// {
// public Propagation Propagation { get; set; } = Propagation.Required;
// public IsolationLevel IsolationLevel { get => _IsolationLevelPriv.Value; set => _IsolationLevelPriv = value; }
// IsolationLevel? _IsolationLevelPriv;
// [DynamicProxyFromServices]
//#pragma warning disable IDE0044 // 添加只读修饰符
// UnitOfWorkManager _uowManager;
//#pragma warning restore IDE0044 // 添加只读修饰符
// IUnitOfWork _uow;
// public override Task Before(DynamicProxyBeforeArguments args) => OnBefore(_uowManager);
// public override Task After(DynamicProxyAfterArguments args) => OnAfter(args.Exception);
// //这里是为了 controller
// public void OnActionExecuting(ActionExecutingContext context) => OnBefore(context.HttpContext.RequestServices.GetService(typeof(UnitOfWorkManager)) as UnitOfWorkManager);
// public void OnActionExecuted(ActionExecutedContext context) => OnAfter(context.Exception);
// Task OnBefore(UnitOfWorkManager uowm)
// {
// _uow = uowm.Begin(this.Propagation, this._IsolationLevelPriv);
// return Task.FromResult(false);
// }
// Task OnAfter(Exception ex)
// {
// try
// {
// if (ex == null) _uow.Commit();
// else _uow.Rollback();
// }
// finally
// {
// _uow.Dispose();
// }
// return Task.FromResult(false);
// }
// }
//}

View File

@ -11,15 +11,10 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FreeSql.DbContext" Version="3.2.669" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.669" />
<PackageReference Include="Rougamo.Fody" Version="1.1.1" /> <PackageReference Include="Rougamo.Fody" Version="1.1.1" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" /> <PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
<PackageReference Include="FreeSql.DynamicProxy" Version="1.5.0" />
<PackageReference Include="IdleBus" Version="1.5.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -9,5 +9,10 @@
自增 自增
</summary> </summary>
</member> </member>
<member name="P:aspnetcore_transaction.Domain.Song.Id">
<summary>
自增
</summary>
</member>
</members> </members>
</doc> </doc>

View File

@ -79,6 +79,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "base_entity", "Examples\bas
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.OracleOledb", "Providers\FreeSql.Provider.OracleOledb\FreeSql.Provider.OracleOledb.csproj", "{19D2E22A-B000-46B6-AFC8-60BF01A51C9A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.OracleOledb", "Providers\FreeSql.Provider.OracleOledb\FreeSql.Provider.OracleOledb.csproj", "{19D2E22A-B000-46B6-AFC8-60BF01A51C9A}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "aspnetcore_transaction", "Examples\aspnetcore_transaction\aspnetcore_transaction.csproj", "{28163C3B-B2E6-432D-AAC3-F5F19374BE31}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -425,6 +427,18 @@ Global
{19D2E22A-B000-46B6-AFC8-60BF01A51C9A}.Release|x64.Build.0 = Release|Any CPU {19D2E22A-B000-46B6-AFC8-60BF01A51C9A}.Release|x64.Build.0 = Release|Any CPU
{19D2E22A-B000-46B6-AFC8-60BF01A51C9A}.Release|x86.ActiveCfg = Release|Any CPU {19D2E22A-B000-46B6-AFC8-60BF01A51C9A}.Release|x86.ActiveCfg = Release|Any CPU
{19D2E22A-B000-46B6-AFC8-60BF01A51C9A}.Release|x86.Build.0 = Release|Any CPU {19D2E22A-B000-46B6-AFC8-60BF01A51C9A}.Release|x86.Build.0 = Release|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Debug|x64.ActiveCfg = Debug|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Debug|x64.Build.0 = Debug|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Debug|x86.ActiveCfg = Debug|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Debug|x86.Build.0 = Debug|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Release|Any CPU.Build.0 = Release|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Release|x64.ActiveCfg = Release|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Release|x64.Build.0 = Release|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Release|x86.ActiveCfg = Release|Any CPU
{28163C3B-B2E6-432D-AAC3-F5F19374BE31}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -454,10 +468,11 @@ Global
{D4FEE5C1-6805-4B46-B10B-BE5CC942B883} = {2A381C57-2697-427B-9F10-55DA11FD02E4} {D4FEE5C1-6805-4B46-B10B-BE5CC942B883} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
{9D82A683-2950-4E8A-B078-A422ECA6AAA5} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B} {9D82A683-2950-4E8A-B078-A422ECA6AAA5} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
{19D2E22A-B000-46B6-AFC8-60BF01A51C9A} = {2A381C57-2697-427B-9F10-55DA11FD02E4} {19D2E22A-B000-46B6-AFC8-60BF01A51C9A} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
{28163C3B-B2E6-432D-AAC3-F5F19374BE31} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
RESX_NeutralResourcesLanguage = en-US
RESX_PrefixTranslations = True
SolutionGuid = {089687FA-5D21-40AC-BA8A-AA0D1E1H7F98} SolutionGuid = {089687FA-5D21-40AC-BA8A-AA0D1E1H7F98}
RESX_PrefixTranslations = True
RESX_NeutralResourcesLanguage = en-US
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal