You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在.NET Clean Architecture中用Mediatr实现CQRS,业务逻辑应放何处?

Clean Architecture + CQRS架构下业务逻辑的放置位置困惑

我在使用Clean Architecture和CQRS模式实现业务逻辑时遇到了困惑,当前使用Mediatr和AutoMapper进行开发,项目结构如下:

  • ApplicationCore
    • DTOs
      • SmartphoneDto.cs
    • Features
      • Smartphone
        • Handlers
          • Commands
            • AddSmartphoneCommandHandler.cs
          • Queries
        • Requests
          • Commands
            • AddSmartphoneCommand.cs
          • Queries
  • DomainCore
    • Smartphone.cs

现有代码实现

SmartphoneDto.cs

public class SmartphoneDto
{
    public int Id { get; set; }
    public string serialnumber { get; set; }
}

领域实体Smartphone.cs

public class Smartphone
{
    public int Id { get; set; }
    public string imei { get; set; }
    public string serialnumber { get; set; }
    public string calculatedValue { get; set; }
}

AddSmartphoneCommand.cs

public class AddSmartphoneCommand : IRequest<bool>
{
    public SmartphoneDto SmartphoneDto { get; set; }
}

AddSmartphoneCommandHandler.cs

public class AddSmartphoneCommandHandler : IRequestHandler<AddSmartphoneCommand, bool>
{
    private readonly ISmartphoneRepository _smartphoneRepository;
    private readonly IMapper _mapper;

    public AddSmartphoneCommandHandler(ISmartphoneRepository smartphoneRepository, IMapper mapper)
    {
        _smartphoneRepository = smartphoneRepository;
        _mapper = mapper;
    }

    public async Task<bool> Handle(AddSmartphoneCommand request, CancellationToken cancellationToken)
    {
        var smartphone = _mapper.Map<Smartphone>(request.SmartphoneDto);
        //Business Logic???
        await _smartphoneRepository.AddAsync(smartphone);

        return true;
    }
}

困惑点与尝试

我不确定业务逻辑应该放置在哪里,查阅了两篇StackOverflow帖子但未得到清晰解决方案:

  • 一篇提到可将业务逻辑放在命令处理器Handle方法调用的外部服务类中;
  • 另一篇则指出命令处理器不应包含任何逻辑。

我曾考虑将业务逻辑直接放在Handle方法中,示例如下:

public class AddSmartphoneCommandHandler : IRequestHandler<AddSmartphoneCommand, bool>
{
    private readonly ISmartphoneRepository _smartphoneRepository;
    private readonly IMapper _mapper;
    private readonly ISomeApiService _someApiService;

    public AddSmartphoneCommandHandler(ISmartphoneRepository smartphoneRepository, IMapper mapper, ISomeApiService someApiService)
    {
        _smartphoneRepository = smartphoneRepository;
        _mapper = mapper;
        _someApiService = someApiService;
    }

    public async Task<bool> Handle(AddSmartphoneCommand request, CancellationToken cancellationToken)
    {
        var smartphone = _mapper.Map<Smartphone>(request.SmartphoneDto);

        #region businesslogic
        // 业务逻辑示例
        smartphone.imei = await _someApiService.GetAsync("someurl", smartphone.serialnumber);
        var serialHash = smartphone.serialnumber.GetHashCode();
        var imeiHash = smartphone.imei.GetHashCode();
        smartphone.calculatedValue = ((serialHash - imeiHash) * 0.05).ToString();
        // end business logic
        #endregion

        await _smartphoneRepository.AddAsync(smartphone);

        return true;
    }
}

问题

请问在当前项目结构中,这类业务逻辑的正确放置位置是什么?

内容的提问来源于stack exchange,提问作者Andres Camacho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 13:10:20