UnitOfWorkManager事务

ASP.NET Core下FreeSql的仓储事务

第一步:配置 Startup.cs 注入

引入包

dotnet add package FreeSql
dotnet add package FreeSql.DbContext
dotnet add package FreeSql.Provider.MySqlConnector
1
2
3

配置 Startup.cs 注入

public void ConfigureServices(IServiceCollection services)
{
  IConfigurationSection Mysql = Configuration.GetSection("Mysql");
        Fsql = new FreeSqlBuilder()
            .UseConnectionString(DataType.MySql, Mysql.Value)
            .UseAutoSyncStructure(true)
            .UseNameConvert(NameConvertType.PascalCaseToUnderscoreWithLower)
            .UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
            .Build();
    services.AddSingleton<IFreeSql>(fsql);
    services.AddScoped<UnitOfWorkManager>();
    services.AddFreeRepository(null, typeof(Startup).Assembly);
    //新增自己的服务,这里只有实现
    services.AddScoped<TransBlogService>();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • appsettings.json
{
  "Mysql": "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=ovov_freesql_repository;Charset=utf8;SslMode=none;Max pool size=10",
}
1
2
3
UnitOfWorkManager 成员说明
IUnitOfWork Current返回当前的工作单元
void Binding(repository)将仓储的事务交给它管理
IUnitOfWork Begin(propagation, isolationLevel)创建工作单元
  • TransBlogService.cs
private readonly IBaseRepository<Blog, int> _blogRepository;
private readonly IBaseRepository<Tag, int> _tagRepository;
private readonly UnitOfWorkManager _unitOfWorkManager;

public TransBlogService(IBaseRepository<Blog, int> blogRepository, IBaseRepository<Tag, int> tagRepository,UnitOfWorkManager unitOfWorkManager)
{
    _blogRepository = blogRepository ;
    _tagRepository = tagRepository ;
    _unitOfWorkManager = unitOfWorkManager;
}

public async Task CreateBlogUnitOfWorkAsync(Blog blog,List<Tag>tagList)
{
    using (IUnitOfWork unitOfWork = _unitOfWorkManager.Begin())
    {
        try
        {
            await _blogRepository.InsertAsync(blog);
            tagList.ForEach(r =>
            {
                r.PostId = blog.Id;
            });
            await _tagRepository.InsertAsync(tagList);
            unitOfWork.Commit();
        }
        catch (Exception e)
        {     
            //实际 可以不Rollback。因为IUnitOfWork内部Dispose,会把没有Commit的事务Rollback回来,但能提前Rollback
        
            unitOfWork.Rollback();
            //记录日志、或继续throw;出来
        }
    }
}

public async Task UpdateBlogAsync(int id)
{
    using (IUnitOfWork unitOfWork = _unitOfWorkManager.Begin())
    {
        try
        {
            Blog blog = _blogRepository.Select.Where(r => r.Id == id).First();
            blog.IsDeleted = true;
            await _blogRepository.UpdateAsync(blog);
            unitOfWork.Commit();
        }
        catch (Exception e)
        {
           //记录日志、或继续throw;出来
            unitOfWork.Rollback();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
IUnitOfWork 成员说明
IFreeSql Orm该对象 Select/Delete/Insert/Update/InsertOrUpdate 与工作单元事务保持一致,可省略传递 WithTransaction
DbTransaction GetOrBeginTransaction()开启事务,或者返回已开启的事务
void Commit()提交事务
void Rollback()回滚事务
DbContext.EntityChangeReport EntityChangeReport工作单元内的实体变化跟踪
Dictionary<string, object> States用户自定义的状态数据,便于扩展

完整的代码

以上使用的是泛型仓储,那我们如果是重写一个仓储 如何保持和UnitOfWorkManager同一个事务呢。 继承现有的DefaultRepository<,>仓储,实现自定义的仓储BlogRepository.cs,

    public class BlogRepository : DefaultRepository<Blog, int>, IBlogRepository
    {
        public BlogRepository(UnitOfWorkManager uowm) : base(uowm?.Orm, uowm)
        {
        }

        public List<Blog> GetBlogs()
        {
            return Select.Page(1, 10).ToList();
        }
    }
1
2
3
4
5
6
7
8
9
10
11

其中接口。IBlogRepository.cs

    public interface IBlogRepository : IBaseRepository<Blog, int>
    {
        List<Blog> GetBlogs();
    }
1
2
3
4

在 startup.cs注入此服务

    services.AddScoped<IBlogRepository, BlogRepository>();
1