DEV Community


Discussion on: Consuming APIs in Angular – The Model-Adapter Pattern

enjoiful profile image
Dan Strengier

Great article, thanks!

Can you help me understand the pros and cons of the following approach, compared to the adapter approach?

In the following code (which I did not write), you can see the author is mapping an unknown object to the Hero class. If the API changes, they can simply change the constructor mapping, and keep the Hero properties as is.

Why not just put all of that adapter logic in the model's constructor?

export class Hero  {
  id: string;
  name: string;
  alterEgo: string;
  likes: number;
  default: boolean;
  avatarUrl: string;
  avatarBlurredUrl: string;
  avatarThumbnailUrl: string;

  constructor(hero: any = {}) { =; = || '';
    this.alterEgo = hero.alterEgo || '';
    this.likes = hero.likes || 0;
    this.default = hero.default || false;
    this.avatarUrl = hero.avatarUrl || '';
    this.avatarBlurredUrl = hero.avatarBlurredUrl || '';
    this.avatarThumbnailUrl = hero.avatarThumbnailUrl || '';
Forem Open with the Forem app