【问题标题】:How to manage DTO Implementaion for Rest-Api in .NET CORE? Alternatives?如何在 .NET CORE 中管理 Rest-Api 的 DTO 实现?备择方案?
【发布时间】: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


    【解决方案1】:

    这个问题可能会因为您有工作代码而被关闭,但我的建议是,在多年尝试 AutoMapper、基于反射的映射和手写映射之后,您应该坚持使用最简单且有效的方法。

    您通常必须为 DTO 编写一次映射逻辑。您将编写的代码清晰明了。当您将其移至 AutoMapper 时,您现在最终会得到一段通常不相关且不太易读的代码,用于非常非常简单的事情。

    如果您需要其他函数中的映射逻辑,请将其提取到单独的方法中。如果您在单独的类中需要它,请将该映射函数提升为 DTO 上的静态方法。

    我的大部分映射代码如下所示:

    // Some controller code
    da.GetStudents().Select(Map); // Map is the function below
    

    在控制器中,定义了如下方法:

    public StudentDto Map(Student student)
    {
        if (student == null) return null;
    
        return new StudentDto
        {
            FirstName = student.FirstName,
            ...
        };
    }
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-03-28
      • 1970-01-01
      • 2018-04-27
      • 2020-03-28
      • 1970-01-01
      • 2019-07-19
      • 2018-05-02
      • 2020-03-16
      • 2018-05-28
      相关资源
      最近更新 更多