Commit 825ca353 authored by Almouhannad's avatar Almouhannad

(B) Add transactions to unit of work

parent 520307e1
...@@ -21,9 +21,9 @@ builder.Services.AddDbContext<ClinicsDbContext>( ...@@ -21,9 +21,9 @@ builder.Services.AddDbContext<ClinicsDbContext>(
dbContectOptionsBuilder.UseSqlServer(databaseOptions.ConnectionString, sqlServerAction => dbContectOptionsBuilder.UseSqlServer(databaseOptions.ConnectionString, sqlServerAction =>
{ {
sqlServerAction.EnableRetryOnFailure(databaseOptions.MaxRetryCount); //sqlServerAction.EnableRetryOnFailure(databaseOptions.MaxRetryCount);
sqlServerAction.CommandTimeout(databaseOptions.CommandTimeout); //sqlServerAction.CommandTimeout(databaseOptions.CommandTimeout);
}); });
// Be careful with this option, true only in development process! // Be careful with this option, true only in development process!
......
namespace Domain.UnitOfWork; using System.Data;
namespace Domain.UnitOfWork;
public interface IUnitOfWork public interface IUnitOfWork
{ {
public Task SaveChangesAsync(); public Task SaveChangesAsync();
public IDbTransaction BeginTransaction();
// Note that type "IDbTranaction" is from System.Data
// i.e. It's not related to any database system
} }
using Domain.UnitOfWork; using Domain.UnitOfWork;
using Microsoft.EntityFrameworkCore.Storage;
using Persistence.Context; using Persistence.Context;
using System.Data;
namespace Persistence.UnitOfWork; namespace Persistence.UnitOfWork;
...@@ -12,6 +14,12 @@ public class UnitOfWork : IUnitOfWork ...@@ -12,6 +14,12 @@ public class UnitOfWork : IUnitOfWork
_context = context; _context = context;
} }
public IDbTransaction BeginTransaction()
{
var transaction = _context.Database.BeginTransaction();
return transaction.GetDbTransaction();
}
public async Task SaveChangesAsync() public async Task SaveChangesAsync()
{ {
try try
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment