Skip to content
FreeSql DocumentsFreeSql Documents
Guide
NuGetopen in new window
Apiopen in new window
github icon
  • Guide

      • Repository Layer
        • Cascade Saving
          • Unit of Work
            • Unit of Work Manager
              • Use FreeSql's repository transaction in ASP.NET Core

          Unit of Work Manager

          author iconnicyecalendar iconNovember 21, 2021timer iconAbout 2 minword iconAbout 452 words

          On This Page
          • Use FreeSql's repository transaction in ASP.NET Core

          # Unit of Work Manager

          # Use FreeSql's repository transaction in ASP.NET Core

          # Step 1: Configure Startup.cs

          Install NuGet packages:

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

          Configure 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);
              //Add your own service, here is only an implementation
              services.AddScoped<TransBlogService>();
          }
          
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15

          Update your 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 MembersDescription
          IUnitOfWork CurrentReturn the current unit of work
          void Binding(repository)Hand over repository transaction to ir for management
          IUnitOfWork Begin(propagation, isolationLevel)Create unit of work
          • 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)
                  {     
                      //Actually, Rollback may not be used. 
                      //Because the internal Dispose of IUnitOfWork will roll back the transaction without Commit. 
                      //But here can be Rollback in advance.
                  
                      unitOfWork.Rollback();
                      //Log, 
                      //or use throw to continue throwing exceptions upwards
                  }
              }
          }
          
          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)
                  {
                      //Log, 
                      //or use throw to continue throwing exceptions upwards
                      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
          54
          55
          56
          57
          IUnitOfWork MembersDescription
          IFreeSql OrmThe object Select/Delete/Insert/Update/InsertOrUpdate is consistent with the unit of work transaction and can be omitted to pass WithTransaction
          DbTransaction GetOrBeginTransaction()Open the transaction, or return to the opened transaction
          void Commit()Commit transaction
          void Rollback()Rollback transaction
          DbContext.EntityChangeReport EntityChangeReportEntity change tracking within the unit of work

          # Complete code

          • Blog.csopen in new window
          • Tag.csopen in new window
          • TransBlogService.csopen in new window

          The above uses generic repository.

          If you want to rewrite a repository, how do you keep the same transaction as UnitOfWorkManager? You can inherit the existing DefaultRepository<,> and implement a custom repository 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

          The interface is IBlogRepository.cs:

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

          Inject this service in startup.cs

              services.AddScoped<IBlogRepository, BlogRepository>();
          
          1
          edit iconEdit this pageopen in new window
          Last update: 11/21/2021, 4:13:16 PM
          Contributors: luoyunchong
          Prev
          Unit of Work
          Copyright © 2018-present nicye
          Copyright © 2022 nicye

          This app can be installed on your PC or mobile device. This will allow this web app to look and behave like any other installed app. You will find it in your app lists and be able to pin it to your home screen, start menus or task bars. This installed web app will also be able to safely interact with other apps and your operating system.

          Description