如何将 DTO 映射到打字稿中的实体?

问题描述 投票:0回答:1

在我的项目中,我使用Vue + TypeScript和Axios从后端获取对象。所以,我有一个像这样的对象类型:

class Annoucement {
    id!: string;
    description?: string;
    deal?: Deal;
    realty?: Realty;
    user?: User; 
}

以及这样的回应:

{
  "id": "guid",
  "description": "string",
  "deal": {
     //...
  },
  "realty": {
     //...
  },
  "user": {
     //...
  }
}

主要问题是

deal
reality
可以具有不同的属性,这就是为什么我不能只使用这样的东西

let annoucement = new Annoucement();
annoucement.realty.floor = dto.realty.floor;
//...

编辑: 这些是我的课

    class Deal {
        private _id!: string;
    
        private _dealInfo!: Sell | Rent; 
    
        public get id(): string {
            return this._id;
        }
    
        public set id(value: string) {
            this._id = value;
        }
    
        public get dealInfo(): Sell | Rent {
            return this._dealInfo;
        }
    
        public set dealInfo(value: Sell | Rent) {
            this._dealInfo = value;
        }
    
        public constructor(id: string) {
            this.id = id;
        }
    }

    class Sell extends Deal {
    
        public readonly dealType: string = "Sell";
    
        private _conditions!: SellConditions;
    
        public get conditions(): SellConditions {
            return this._conditions;
        }
    
        public set conditions(value: SellConditions) {
            this._conditions = value;
        }
    
        public constructor(id: string, conditions: SellConditions) {
            super(id);
            this.conditions = conditions;
        }
    } 


    class SellConditions {
        private _id!: string;
    
        public get id(): string {
            return this._id;
        }
    
        public set id(value: string) {
            this._id = value;
        }
    
        private _price!: number;
    
        public get price(): number {
            return this._price;
        }
    
        public set price(value: number) {
            this._price = value;
        }
    
        private _type?: string | undefined;
    
        public get type(): string | undefined {
            return this._type;
        }
    
        public set type(value: string | undefined) {
            this._type = value;
        }
    
        private _yearInOwn!: number;
    
        public get yearInOwn(): number {
            return this._yearInOwn;
        }
    
        public set yearInOwn(value: number) {
            this._yearInOwn = value;
        }
    
        private _ownersCount!: number;
    
        public get ownersCount(): number {
            return this._ownersCount;
        }
    
        public set ownersCount(value: number) {
            this._ownersCount = value;
        }
    
        private _prescribersCount!: number;
    
        public get prescribersCount(): number {
            return this._prescribersCount;
        }
    
        public set prescribersCount(value: number) {
            this._prescribersCount = value;
        }
    
        private _haveChildOwners!: boolean;
    
        public get haveChildOwners(): boolean {
            return this._haveChildOwners;
        }
    
        public set haveChildOwners(value: boolean) {
            this._haveChildOwners = value;
        }
    
        private _haveChildPrescribers!: boolean;
    
        public get haveChildPrescribers(): boolean {
            return this._haveChildPrescribers;
        }
    
        public set haveChildPrescribers(value: boolean) {
            this._haveChildPrescribers = value;
        }
    }

    class Rent extends Deal {
        public readonly dealType: string = "Rell";
    
        private _conditions!: RentConditions;
    
        public get conditions(): RentConditions {
            return this._conditions;
        }
    
        public set conditions(value: RentConditions) {
            this._conditions = value;
        }
    
        public constructor(id: string, rentConditions: RentConditions) {
            super(id);
            this.conditions = rentConditions;
        }
    }
    
    class RentConditions {
        private _id!: string;
    
        public get id(): string {
            return this._id;
        }
    
        public set id(value: string) {
            this._id = value;
        }
    
        private _price!: number;
    
        public get price(): number {
            return this._price;
        }
    
        public set price(value: number) {
            this._price = value;
        }
    
        private _period?: string | undefined;
    
        public get period(): string | undefined {
            return this._period;
        }
    
        public set period(value: string | undefined) {
            this._period = value;
        }
    
        private _deposit!: number;
    
        public get deposit(): number {
            return this._deposit;
        }
    
        public set deposit(value: number) {
            this._deposit = value;
        }
    
        private _communalPays!: number;
    
        public get communalPays(): number {
            return this._communalPays;
        }
    
        public set communalPays(value: number) {
            this._communalPays = value;
        }
    
        private _prepay!: number;
    
        public get prepay(): number {
            return this._prepay;
        }
    
        public set prepay(value: number) {
            this._prepay = value;
        }
    
        private _facilities?: string | undefined;
    
        public get facilities(): string | undefined {
            return this._facilities;
        }
    
        public set facilities(value: string | undefined) {
            this._facilities = value;
        }
    
        private _withKids!: boolean;
    
        public get withKids(): boolean {
            return this._withKids;
        }
    
        public set withKids(value: boolean) {
            this._withKids = value;
        }
    
        private _withAnimals!: boolean;
    
        public get withAnimals(): boolean {
            return this._withAnimals;
        }
    
        public set withAnimals(value: boolean) {
            this._withAnimals = value;
        }
    
        private _canSmoke!: boolean;
    
        public get canSmoke(): boolean {
            return this._canSmoke;
        }
    
        public set canSmoke(value: boolean) {
            this._canSmoke = value;
        }
    }

    class Reality {
        private _id!: string;
    
        public get id(): string {
            return this._id;
        }
    
        public set id(value: string) {
            this._id = value;
        }
    
        private _area!: number;
    
        public get area(): number {
            return this._area;
        }
    
        public set area(value: number) {
            this._area = value;
        }
    
        private _type?: string | undefined;
    
        public get type(): string | undefined {
            return this._type;
        }
    
        public set type(value: string | undefined) {
            this._type = value;
        }
    
        public constructor(id: string, area: number, type?: string | undefined) {
            this.id = id;
            this.area = area;
            this.type = type;
        }
    }

class Office extends CommercialBuilding {

    private _name?: string | undefined;

    public get name(): string | undefined {
        return this._name;
    }
    public set name(value: string | undefined) {
        this._name = value;
    }

    private _roomsCount!: number;

    public get roomsCount(): number {
        return this._roomsCount;
    }

    public set roomsCount(value: number) {
        this._roomsCount = value;
    }
}
class CommercialBuilding extends Reality {

    private _floorsCount!: number;

    public get floorsCount(): number {
        return this._floorsCount;
    }

    public set floorsCount(value: number) {
        this._floorsCount = value;
    }

    private _entry!: boolean;

    public get entry(): boolean {
        return this._entry;
    }

    public set entry(value: boolean) {
        this._entry = value;
    }

    private _adress!: string;

    public get adress(): string {
        return this._adress;
    }

    public set adress(value: string) {
        this._adress = value;
    }

    private _isUse!: boolean;

    public get isUse(): boolean {
        return this._isUse;
    }

    public set isUse(value: boolean) {
        this._isUse = value;
    }

    private _isAcces!: boolean;

    public get isAcces(): boolean {
        return this._isAcces;
    }

    public set isAcces(value: boolean) {
        this._isAcces = value;
    }

    private _building?: Building | undefined;

    public get building(): Building | undefined {
        return this._building;
    }

    public set building(value: Building | undefined) {
        this._building = value;
    }

    class Building {
    
        private _id!: string;
    
        public get id(): string {
            return this._id;
        }
    
        public set id(value: string) {
            this._id = value;
        }
    
        private _class!: string;
    
        public get classBuilding(): string {
            return this._class;
        }
    
        public set classBuilding(value: string) {
            this._class = value;
        }
    
        private _buildingYear!: number;
    
        public get buildingYear(): number {
            return this._buildingYear;
        }
    
        public set buildingYear(value: number) {
            this._buildingYear = value;
        }
    
        private _centerName?: string | undefined;
    
        public get centerName(): string | undefined {
            return this._centerName;
        }
    
        public set centerName(value: string | undefined) {
            this._centerName = value;
        }
    
        private _haveParking!: boolean;
    
        public get haveParking(): boolean {
            return this._haveParking;
        }
    
        public set haveParking(value: boolean) {
            this._haveParking = value;
        }
    
        private _isEquipment!: boolean;
    
        public get isEquipment(): boolean {
            return this._isEquipment;
        }
    
        public set isEquipment(value: boolean) {
            this._isEquipment = value;
        }
    }

演示继承层次结构的类图

如您所见,当我想从数据库检索一些公告时,我可以获得不同类型的房地产。我不知道哪种方式是将响应映射到实体的最佳方式。

所以我只有两个解决方案:

  1. 使用
    if
    检查响应属性并定义类型
  2. 在不同的函数中获取响应,我将知道响应结构。

我尝试在Google中找到解决方案。但在大多数情况下,人们会问如何将实体映射到 DTO。

typescript mapping dto
1个回答
0
投票

我会添加一种不动产(可能是某种枚举?)来响应并使用 switch 语句

switch(response.type) { 
   case realty1: { 
      MapRealty1(r); 
      break; 
   } 
   case realty2: { 
      MapRealty2(); 
      break; 
   } 
   default: { 
      throw new Error('Unsupported realty type'); 
      break; 
   } 
} 

你的代码不可能从任何地方猜测出类型是什么 - 你需要以某种方式提供这些信息,因为你的前端现在没有你的后端类,这是我认为告诉它的最好方法

© www.soinside.com 2019 - 2024. All rights reserved.