AutoMapper自定义值解析器异步

时间:2019-08-20 14:00:29

标签: asp.net-core automapper

我想使用AutoMapper将BotViewModel转换为Bot

下面有一个来自类型BotViewModel的示例输入。如果要向数据库中添加新的漫游器,则应将该模型与Bot模型进行匹配/映射,并应解决Bot的外键。

换句话说,BotViewModel.Symbol应该与CryptoPair.Symbol匹配,BotViewModel.Interval要与TimeInterval.Interval匹配。

{
  "id": 0,
  "name": "Bot 1",
  "status": true,
  "symbol": "LTCBTC",
  "interval": 6
}

我下面的代码完全按照上面的描述工作。它使用http://docs.automapper.org/en/stable/Custom-value-resolvers.html。问题是我想使_context.CryptoPairs.FirstOrDefault异步FirstOrDefault => FirstOrDefaultAsync

我想这样做,因为我觉得这不是最佳实践。基本上,我正在寻找建议,如果可以的话。顺便说一句,如果符号或间隔因为数据库中根本不存在而无法匹配,则var bot = _mapper.Map<Bot>(botViewModel);应该抛出一个异常,稍后由我的控制器处理。

public class CryptoPairResolver : IValueResolver<BotViewModel, Bot, int>
{
    private readonly BinanceContext _context;

    public CryptoPairResolver(BinanceContext context)
    {
        _context = context;
    }

    public int Resolve(BotViewModel source, Bot destination, int destMember, ResolutionContext context)
    {
        return _context.CryptoPairs.FirstOrDefault(p => p.Symbol == source.Symbol).Id;
    }
}

public class TimeIntervalResolver : IValueResolver<BotViewModel, Bot, int>
{
    private readonly BinanceContext _context;

    public TimeIntervalResolver(BinanceContext context)
    {
        _context = context;
    }

    public int Resolve(BotViewModel source, Bot destination, int destMember, ResolutionContext context)
    {
        return _context.TimeIntervals.FirstOrDefault(i => i.Interval == source.Interval).Id;
    }
}

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<BotViewModel, Bot>()
            .ForMember(dest => dest.CryptoPairId, opt => opt.MapFrom<CryptoPairResolver>())
            .ForMember(dest => dest.TimeIntervalId, opt => opt.MapFrom<TimeIntervalResolver>());
    }
}

型号:

public class Bot
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Status { get; set; }

    public int CryptoPairId { get; set; }
    public CryptoPair CryptoPair { get; set; }

    public int TimeIntervalId { get; set; }
    public TimeInterval TimeInterval { get; set; }
}

public class BotViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Status { get; set; }
    public string Symbol { get; set; }
    public KlineInterval Interval { get; set; }
}

public class CryptoPair
{
    public int Id { get; set; }
    public string Symbol { get; set; }

    public List<Bot> Bots { get; set; }
}

public class TimeInterval
{
    public int Id { get; set; }
    public KlineInterval Interval { get; set; }

    public List<Bot> Bots { get; set; }
}

编辑:

public async Task<Bot> CreateAsync(BotViewModel botViewModel)
{
    // Map BotViewModel to Bot
    var cryptoPair = await _context.CryptoPairs.FirstOrDefaultAsync(e => e.Symbol == botViewModel.Symbol);

    if (cryptoPair == null)
    {
        throw new BadRequestException();
    }

    var timeInterval = await _context.TimeIntervals.FirstOrDefaultAsync(e => e.Interval == botViewModel.Interval);

    if (timeInterval == null)
    {
        throw new BadRequestException();
    }

    var bot = new Bot
    {
        Name = botViewModel.Name,
        Status = botViewModel.Status,
        CryptoPairId = cryptoPair.Id,
        TimeIntervalId = timeInterval.Id
    };

    // Create code
    _context.Bots.Add(bot);
    await _context.SaveChangesAsync();

    return bot;
}

0 个答案:

没有答案