admin管理员组文章数量:1410689
For context: I use Mapster and Serilog.
public class UserService(IUserRepository repository, ILogger logger)
{
public async Task<TDto?> GetByIdAsync<TDto>(int userId, Func<IQueryable<User>, IQueryable<User>>? include = null,
CancellationToken cancellationToken = default)
{
try
{
var entity = await repository.GetByIdAsync(userId, include, cancellationToken);
return entity is null ? default : entity.Adapt<TDto?>();
}
catch (OracleException ex) when (ex.Number == 1013)
{
logger.Information("The request was cancelled.");
return default;
}
}
public async Task<IEnumerable<TDto>> GetByNameAsync<TDto>(string name,
CancellationToken cancellationToken = default)
{
try
{
var entities = await repository.GetByNameAlnumAsync(name, cancellationToken);
return entities.Adapt<IEnumerable<TDto>>();
}
catch (OracleException ex) when (ex.Number == 1013)
{
logger.Information("The request was cancelled.");
return [];
}
}
public async Task<IEnumerable<TDto>> GetByFilterAsync<TDto>(Filter filter,
CancellationToken cancellationToken = default)
{
try
{
var entities = await repository.GetByFilterAsync(filter, cancellationToken);
return entities.Adapt<IEnumerable<TDto>>();
}
catch (OracleException ex) when (ex.Number == 1013)
{
logger.Information("The request was cancelled.");
return [];
}
}
}
Questions:
- Am I using
TDto
correctly? - The
include
argument in theGetByIdAsync
method violates SRP. Let's sayUser
has many external dependencies, and they are used in different ways everywhere. For each use, make a method that differs from many similar ones by another set of.Include()
? - What is the best way to anize
try catch
blocks? Result object? What about repeatability? I shouldn't make a method in this service that will handleException
, right? Handler service? What information should be passed on?
本文标签: cHow to properly organize (User)Service DTOInclude and exception handlingStack Overflow
版权声明:本文标题:c# - How to properly organize (User)Service: DTO, Include and exception handling? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744817033a2626758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论