- What language is this for? C#
- Which rule? Duplication
- Are you using
- SonarQube - 10.1 (build 73491)
Duplication problems emerge with the following code:
[LoggerMessage(11, LogLevel.Information, "{auth} - {action}({param})")]
public static partial void LogControllerAction(this ILogger logger, string auth, string? param = null, [CallerMemberName] string action = null!);
public async Task<ActionResult> CreateResourceAsync([FromBody] MailboxModel? resource)
{
_logger.LogControllerAction(_identityService.GetUserName(), resource?.Uuid);
_logger.DebugCreateResource(_identityService.GetUserName(), resource?.Uuid, JsonSerializer.Serialize(resource, _serializerOptions));
await _mediator.Send(new NewResourceDispatchCommand(resource));
return NoContent();
}
public async Task<ActionResult> CreateResourceAsync([FromBody] AppModel? resource)
{
_logger.LogControllerAction(_identityService.GetUserName(), resource?.Uuid);
_logger.DebugCreateResource(_identityService.GetUserName(), resource?.Uuid, JsonSerializer.Serialize(resource, _serializerOptions));
await _mediator.Send(new NewResourceDispatchCommand(resource));
return NoContent();
}
Why I think it shouldn’t:
- the call to
LogControllerAction
can not be moved or the[CallerMemberName]
will be lost - While
MailboxModel
andAppModel
both inherit from a base model class, their serialization result won’t be the same hence it is not possible to refactor it.
To comply with the rule, I refactored the following way but honestly I fail to see the gain considering the actions done:
public async Task<ActionResult> CreateResourceAsync([FromBody] AppModel? resource)
{
_logger.LogControllerAction(_identityService.GetUserName(), resource?.Uuid);
await Create(resource, JsonSerializer.Serialize(resource, _serializerOptions));
return NoContent();
}
private async Task Create(CoreResourceModel? model, string serialized)
{
_logger.DebugCreateResource(_identityService.GetUserName(), model?.Uuid, serialized);
await _mediator.Send(new NewResourceDispatchCommand(model));
}