使用Domain-Driven创建Hypermedia API (2)

AirportTransfer为Maybe<T>类型,意味着在一个完整的Booking中,可以不选择接送机服务(TransferService);反之对于Trip属性而言,即便从语言层面上来讲他也是引用类型,也可以为null,但是一个包含空Trip的Booking是不存在的,所以一个完整的Domain里,一旦一个非Maybe<T>类型的属性为null,那我们就可以认为这个Booking就是无效的;

该类的构造函数被修饰为private,意味着Booking只能通过选择可用的航班来创建,代码的含义诠释了业务需求

Checkin被设计为Sub-Domain,因为Checkin的实现过程略微复杂,是否是一个Sub-Domain取决于设计;

public class Booking { public Guid Id { get; } public IReadOnlyList<Passenger> Passengers => _passengers.AsReadOnly(); public Trip Trip { get; } public IReadOnlyList<Maybe<Seat>> Seats => _passengers.Select(p => p.SelectedSeat).ToList().AsReadOnly(); public Maybe<AirportTransfer> AirportTransfer { get; private set; } private readonly List<Passenger> _passengers; private readonly CheckinProcess _checkinProcess; private Booking(Trip trip, List<Passenger> passengers) { Id = Guid.NewGuid(); _checkinProcess = CheckinProcess.CreateCheckinProcess(this); Trip = trip; _passengers = passengers; } public static Booking SelectTrip(Trip trip, List<Passenger> passengers) { //Validation for trip and passengers in here var booking = new Booking(trip, passengers); return booking; } public void ChangeFlight(Trip.Journey journey, Flight flight) { // Checking is it eligible for changing flight; Trip.ChangeFlight(journey.Id, flight); } public void AssignSeat(Seat seat, Passenger passenger) { //Validation in here var p = _passengers.Single(s => s.Name.Equals(passenger.Name)); p.AssignSeat(seat); } //... Other capabilities }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxpfz.html