简体   繁体   English

在不同端点使用 UnitOfWork 设计。 当我尝试应用保存更改时无法跟踪实体更新。 #.net(工作单元设计)

[英]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.

相关问题 这是围绕DDD使用仓库模式设计的一个很好的UnitOfWork - Is this a good UnitOfWork with Repository pattern Design around DDD 可以访问存储库中的UnitOfWork是不好的设计吗? - Is it bad design to have access to the UnitOfWork in the Repository? 我如何使用StructureMap从UnitOfWork注入上下文 - How i can inject context from UnitOfWork using StructureMap 使用UnitOfWork和存储库模式与实体框架 - using UnitOfWork and Repository Pattern with Entity Framework 带有存储库和实体框架的UnitOfWork - UnitOfWork with Repository and Entity Framework 实体框架工作单元模式 - Entity Framework UnitOfWork Pattern DTO和UnitOfWork模式是一种为Web应用程序设计DAL的好方法吗? - Is DTO plus UnitOfWork pattern a good approach to design a DAL for a web application? 无法模拟unitOfWork并且没有服务接口 - Can't mock unitOfWork and no interfaces of services 设计模式:使用IoC设置控制器,服务,存储库和UnitOfWork - Design Pattern: Setting up Controllers, Service, Repositories and UnitOfWork with IoC 使用 UnitOfWork 时正确分离关注点? - Proper Separation of Concerns When Using UnitOfWork?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM