0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

typescriptでクリーンアーキテクチャする

Last updated at Posted at 2021-03-05

typescriptと相性がいいです。
type句書かないのでスッキリします。
難しいことするとtype句は必要ですがrepositoryに隠蔽してしまえばアプリケーション層では全く書かないはずです。

Enterprise Business Rules

class ValueObject {
  constructor(private value: any) {}
  get() {
    return this.value;
  }
  eq(valueObject: this) {
    return this.value === valueObject.get();
  }
}

class Entity {
  constructor(
    private props: {
      id: ValueObject;
      name: string;
    }
  ) {}
  get id() {
    return this.props.id;
  }
  set name(value: string) {
    this.props.name = value;
  }
}

interface StreageInterface {
  save(entity: Entity): void;
  find(id: ValueObject): Entity;
}

Application Business Rules

/**
 * ユースケース: Entityを取得して名前を書き換え上書き永続化
 */
class Usecase {
  constructor(private repository: StreageInterface) {}
  invoke(id: ValueObject) {
    const entity = this.repository.find(id);
    entity.name = "山田 太郎";
    this.repository.save(entity);
  }
}

Interface Adapters


const storeage = [
  new Entity({
    id: new ValueObject(1),
    name: "unknown",
  }),
];

class Repository implements StreageInterface {
  save(entity: Entity) {
    storeage.push(entity);
  }
  find(id: ValueObject) {
    const entity = storeage.find((item) => item.id.eq(id));
    if (entity) {
      return entity;
    } else {
      throw new Error("entityが存在しない");
    }
  }
}

class Controller {
  patch(param: string) {
    new Usecase(new Repository()).invoke(new ValueObject(param));
  }
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?