Modéliser votre
domaine métier
grâce aux types

@jgrenat@bsky.social

Slides : bit.ly/metier-types

Type ?

Boolean ?

True
False

Number ?

1
12347
2
0.5664908

Infinity

String ?

"1"
"Devoxx"
""

"Elm"

"笑う門 には 福来たる"

"🐣"

Type = ensemble de valeurs possibles

Boolean

True
False

2 valeurs

Number = valeurs

String =  valeurs

Cardinalité

Domaine métier ?

L’ensemble des connaissances, règles, processus et concepts spécifiques liés à l’activité

La gestion de réservations de salles et de services pour des réunions

  • Booking
  • Product (salle ou service)
  • Customer
type Booking = {
  items: { 
    product: Product, 
    discount?: Discount 
  }[];
  customer: Customer;
  status: string;
  paymentDate?: DateTime;
}
export type Product = {
  id: string;
  name: string;
  type: string;
  unitPrice: number;
  details: any;
}
export type Discount = {
  type: string;
  value: number;
}
export type Customer = {
  id: string;
  email: string;
  address: string;
}

Modélisation

Métier

2

2

Card(modelisation) == Card(business)

Primitive Obsession

export type Discount = {
  type: 'percentage' | 'fixed';
  value: number;
}
export type Discount = {
  isPercentage: boolean;
  value: number;
}

Encapsulation

Value Objects

Discriminated
Union

Types algébriques

export type Customer = {
  id: CustomerId;
  email: Email;
  address: string;
};
export type Discount = {
  type: "percentage" | "fixed" | "free";
  value: number;
};
export type Product = RoomProduct | ServiceProduct;

type RoomProduct = {
  id: ProductId;
  name: string;
  type: "room";
  unitPrice: number;
  details: { from: BookingTime; to: BookingTime; roomId: RoomId };
};

type ServiceProduct = {
  id: ProductId;
  name: string;
  type: "service";
  unitPrice: number;
  details: { serviceId: ServiceId; count: number };
};
export type Booking = DraftBooking | ValidatedBooking | PaidBooking;

type DraftBooking = {
  items: BookingItem[];
  customer?: {
    email?: string;
    address?: string;
  };
  status: "draft";
};

type ValidatedBooking = {
  items: NonEmptyArray<BookingItem>;
  customer: Customer;
  status: "validated";
};

type PaidBooking = {
  // ...
  paymentDate: DateTime;
};
  • Unions explicites
  • Value object
  • Types brandés
  • Types algébriques

Si c'est important pour votre métier, faites-en un type

Merci !

@jgrenat@bsky.social

Slides : bit.ly/metier-types

Liens

Slides : bit.ly/metier-types

@jgrenat@bsky.social

Slides : bit.ly/metier-types

@jgrenat@bsky.social

Modéliser votre domaine métier grâce aux types

By ereold

Modéliser votre domaine métier grâce aux types

  • 129