Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 属性值更新的最佳/逻辑实践(控制器或服务层?)_C#_Asp.net_Design Patterns_Architecture - Fatal编程技术网

C# 属性值更新的最佳/逻辑实践(控制器或服务层?)

C# 属性值更新的最佳/逻辑实践(控制器或服务层?),c#,asp.net,design-patterns,architecture,C#,Asp.net,Design Patterns,Architecture,我的项目得到了Web API控制器、服务和存储库。控制器获得了如下更新方法: public IActionResult Update(CreateCollaboratorViewModel collaboratorViewModel) { //Is it good to set values here in Controller or in Service layer ? AdminCollaborators collaborator = new Adm

我的项目得到了Web API控制器、服务和存储库。控制器获得了如下更新方法:

 public IActionResult Update(CreateCollaboratorViewModel collaboratorViewModel)
    {
       //Is it good to set values here in Controller or in Service layer ?

        AdminCollaborators collaborator = new AdminCollaborators();
        collaborator.Description = collaboratorViewModel.Description;
        collaborator.ModifiedBy = _myContext.CurrentUserId;
        var output = _collaboratorService.UpdateCollaborator(collaborator, _myContext.CurrentUserId);
        return Ok(new WebApiResultValue(true, output, "update successful."));
    }
服务

 public AdminCollaborators UpdateCollaborator(AdminCollaborators collaborator, Guid actorUserId)
    {
        collaborator.ModifiedBy = actorUserId;
        collaborator.ModifiedOn = DateTimeHelper.Instance.GetCurrentDate();
        _collaboratorRepository.UpdateCollaborator(collaborator,actorUserId);
        return _collaborationRepository.SaveChanges();
    }
通常,服务应该实现业务逻辑(如果im没有错的话)。请告知我是否应该更新控制器或服务中的属性值


谢谢

这是实施DDD ish架构的常见结果。当域过于简单时,最终会有许多层在或多或少的抽象级别上做相同的事情

顺便说一句,你走这条路是因为你决定有一天你会想把更复杂的事情放在正确的地方。否则,您需要进行大规模重构,才能将一个非分层项目转变为一个n层项目

无论如何,更新应该在服务层完成。您的服务层可能应该如下所示:

collaboratorService.Update(collaboratorId, updateCollaboratorDto);
其中,
updateCollaboratorDto
应该是一个DTO,它应该与要在域对象上更新的数据一起提供

对于DTO和域对象之间的映射,您应该使用AutoMapper自动映射它们


请注意,您身处一个WebAPI/restfulapi的世界,您正在谈论视图模型。我会将它们重命名,将ViewModel sufix替换为Dto。

这是实现DDD ish架构的常见结果。当域过于简单时,最终会有许多层在或多或少的抽象级别上做相同的事情

顺便说一句,你走这条路是因为你决定有一天你会想把更复杂的事情放在正确的地方。否则,您需要进行大规模重构,才能将一个非分层项目转变为一个n层项目

无论如何,更新应该在服务层完成。您的服务层可能应该如下所示:

collaboratorService.Update(collaboratorId, updateCollaboratorDto);
其中,
updateCollaboratorDto
应该是一个DTO,它应该与要在域对象上更新的数据一起提供

对于DTO和域对象之间的映射,您应该使用AutoMapper自动映射它们

请注意,您身处一个WebAPI/restfulapi的世界,您正在谈论视图模型。我会将它们重命名,将ViewModel sufix替换为Dto