From 96844fcee30b01e3df85b544efe59f3ae99c2d0e Mon Sep 17 00:00:00 2001 From: igeekfan Date: Mon, 22 Aug 2022 22:09:29 +0800 Subject: [PATCH] =?UTF-8?q?Update=20async=20public=E6=8D=A2=E6=88=90public?= =?UTF-8?q?=20async?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/HomeController.cs | 20 ++++++++----------- Examples/aspnetcore_transaction/Program.cs | 8 +------- Examples/aspnetcore_transaction/Startup.cs | 9 +-------- .../TransactionalAttribute.cs | 9 ++------- .../Controllers/ValuesController.cs | 17 ++++++++-------- FreeSql.DbContext/DbContext/DbContextAsync.cs | 4 ++-- .../EfCoreFluentApi/EfCoreColumnFluent.cs | 5 +---- .../ContextSet/RepositoryDbContext.cs | 2 +- .../Repository/Repository/BaseRepository.cs | 10 +++++----- .../Repository/BaseRepositoryAsync.cs | 17 ++++++++-------- 10 files changed, 38 insertions(+), 63 deletions(-) diff --git a/Examples/aspnetcore_transaction/Controllers/HomeController.cs b/Examples/aspnetcore_transaction/Controllers/HomeController.cs index c979e628..cf840c17 100644 --- a/Examples/aspnetcore_transaction/Controllers/HomeController.cs +++ b/Examples/aspnetcore_transaction/Controllers/HomeController.cs @@ -1,14 +1,10 @@ -using FreeSql; +using System; +using System.ComponentModel; +using System.Threading.Tasks; +using FreeSql; using FreeSql.DataAnnotations; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using System; -using System.ComponentModel; -using System.Linq; -using System.Reflection; -using System.Threading; -using System.Threading.Tasks; namespace aspnetcore_transaction.Controllers { @@ -25,7 +21,7 @@ namespace aspnetcore_transaction.Controllers [HttpGet("1")] //[Transactional] - async public Task Get([FromServices] BaseRepository repoSong, [FromServices] BaseRepository repoDetail, [FromServices] SongRepository repoSong2, + public async Task Get([FromServices] BaseRepository repoSong, [FromServices] BaseRepository repoDetail, [FromServices] SongRepository repoSong2, [FromServices] SongService serviceSong) { //repoSong.Insert(new Song()); @@ -39,7 +35,7 @@ namespace aspnetcore_transaction.Controllers [HttpGet("2")] //[Transactional] - async public Task GetAsync([FromServices] BaseRepository repoSong, [FromServices] BaseRepository repoDetail, [FromServices] SongRepository repoSong2, + public async Task GetAsync([FromServices] BaseRepository repoSong, [FromServices] BaseRepository repoDetail, [FromServices] SongRepository repoSong2, [FromServices] SongService serviceSong) { await serviceSong.Test2(); @@ -77,7 +73,7 @@ namespace aspnetcore_transaction.Controllers } [Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的 - async public Task Test2() + public async Task Test2() { await _repoSong.InsertAsync(new Song()); await _repoDetail.InsertAsync(new Detail()); @@ -85,7 +81,7 @@ namespace aspnetcore_transaction.Controllers } [Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的 - async public Task Test3() + public async Task Test3() { await _repoSong.InsertAsync(new Song()); await _repoDetail.InsertAsync(new Detail()); diff --git a/Examples/aspnetcore_transaction/Program.cs b/Examples/aspnetcore_transaction/Program.cs index ffebf9c9..7d61ff83 100644 --- a/Examples/aspnetcore_transaction/Program.cs +++ b/Examples/aspnetcore_transaction/Program.cs @@ -1,11 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; namespace aspnetcore_transaction { diff --git a/Examples/aspnetcore_transaction/Startup.cs b/Examples/aspnetcore_transaction/Startup.cs index c6d52bec..bbaffcd1 100644 --- a/Examples/aspnetcore_transaction/Startup.cs +++ b/Examples/aspnetcore_transaction/Startup.cs @@ -1,19 +1,12 @@ using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using System.Text; -using System.Threading.Tasks; using aspnetcore_transaction.Controllers; using FreeSql; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.HttpsPolicy; -using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; namespace aspnetcore_transaction { @@ -22,7 +15,7 @@ namespace aspnetcore_transaction public Startup(IConfiguration configuration) { Configuration = configuration; - Fsql = new FreeSql.FreeSqlBuilder() + Fsql = new FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\test_trans.db") .UseAutoSyncStructure(true) .UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText)) diff --git a/Examples/aspnetcore_transaction/TransactionalAttribute.cs b/Examples/aspnetcore_transaction/TransactionalAttribute.cs index f37a1a3b..d1ab3350 100644 --- a/Examples/aspnetcore_transaction/TransactionalAttribute.cs +++ b/Examples/aspnetcore_transaction/TransactionalAttribute.cs @@ -1,13 +1,8 @@ -using FreeSql; -using Microsoft.AspNetCore.Mvc.Filters; -using Rougamo.Context; -using System; -using System.Collections.Generic; +using System; using System.Data; -using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; +using Rougamo.Context; namespace FreeSql { diff --git a/Examples/dbcontext_01/Controllers/ValuesController.cs b/Examples/dbcontext_01/Controllers/ValuesController.cs index 08586291..fd78fbd2 100644 --- a/Examples/dbcontext_01/Controllers/ValuesController.cs +++ b/Examples/dbcontext_01/Controllers/ValuesController.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using FreeSql; @@ -12,9 +11,9 @@ namespace dbcontext_01.Controllers public class ValuesController : ControllerBase { - IFreeSql _orm; - SongContext _songContext; - CurdAfterLog _curdLog; + private readonly IFreeSql _orm; + private readonly SongContext _songContext; + private readonly CurdAfterLog _curdLog; public ValuesController(SongContext songContext, IFreeSql orm1, CurdAfterLog curdLog) { _songContext = songContext; @@ -25,10 +24,12 @@ namespace dbcontext_01.Controllers // GET api/values [HttpGet] - async public Task Get() + public async Task Get() { - _orm.SetDbContextOptions(opt => { - opt.OnEntityChange = changeReport => { + _orm.SetDbContextOptions(opt => + { + opt.OnEntityChange = changeReport => + { Console.WriteLine(changeReport); }; }); @@ -136,7 +137,7 @@ namespace dbcontext_01.Controllers using (var uow = _orm.CreateUnitOfWork()) { - + var reposSong = uow.GetRepository(); reposSong.Where(a => a.Id > 10).ToList(); //查询结果,进入 states diff --git a/FreeSql.DbContext/DbContext/DbContextAsync.cs b/FreeSql.DbContext/DbContext/DbContextAsync.cs index 83fab920..af4717b7 100644 --- a/FreeSql.DbContext/DbContext/DbContextAsync.cs +++ b/FreeSql.DbContext/DbContext/DbContextAsync.cs @@ -13,14 +13,14 @@ namespace FreeSql { partial class DbContext { - async public virtual Task SaveChangesAsync(CancellationToken cancellationToken = default) + public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = default) { await FlushCommandAsync(cancellationToken); return SaveChangesSuccess(); } static ConcurrentDictionary>>> _dicFlushCommandDbSetBatchAsync = new ConcurrentDictionary>>>(); - async internal Task FlushCommandAsync(CancellationToken cancellationToken) + internal async Task FlushCommandAsync(CancellationToken cancellationToken) { if (isFlushCommanding) return; if (_prevCommands.Any() == false) return; diff --git a/FreeSql.DbContext/EfCoreFluentApi/EfCoreColumnFluent.cs b/FreeSql.DbContext/EfCoreFluentApi/EfCoreColumnFluent.cs index 1f89fc59..93ee91d4 100644 --- a/FreeSql.DbContext/EfCoreFluentApi/EfCoreColumnFluent.cs +++ b/FreeSql.DbContext/EfCoreFluentApi/EfCoreColumnFluent.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq.Expressions; -using FreeSql.DataAnnotations; +using FreeSql.DataAnnotations; namespace FreeSql.Extensions.EfCoreFluentApi { diff --git a/FreeSql.DbContext/Repository/ContextSet/RepositoryDbContext.cs b/FreeSql.DbContext/Repository/ContextSet/RepositoryDbContext.cs index bf6c0f7a..2134eee5 100644 --- a/FreeSql.DbContext/Repository/ContextSet/RepositoryDbContext.cs +++ b/FreeSql.DbContext/Repository/ContextSet/RepositoryDbContext.cs @@ -82,7 +82,7 @@ namespace FreeSql } #if net40 #else - async public override Task SaveChangesAsync(CancellationToken cancellationToken = default) + public override async Task SaveChangesAsync(CancellationToken cancellationToken = default) { await FlushCommandAsync(cancellationToken); return SaveChangesSuccess(); diff --git a/FreeSql.DbContext/Repository/Repository/BaseRepository.cs b/FreeSql.DbContext/Repository/Repository/BaseRepository.cs index da97a6b8..5770ed5a 100644 --- a/FreeSql.DbContext/Repository/Repository/BaseRepository.cs +++ b/FreeSql.DbContext/Repository/Repository/BaseRepository.cs @@ -103,7 +103,7 @@ namespace FreeSql _dbset.RemoveRange(entitys); return _db.SaveChanges(); } - public List DeleteCascadeByDatabase(Expression> predicate) + public virtual List DeleteCascadeByDatabase(Expression> predicate) { var list = _dbset.RemoveCascadeByDatabase(predicate); var affrows = _db.SaveChanges(); @@ -151,14 +151,14 @@ namespace FreeSql return entity; } - public void SaveMany(TEntity entity, string propertyName) + public virtual void SaveMany(TEntity entity, string propertyName) { _dbset.SaveMany(entity, propertyName); _db.SaveChanges(); } - public void BeginEdit(List data) => _dbset.BeginEdit(data); - public int EndEdit(List data = null) + public virtual void BeginEdit(List data) => _dbset.BeginEdit(data); + public virtual int EndEdit(List data = null) { _db.FlushCommand(); if (UnitOfWork?.GetOrBeginTransaction(true) == null && _db.OrmOriginal.Ado.TransactionCurrentThread == null) @@ -206,6 +206,6 @@ namespace FreeSql public virtual int Delete(TKey id) => Delete(CheckTKeyAndReturnIdEntity(id)); public virtual TEntity Find(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOne(); - public TEntity Get(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOne(); + public virtual TEntity Get(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOne(); } } diff --git a/FreeSql.DbContext/Repository/Repository/BaseRepositoryAsync.cs b/FreeSql.DbContext/Repository/Repository/BaseRepositoryAsync.cs index 6960ec56..5ade3e00 100644 --- a/FreeSql.DbContext/Repository/Repository/BaseRepositoryAsync.cs +++ b/FreeSql.DbContext/Repository/Repository/BaseRepositoryAsync.cs @@ -1,5 +1,4 @@ -using FreeSql.Extensions.EntityUtil; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -14,7 +13,7 @@ namespace FreeSql where TEntity : class { - async virtual public Task DeleteAsync(Expression> predicate, CancellationToken cancellationToken = default) + public virtual async Task DeleteAsync(Expression> predicate, CancellationToken cancellationToken = default) { var delete = _dbset.OrmDeleteInternal(null).Where(predicate); var sql = delete.ToSql(); @@ -32,20 +31,20 @@ namespace FreeSql _dbset.RemoveRange(entitys); return _db.SaveChangesAsync(cancellationToken); } - async public virtual Task> DeleteCascadeByDatabaseAsync(Expression> predicate, CancellationToken cancellationToken = default) + public virtual async Task> DeleteCascadeByDatabaseAsync(Expression> predicate, CancellationToken cancellationToken = default) { var list = await _dbset.RemoveCascadeByDatabaseAsync(predicate, cancellationToken); var affrows = await _db.SaveChangesAsync(cancellationToken); return list; } - async public virtual Task InsertAsync(TEntity entity, CancellationToken cancellationToken = default) + public virtual async Task InsertAsync(TEntity entity, CancellationToken cancellationToken = default) { await _dbset.AddAsync(entity, cancellationToken); await _db.SaveChangesAsync(cancellationToken); return entity; } - async public virtual Task> InsertAsync(IEnumerable entitys, CancellationToken cancellationToken = default) + public virtual async Task> InsertAsync(IEnumerable entitys, CancellationToken cancellationToken = default) { await _dbset.AddRangeAsync(entitys, cancellationToken); await _db.SaveChangesAsync(cancellationToken); @@ -63,14 +62,14 @@ namespace FreeSql return _db.SaveChangesAsync(cancellationToken); } - async public virtual Task InsertOrUpdateAsync(TEntity entity, CancellationToken cancellationToken = default) + public virtual async Task InsertOrUpdateAsync(TEntity entity, CancellationToken cancellationToken = default) { await _dbset.AddOrUpdateAsync(entity, cancellationToken); await _db.SaveChangesAsync(cancellationToken); return entity; } - async public Task SaveManyAsync(TEntity entity, string propertyName, CancellationToken cancellationToken = default) + public virtual async Task SaveManyAsync(TEntity entity, string propertyName, CancellationToken cancellationToken = default) { await _dbset.SaveManyAsync(entity, propertyName, cancellationToken); await _db.SaveChangesAsync(cancellationToken); @@ -81,7 +80,7 @@ namespace FreeSql { public virtual Task DeleteAsync(TKey id, CancellationToken cancellationToken = default) => DeleteAsync(CheckTKeyAndReturnIdEntity(id), cancellationToken); public virtual Task FindAsync(TKey id, CancellationToken cancellationToken = default) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken); - public Task GetAsync(TKey id, CancellationToken cancellationToken = default) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken); + public virtual Task GetAsync(TKey id, CancellationToken cancellationToken = default) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken); } } #endif \ No newline at end of file