我在核心层实现了存储库和 UnitOfWork 模式:
public class Repository<TEntity, TContext> : UnitOfWork, IRepository<TEntity>
where TEntity : class, new()
where TContext : DbContext
{
private readonly TContext _context;
public Repository(TContext dbContext) : base(dbContext)
{
_context = dbContext;
}
public async Task<bool> AddAsync(TEntity model)
{
await _context.Set<TEntity>().AddAsync(model);
return true;
}
public List<TEntity> Get()
{
return _context.Set<TEntity>().ToList();
}
public bool Remove(TEntity model)
{
_context.Set<TEntity>().Remove(model);
return true;
}
public async Task<int> SaveAsync()
{
return await _context.SaveChangesAsync();
}
public bool Update(TEntity model)
{
_context.Set<TEntity>().Update(model);
return true;
}
}
和
public class UnitOfWork : IUnitOfWork
{
private readonly DbContext _context;
private readonly IDbContextTransaction _transaction;
public UnitOfWork(DbContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_transaction = _context.Database.CurrentTransaction ?? _context.Database.BeginTransaction();
}
public async Task<bool> Commit(bool state = true)
{
try
{
await _context.SaveChangesAsync();
if (state)
_transaction.Commit();
else
_transaction.Rollback();
return true;
}
catch (Exception)
{
_transaction.Rollback();
throw; // Rethrow the exception after rolling back the transaction
}
finally
{
Dispose();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual async void Dispose(bool disposing)
{
if (disposing)
{
await _context.DisposeAsync();
_transaction.Dispose();
}
}
}
所以我在我的 DataAccess 和 BusinessLayer 中使用这个类,最后在我的 MVC 控制器中使用这个类,就像分层结构一样。当我调用 Get() 方法时我可以获取值。但我尝试通过 Update() 方法更改一个值,但没有成功。尽管我调用了 SaveAsync() 方法。
我正在下面分享我的数据访问和业务层。
数据访问:
public class ResponsibleDal : Repository<Responsible, TaskTracingDbContext>, IResponsibleDal
{
public ResponsibleDal(TaskTracingDbContext dbContext) : base(dbContext)
{
}
}
数据访问工厂:
public class MainDal : IMainDal
{
private readonly Lazy<IResponsibleDal> _responsibleDalLazy;
public MainDal(IResponsibleDal responsibleDalLazy)
{
_responsibleDalLazy = new Lazy<IResponsibleDal>(() => responsibleDalLazy);
}
public IResponsibleDal ResponsibleDal => _responsibleDalLazy.Value;
}
业务:
public class ResponsibleBus : IResponsibleBus
{
private readonly IMainDal _mainDal;
public ResponsibleBus(IMainDal mainDal)
{
_mainDal = mainDal;
}
public async Task<bool> AddAsync(Responsible model)
{
return await _mainDal.ResponsibleDal.AddAsync(model);
}
public List<Responsible> Get()
{
return _mainDal.ResponsibleDal.Get();
}
public bool Remove(Responsible model)
{
return _mainDal.ResponsibleDal.Remove(model);
}
public Task<int> SaveAsync()
{
return _mainDal.ResponsibleDal.SaveAsync();
}
public Responsible? GetById(int id)
{
return _mainDal.ResponsibleDal.Get().Find(x => x.Id == id);
}
public Responsible? GetByIdentityNumber(string? identityNumber)
{
if (identityNumber is null)
return null;
return _mainDal.ResponsibleDal.Get().Find(x => x.IdentityNumber == identityNumber);
}
public bool Update(Responsible model)
{
return _mainDal.ResponsibleDal.Update(model);
}
}
商业工厂:
public class MainBus : IMainBus
{
private readonly Lazy<IResponsibleBus> _responsibleBusLazy;
public MainBus(IResponsibleBus responsibleBusLazy)
{
_responsibleBusLazy = new Lazy<IResponsibleBus>(() => responsibleBusLazy);
}
public IResponsibleBus ResponsibleBus => _responsibleBusLazy.Value;
}
终于在我的控制器中:
var entity = _mainBus.ResponsibleBus.GetByIdentityNumber(model.UserName);
if (entity is not null)
{
entity.ThemeSettings = model.Theme;
_mainBus.ResponsibleBus.Update(entity);
await _mainBus.ResponsibleBus.SaveAsync();
HttpContext.Session.SetString("Theme", model.Theme.ToString());
}
else
{
TempData["Error"] = "Kullanıcı veritabanında bulunamadı";
return RedirectToAction(nameof(Index), model);
}
我还在 Program.cs 中这样定义了范围:
builder.Services.AddDbContext<TaskTracingDbContext>();
builder.Services.AddTransient<IResponsibleDal, ResponsibleDal>();
builder.Services.AddTransient(provider => new Lazy<IResponsibleDal>(provider.GetRequiredService<IResponsibleDal>()));
builder.Services.AddTransient<IResponsibleBus, ResponsibleBus>();
builder.Services.AddTransient(provider => new Lazy<IResponsibleBus>(provider.GetRequiredService<IResponsibleBus>()));
builder.Services.AddTransient<IMainDal, MainDal>();
builder.Services.AddTransient<IMainBus, MainBus>();
你能向我解释一下我不知道的细节吗?
我解决了。因为 Repository 类中的
SaveAsync()
方法。我必须调用 Commit()
类下的 UnitOfWork
方法。 Commit()
方法保存更改。
public async Task<bool> SaveAsync()
{
return await Commit();
}