【问题标题】:oat++ : put DTO in a list of DTOsoat++ : 将 DTO 放入 DTO 列表中
【发布时间】:2021-10-27 13:50:21
【问题描述】:

我正在尝试从多个 DTO 创建一个大 DTO,但是将我的 DTO 放入列表中时遇到了很多麻烦。

我有两个 DTO:

class TypeDocDto : public oatpp::DTO
{
    DTO_INIT(TypeDocDto, DTO)
    DTO_FIELD(Int32, code);
    DTO_FIELD(String, desciption);
};

class DocumentDto : public oatpp::DTO
{
    DTO_INIT(DocumentDto, DTO)
    DTO_FIELD(Int32, docNumber);
    DTO_FIELD(Int32, typeDocNb);
    DTO_FIELD(List<Object<TypeDocDto>>, typeDocs);
};

这里的想法是一个文档对象可以携带多个“TypeDoc”对象。

所以我尝试创建一个 TypeDocDto 列表,然后将其添加到我的 DocumentDto 对象中。

auto dtoDoc = DocumentDto::createShared();
dtoDoc->docNumber = 0; //That value is whatever for now.
dtoDoc->typeDocNb = 3;

oatpp::List<oatpp::Object<TypeDocDto>> typeDocsList = {};
for (int i = 0; i < dtoDoc->typeDocNb; i++)
{
    auto typedocDto = TypeDocDto::createShared();
    typedocDto->code = i;
    typedocDto->desciption = "foo";
    typeDocsList->emplace(typeDocsList->end(), typedocDto);
}
dtoDoc->typeDocs = typeDocsList;

但我无法在我的 typeDocsList 变量中添加任何内容。我添加的对象似乎总是 NULL。

我做错了什么?

【问题讨论】:

    标签: c++ dto oat++


    【解决方案1】:

    发现问题出在哪里。

    在声明列表对象时,oat++ 看起来有点笨拙。

    //*oatpp::List<oatpp::Object<TypeDocDto>> typeDocsList = {}* should become :
    oatpp::List<oatpp::Object<TypeDocDto>> typeDocsList({});
    

    似乎需要这种精确的语法。之后,我的代码按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2019-10-04
      • 1970-01-01
      相关资源
      最近更新 更多