[英]Using UnitOfWork design at different endpoints. Can't tracking entity updates when I try to apply savechanges. #.net (UnitOfWork design)
If I use the two service's method at same action like below It is working.如果我像下面这样在同一操作中使用这两种服务的方法,它就可以工作。
[HttpPost, Route("save_changes")]
public async Task<IActionResult> SaveChangesAsync()
{
_logService.CreateLog(logCreateModel);
await _unitOfWork.SaveChangesAsync();
return Ok();
}
But I need to call the entity updating and SaveChanges methods at different action Because I want to send request from different API.Like this;但是我需要在不同的操作中调用实体更新和 SaveChanges 方法因为我想从不同的 API 发送请求。像这样;
[HttpPost, Route("create")]
public async Task CreateLog(LogCreateModel logCreateModel)
{
await _logService.CreateLog(logCreateModel);
}
[HttpPost, Route("save_changes")]
public async Task<IActionResult> SaveChangesAsync()
{
unitOfWork.SaveChangesAsync();
return Ok();
}
and this my UnitOfWork class;这是我的工作单元 class;
public class UnitOfWork : IUnitOfWork
{
private readonly PrimeLogDatabaseContext _context;
private IRepository<Log> _log;
private IRepository<LifeCycle> _lifeCycle;
public UnitOfWork(PrimeLogDatabaseContext context)
{
_context = context;
}
public IRepository<Log> log => _log ?? new LogRepository(_context);
public IRepository<LifeCycle> lifeCycle => _lifeCycle ?? new LifeCycleRepository(_context);
public async Task SaveChangesAsync()
{
await _context.SaveChangesAsync();
}
public void SaveChanges()
{
_context.SaveChanges();
}
}
How I can?我怎么能?
Ps: I want to create more than one log and at the end of the request I want commit all logs to database. Ps:我想创建多个日志,在请求结束时我想将所有日志提交到数据库。 I used the UnitOfWork design because I have to seperate SaveChanges method from creation process.
我使用 UnitOfWork 设计是因为我必须将 SaveChanges 方法与创建过程分开。 At Another API I have action which including CreateLog request services.
在另一个 API 我有包括 CreateLog 请求服务在内的操作。 And I send request to SaveChanges with using the attribute that trigger when action result.
我使用在操作结果时触发的属性向 SaveChanges 发送请求。
After months;几个月后; I solved this problem with another design pattern.
我用另一种设计模式解决了这个问题。 I used to UnitOfWork structure at one Api(working as middleware one) and used chain of responsibility pattern.
我曾经在一个 Api 中使用 UnitOfWork 结构(作为中间件)并使用责任链模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.