【发布时间】:2019-03-01 03:58:09
【问题描述】:
我的 WebApi 中有一个相当大的查询,它过滤来自不同模型的数据以将它们以 DTO 对象的形式发送到 FrontEnd(Angular)。
我认为 DTO 可能是正确的方法,因为前端不需要从所有模型中获取所有参数。
我的问题在于将 DTO 对象映射回我的 WebApi 模型。
我尝试了 NugetPackages 的 Automapper,但它没有用。我还听说当项目越来越大时,AutoMapper 不是正确的选择。
以下是我的 DTO 对象、查询和模型的代码:
public class ApplicationSettingsDto
{
public string KeyName { get; set; }
public string Wert { get; set; }
public string DefaultValue { get; set; }
public string Description { get; set; }
}
型号:
public partial class ApplicationSettings
{
public string KeyName { get; set; }
public string Wert { get; set; }
public int Typ { get; set; }
public string DisplayOrder { get; set; }
}
public partial class ApplicationSettingsDefaults
{
public Guid Id { get; set; }
public string KeyName { get; set; }
public string Value { get; set; }
public int ProduktOption { get; set; }
}
public partial class Text
{
public string KeyName { get; set; }
public string Sprache { get; set; }
public string Text1 { get; set; }
public DateTime LetzteAenderung { get; set; }
}
查询:
public IQueryable Description()
{
int produktOption = GetProduktOption();
var query = from appl in _repositoryContext.ApplicationSettings
from text in _repositoryContext.Text
from defaults in _repositoryContext.ApplicationSettingsDefaults
//filter DefaultValues
where appl.KeyName.Equals(defaults.KeyName) &&
(defaults.ProduktOption.Equals(produktOption) || defaults.ProduktOption.Equals(65535))
//Filter TextValues
where EF.Functions.Like(text.KeyName, "%" + appl.KeyName) ||
EF.Functions.Like(text.KeyName, "%" + appl.KeyName + "$Descr")
where EF.Functions.Like(text.Sprache, "de-DE")
select new ApplicationSettingsDto()
{
KeyName = appl.KeyName,
Wert = appl.Wert,
DefaultValue = defaults.Value,
Description = text.Text1
}
into output orderby output.KeyName select output;
return query;
}
所以这个问题不是关于详细的实现,它只是关于实现 DTO 的建议,因为映射可能是 *ss 中的一个痛苦,就像在我的示例中一样。
我乐于接受我还不知道的新想法或新模式来尝试解决此类问题。
在此先感谢 ;)
【问题讨论】:
标签: rest asp.net-web-api .net-core automapper dto