1- using FreeSql ;
1+ using System ;
2+ using System . ComponentModel ;
3+ using System . Threading . Tasks ;
4+ using FreeSql ;
25using FreeSql . DataAnnotations ;
36using Microsoft . AspNetCore . Mvc ;
4- using Microsoft . Extensions . DependencyInjection ;
57using Microsoft . Extensions . Logging ;
6- using System . ComponentModel ;
7- using System . Linq ;
8- using System . Reflection ;
9- using System . Threading ;
10- using System . Threading . Tasks ;
118
129namespace aspnetcore_transaction . Controllers
1310{
@@ -24,24 +21,25 @@ public HomeController(ILogger<HomeController> logger)
2421
2522 [ HttpGet ( "1" ) ]
2623 //[Transactional]
27- virtual public object Get ( [ FromServices ] BaseRepository < Song > repoSong , [ FromServices ] BaseRepository < Detail > repoDetail , [ FromServices ] SongRepository repoSong2 ,
24+ public async Task < object > Get ( [ FromServices ] BaseRepository < Song > repoSong , [ FromServices ] BaseRepository < Detail > repoDetail , [ FromServices ] SongRepository repoSong2 ,
2825 [ FromServices ] SongService serviceSong )
2926 {
3027 //repoSong.Insert(new Song());
3128 //repoDetail.Insert(new Detail());
3229 //repoSong2.Insert(new Song());
3330
34- serviceSong . Test1 ( ) ;
31+ //serviceSong.Test1();
32+ await serviceSong . Test11 ( ) ;
3533 return "111" ;
3634 }
3735
3836 [ HttpGet ( "2" ) ]
39- // [Transactional]
40- async virtual public Task < object > GetAsync ( [ FromServices ] BaseRepository < Song > repoSong , [ FromServices ] BaseRepository < Detail > repoDetail , [ FromServices ] SongRepository repoSong2 ,
37+ [ Transactional ]
38+ public async Task < object > GetAsync ( [ FromServices ] BaseRepository < Song > repoSong , [ FromServices ] BaseRepository < Detail > repoDetail , [ FromServices ] SongRepository repoSong2 ,
4139 [ FromServices ] SongService serviceSong )
4240 {
43- await serviceSong . Test2 ( ) ;
44- await serviceSong . Test3 ( ) ;
41+ await repoSong . InsertAsync ( new Song ( ) ) ;
42+ await repoDetail . InsertAsync ( new Detail ( ) ) ;
4543 return "111" ;
4644 }
4745 }
@@ -61,23 +59,29 @@ public SongService(BaseRepository<Song> repoSong, BaseRepository<Detail> repoDet
6159 }
6260
6361 [ Transactional ( Propagation = Propagation . Nested ) ] //sqlite 不能嵌套事务,会锁库的
64- public virtual void Test1 ( )
62+ public void Test1 ( )
6563 {
6664 _repoSong . Insert ( new Song ( ) ) ;
6765 _repoDetail . Insert ( new Detail ( ) ) ;
6866 _repoSong2 . Insert ( new Song ( ) ) ;
6967 }
68+ [ Transactional ( Propagation = Propagation . Nested ) ] //sqlite 不能嵌套事务,会锁库的
69+ public Task Test11 ( )
70+ {
71+ return Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) . ContinueWith ( t =>
72+ _repoSong . InsertAsync ( new Song ( ) ) ) ;
73+ }
7074
7175 [ Transactional ( Propagation = Propagation . Nested ) ] //sqlite 不能嵌套事务,会锁库的
72- async public virtual Task Test2 ( )
76+ public async Task Test2 ( )
7377 {
7478 await _repoSong . InsertAsync ( new Song ( ) ) ;
7579 await _repoDetail . InsertAsync ( new Detail ( ) ) ;
7680 await _repoSong2 . InsertAsync ( new Song ( ) ) ;
7781 }
7882
7983 [ Transactional ( Propagation = Propagation . Nested ) ] //sqlite 不能嵌套事务,会锁库的
80- async public virtual Task < object > Test3 ( )
84+ public async Task < object > Test3 ( )
8185 {
8286 await _repoSong . InsertAsync ( new Song ( ) ) ;
8387 await _repoDetail . InsertAsync ( new Detail ( ) ) ;
@@ -110,53 +114,4 @@ public class Detail
110114 public int SongId { get ; set ; }
111115 public string Title { get ; set ; }
112116 }
113-
114- public static class IdleBusExtesions
115- {
116- static AsyncLocal < string > AsyncLocalTenantId = new AsyncLocal < string > ( ) ;
117- public static IdleBus < IFreeSql > ChangeTenant ( this IdleBus < IFreeSql > ib , string tenantId )
118- {
119- AsyncLocalTenantId . Value = tenantId ;
120- return ib ;
121- }
122- public static IFreeSql Get ( this IdleBus < IFreeSql > ib ) => ib . Get ( AsyncLocalTenantId . Value ?? "default" ) ;
123- public static IBaseRepository < T > GetRepository < T > ( this IdleBus < IFreeSql > ib ) where T : class => ib . Get ( ) . GetRepository < T > ( ) ;
124-
125- static void test ( )
126- {
127- IdleBus < IFreeSql > ib = null ; //单例注入
128-
129- var fsql = ib . Get ( ) ; //获取当前租户对应的 IFreeSql
130-
131- var fsql00102 = ib . ChangeTenant ( "00102" ) . Get ( ) ; //切换租户,后面的操作都是针对 00102
132-
133- var songRepository = ib . GetRepository < Song > ( ) ;
134- var detailRepository = ib . GetRepository < Detail > ( ) ;
135- }
136-
137- public static IServiceCollection AddRepository ( this IServiceCollection services , params Assembly [ ] assemblies )
138- {
139- services . AddScoped ( typeof ( IBaseRepository < > ) , typeof ( YourDefaultRepository < > ) ) ;
140- services . AddScoped ( typeof ( BaseRepository < > ) , typeof ( YourDefaultRepository < > ) ) ;
141-
142- services . AddScoped ( typeof ( IBaseRepository < , > ) , typeof ( YourDefaultRepository < , > ) ) ;
143- services . AddScoped ( typeof ( BaseRepository < , > ) , typeof ( YourDefaultRepository < , > ) ) ;
144-
145- if ( assemblies ? . Any ( ) == true )
146- foreach ( var asse in assemblies )
147- foreach ( var repo in asse . GetTypes ( ) . Where ( a => a . IsAbstract == false && typeof ( IBaseRepository ) . IsAssignableFrom ( a ) ) )
148- services . AddScoped ( repo ) ;
149-
150- return services ;
151- }
152- }
153-
154- class YourDefaultRepository < T > : BaseRepository < T > where T : class
155- {
156- public YourDefaultRepository ( IdleBus < IFreeSql > ib ) : base ( ib . Get ( ) , null , null ) { }
157- }
158- class YourDefaultRepository < T , TKey > : BaseRepository < T , TKey > where T : class
159- {
160- public YourDefaultRepository ( IdleBus < IFreeSql > ib ) : base ( ib . Get ( ) , null , null ) { }
161- }
162117}
0 commit comments