From 161f1fed09eb9934fddfd694a3b12e294da33bf9 Mon Sep 17 00:00:00 2001 From: Pc Date: Thu, 15 Jun 2023 01:56:27 +0330 Subject: [PATCH] refactor: Refactor validation of value object from constructor to Of method --- deployments/docker-compose/docker-compose.yaml | 5 +++-- deployments/docker-compose/infrastracture.yaml | 5 +++-- deployments/kubernetes/booking-microservices.yml | 4 ++-- .../Flight/Aircrafts/ValueObjects/AircraftId.cs | 10 +++++----- .../Aircrafts/ValueObjects/ManufacturingYear.cs | 14 ++++++++------ .../src/Flight/Aircrafts/ValueObjects/Model.cs | 15 +++++++++------ .../src/Flight/Aircrafts/ValueObjects/Name.cs | 15 +++++++++------ .../src/Flight/Airports/ValueObjects/Address.cs | 12 +++++++----- .../src/Flight/Airports/ValueObjects/AirportId.cs | 11 ++++++----- .../src/Flight/Airports/ValueObjects/Code.cs | 13 ++++++++----- .../src/Flight/Airports/ValueObjects/Name.cs | 15 +++++++++------ src/Services/Flight/src/Flight/EventMapper.cs | 2 +- .../src/Flight/Flights/ValueObjects/ArriveDate.cs | 9 +++++---- .../Flight/Flights/ValueObjects/DepartureDate.cs | 9 +++++---- .../Flights/ValueObjects/DurationMinutes.cs | 12 +++++++----- .../src/Flight/Flights/ValueObjects/FlightDate.cs | 9 +++++---- .../src/Flight/Flights/ValueObjects/FlightId.cs | 11 ++++++----- .../Flight/Flights/ValueObjects/FlightNumber.cs | 14 +++++++++----- .../src/Flight/Flights/ValueObjects/Price.cs | 12 +++++++----- .../src/Flight/Seats/ValueObjects/SeatId.cs | 12 ++++++------ .../src/Flight/Seats/ValueObjects/SeatNumber.cs | 12 +++++++----- .../src/Passenger/Passengers/ValueObjects/Age.cs | 14 ++++++++------ .../src/Passenger/Passengers/ValueObjects/Name.cs | 13 ++++++++----- .../Passengers/ValueObjects/PassengerId.cs | 10 +++++----- .../Passengers/ValueObjects/PassportNumber.cs | 10 +++++----- 25 files changed, 153 insertions(+), 115 deletions(-) diff --git a/deployments/docker-compose/docker-compose.yaml b/deployments/docker-compose/docker-compose.yaml index 80a6848..83a84f3 100644 --- a/deployments/docker-compose/docker-compose.yaml +++ b/deployments/docker-compose/docker-compose.yaml @@ -7,7 +7,7 @@ services: postgres: image: postgres:latest container_name: postgres - restart: on-failure + restart: unless-stopped ports: - '5432:5432' environment: @@ -30,6 +30,7 @@ services: # sql: # container_name: sql # image: mcr.microsoft.com/mssql/server + # restart: unless-stopped # ports: # - "1433:1433" # environment: @@ -76,7 +77,7 @@ services: eventstore: container_name: eventstore image: eventstore/eventstore:21.2.0-buster-slim - restart: on-failure + restart: unless-stopped environment: - EVENTSTORE_CLUSTER_SIZE=1 - EVENTSTORE_RUN_PROJECTIONS=All diff --git a/deployments/docker-compose/infrastracture.yaml b/deployments/docker-compose/infrastracture.yaml index de9c139..808c430 100644 --- a/deployments/docker-compose/infrastracture.yaml +++ b/deployments/docker-compose/infrastracture.yaml @@ -21,7 +21,7 @@ services: postgres: image: postgres:latest container_name: postgres - restart: on-failure + restart: unless-stopped ports: - '5432:5432' environment: @@ -43,6 +43,7 @@ services: # sql: # container_name: sql # image: mcr.microsoft.com/mssql/server + # restart: unless-stopped # ports: # - "1433:1433" # environment: @@ -77,7 +78,7 @@ services: eventstore: container_name: eventstore image: eventstore/eventstore:21.2.0-buster-slim - restart: on-failure + restart: unless-stopped environment: - EVENTSTORE_CLUSTER_SIZE=1 - EVENTSTORE_RUN_PROJECTIONS=All diff --git a/deployments/kubernetes/booking-microservices.yml b/deployments/kubernetes/booking-microservices.yml index 74f1ff5..204cec2 100644 --- a/deployments/kubernetes/booking-microservices.yml +++ b/deployments/kubernetes/booking-microservices.yml @@ -207,7 +207,7 @@ spec: - containerPort: 1113 - containerPort: 2113 resources: {} - restartPolicy: OnFailure + restartPolicy: Always status: {} --- apiVersion: v1 @@ -405,7 +405,7 @@ spec: name: postgres ports: - containerPort: 5432 - restartPolicy: OnFailure + restartPolicy: Always --- apiVersion: v1 kind: Service diff --git a/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/AircraftId.cs b/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/AircraftId.cs index c5bb6aa..db616d9 100644 --- a/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/AircraftId.cs +++ b/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/AircraftId.cs @@ -9,16 +9,16 @@ public record AircraftId private AircraftId(Guid value) { - if (value == Guid.Empty) - { - throw new InvalidAircraftIdExceptions(value); - } - Value = value; } public static AircraftId Of(Guid value) { + if (value == Guid.Empty) + { + throw new InvalidAircraftIdExceptions(value); + } + return new AircraftId(value); } diff --git a/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/ManufacturingYear.cs b/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/ManufacturingYear.cs index 61de900..de90311 100644 --- a/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/ManufacturingYear.cs +++ b/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/ManufacturingYear.cs @@ -1,21 +1,23 @@ namespace Flight.Aircrafts.ValueObjects; -using Flight.Aircrafts.Exceptions; +using Exceptions; public record ManufacturingYear { public int Value { get; } - public ManufacturingYear(int value) + + private ManufacturingYear(int value) + { + Value = value; + } + + public static ManufacturingYear Of(int value) { if (value < 1900) { throw new InvalidManufacturingYearException(); } - Value = value; - } - public static ManufacturingYear Of(int value) - { return new ManufacturingYear(value); } diff --git a/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/Model.cs b/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/Model.cs index c7f4510..eae73f5 100644 --- a/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/Model.cs +++ b/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/Model.cs @@ -1,20 +1,23 @@ namespace Flight.Aircrafts.ValueObjects; -using Flight.Aircrafts.Exceptions; +using Exceptions; public record Model { public string Value { get; } - public Model(string value) + + private Model(string value) + { + Value = value; + } + + public static Model Of(string value) { if (string.IsNullOrWhiteSpace(value)) { throw new InvalidModelException(); } - Value = value; - } - public static Model Of(string value) - { + return new Model(value); } diff --git a/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/Name.cs b/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/Name.cs index a93a68b..63a0fa4 100644 --- a/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/Name.cs +++ b/src/Services/Flight/src/Flight/Aircrafts/ValueObjects/Name.cs @@ -1,20 +1,23 @@ namespace Flight.Aircrafts.ValueObjects; -using Flight.Aircrafts.Exceptions; +using Exceptions; public record Name { public string Value { get; } - public Name(string value) + + private Name(string value) + { + Value = value; + } + + public static Name Of(string value) { if (string.IsNullOrWhiteSpace(value)) { throw new InvalidNameException(); } - Value = value; - } - public static Name Of(string value) - { + return new Name(value); } diff --git a/src/Services/Flight/src/Flight/Airports/ValueObjects/Address.cs b/src/Services/Flight/src/Flight/Airports/ValueObjects/Address.cs index a9b9aaa..04bbc7c 100644 --- a/src/Services/Flight/src/Flight/Airports/ValueObjects/Address.cs +++ b/src/Services/Flight/src/Flight/Airports/ValueObjects/Address.cs @@ -1,21 +1,23 @@ namespace Flight.Airports.ValueObjects; + using Exceptions; public class Address { public string Value { get; } - public Address(string value) + private Address(string value) { - if (string.IsNullOrWhiteSpace(value)) - { - throw new InvalidAddressException(); - } Value = value; } public static Address Of(string value) { + if (string.IsNullOrWhiteSpace(value)) + { + throw new InvalidAddressException(); + } + return new Address(value); } diff --git a/src/Services/Flight/src/Flight/Airports/ValueObjects/AirportId.cs b/src/Services/Flight/src/Flight/Airports/ValueObjects/AirportId.cs index 8f48019..db79a01 100644 --- a/src/Services/Flight/src/Flight/Airports/ValueObjects/AirportId.cs +++ b/src/Services/Flight/src/Flight/Airports/ValueObjects/AirportId.cs @@ -1,4 +1,5 @@ namespace Flight.Airports.ValueObjects; + using System; using Flight.Airports.Exceptions; @@ -8,16 +9,16 @@ public record AirportId private AirportId(Guid value) { - if (value == Guid.Empty) - { - throw new InvalidAirportIdExceptions(value); - } - Value = value; } public static AirportId Of(Guid value) { + if (value == Guid.Empty) + { + throw new InvalidAirportIdExceptions(value); + } + return new AirportId(value); } diff --git a/src/Services/Flight/src/Flight/Airports/ValueObjects/Code.cs b/src/Services/Flight/src/Flight/Airports/ValueObjects/Code.cs index 210a8d7..f53c297 100644 --- a/src/Services/Flight/src/Flight/Airports/ValueObjects/Code.cs +++ b/src/Services/Flight/src/Flight/Airports/ValueObjects/Code.cs @@ -5,16 +5,19 @@ using Exceptions; public record Code { public string Value { get; } - public Code(string value) + + private Code(string value) + { + Value = value; + } + + public static Code Of(string value) { if (string.IsNullOrWhiteSpace(value)) { throw new InvalidCodeException(); } - Value = value; - } - public static Code Of(string value) - { + return new Code(value); } diff --git a/src/Services/Flight/src/Flight/Airports/ValueObjects/Name.cs b/src/Services/Flight/src/Flight/Airports/ValueObjects/Name.cs index 5529c56..c66e500 100644 --- a/src/Services/Flight/src/Flight/Airports/ValueObjects/Name.cs +++ b/src/Services/Flight/src/Flight/Airports/ValueObjects/Name.cs @@ -1,20 +1,23 @@ namespace Flight.Airports.ValueObjects; -using Flight.Airports.Exceptions; +using Exceptions; public record Name { public string Value { get; } - public Name(string value) + + private Name(string value) + { + Value = value; + } + + public static Name Of(string value) { if (string.IsNullOrWhiteSpace(value)) { throw new InvalidNameException(); } - Value = value; - } - public static Name Of(string value) - { + return new Name(value); } diff --git a/src/Services/Flight/src/Flight/EventMapper.cs b/src/Services/Flight/src/Flight/EventMapper.cs index a5886ee..f8c8b82 100644 --- a/src/Services/Flight/src/Flight/EventMapper.cs +++ b/src/Services/Flight/src/Flight/EventMapper.cs @@ -35,7 +35,7 @@ public sealed class EventMapper : IEventMapper { return @event switch { - FlightCreatedDomainEvent e => new CreateFlightMongo(e.Id, e.FlightNumber, AircraftId.Of(e.AircraftId), e.DepartureDate, e.DepartureAirportId, + FlightCreatedDomainEvent e => new CreateFlightMongo(e.Id, e.FlightNumber, e.AircraftId, e.DepartureDate, e.DepartureAirportId, e.ArriveDate, e.ArriveAirportId, e.DurationMinutes, e.FlightDate, e.Status, e.Price, e.IsDeleted), FlightUpdatedDomainEvent e => new UpdateFlightMongo(e.Id, e.FlightNumber, e.AircraftId, e.DepartureDate, e.DepartureAirportId, e.ArriveDate, e.ArriveAirportId, e.DurationMinutes, e.FlightDate, e.Status, e.Price, e.IsDeleted), diff --git a/src/Services/Flight/src/Flight/Flights/ValueObjects/ArriveDate.cs b/src/Services/Flight/src/Flight/Flights/ValueObjects/ArriveDate.cs index 72fc181..539bc60 100644 --- a/src/Services/Flight/src/Flight/Flights/ValueObjects/ArriveDate.cs +++ b/src/Services/Flight/src/Flight/Flights/ValueObjects/ArriveDate.cs @@ -8,15 +8,16 @@ public record ArriveDate private ArriveDate(DateTime value) { - if (value == null) - { - throw new InvalidArriveDateExceptions(value); - } Value = value; } public static ArriveDate Of(DateTime value) { + if (value == default) + { + throw new InvalidArriveDateExceptions(value); + } + return new ArriveDate(value); } diff --git a/src/Services/Flight/src/Flight/Flights/ValueObjects/DepartureDate.cs b/src/Services/Flight/src/Flight/Flights/ValueObjects/DepartureDate.cs index a8af4a5..5321b7d 100644 --- a/src/Services/Flight/src/Flight/Flights/ValueObjects/DepartureDate.cs +++ b/src/Services/Flight/src/Flight/Flights/ValueObjects/DepartureDate.cs @@ -9,15 +9,16 @@ public record DepartureDate private DepartureDate(DateTime value) { - if (value == null) - { - throw new InvalidDepartureDateExceptions(value); - } Value = value; } public static DepartureDate Of(DateTime value) { + if (value == default) + { + throw new InvalidDepartureDateExceptions(value); + } + return new DepartureDate(value); } diff --git a/src/Services/Flight/src/Flight/Flights/ValueObjects/DurationMinutes.cs b/src/Services/Flight/src/Flight/Flights/ValueObjects/DurationMinutes.cs index ed66f0a..292b345 100644 --- a/src/Services/Flight/src/Flight/Flights/ValueObjects/DurationMinutes.cs +++ b/src/Services/Flight/src/Flight/Flights/ValueObjects/DurationMinutes.cs @@ -1,21 +1,23 @@ namespace Flight.Flights.ValueObjects; + using Exceptions; public class DurationMinutes { public decimal Value { get; } - public DurationMinutes(decimal value) + private DurationMinutes(decimal value) { - if (value < 0) - { - throw new InvalidDurationException(); - } Value = value; } public static DurationMinutes Of(decimal value) { + if (value < 0) + { + throw new InvalidDurationException(); + } + return new DurationMinutes(value); } diff --git a/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightDate.cs b/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightDate.cs index 6c426ed..ae1ba66 100644 --- a/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightDate.cs +++ b/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightDate.cs @@ -8,15 +8,16 @@ public record FlightDate private FlightDate(DateTime value) { - if (value == null) - { - throw new InvalidFlightDateExceptions(value); - } Value = value; } public static FlightDate Of(DateTime value) { + if (value == default) + { + throw new InvalidFlightDateExceptions(value); + } + return new FlightDate(value); } diff --git a/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightId.cs b/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightId.cs index e05855e..0de0e44 100644 --- a/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightId.cs +++ b/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightId.cs @@ -1,4 +1,5 @@ namespace Flight.Flights.ValueObjects; + using System; using Exceptions; @@ -8,16 +9,16 @@ public record FlightId private FlightId(Guid value) { - if (value == Guid.Empty) - { - throw new InvalidFlightIdExceptions(value); - } - Value = value; } public static FlightId Of(Guid value) { + if (value == Guid.Empty) + { + throw new InvalidFlightIdExceptions(value); + } + return new FlightId(value); } diff --git a/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightNumber.cs b/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightNumber.cs index e5384e7..b709d7d 100644 --- a/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightNumber.cs +++ b/src/Services/Flight/src/Flight/Flights/ValueObjects/FlightNumber.cs @@ -1,19 +1,23 @@ namespace Flight.Flights.ValueObjects; + using Exceptions; public record FlightNumber { public string Value { get; } - public FlightNumber(string value) + + private FlightNumber(string value) + { + Value = value; + } + + public static FlightNumber Of(string value) { if (string.IsNullOrWhiteSpace(value)) { throw new InvalidFlightNumberException(value); } - Value = value; - } - public static FlightNumber Of(string value) - { + return new FlightNumber(value); } diff --git a/src/Services/Flight/src/Flight/Flights/ValueObjects/Price.cs b/src/Services/Flight/src/Flight/Flights/ValueObjects/Price.cs index 8819961..d2fa93e 100644 --- a/src/Services/Flight/src/Flight/Flights/ValueObjects/Price.cs +++ b/src/Services/Flight/src/Flight/Flights/ValueObjects/Price.cs @@ -1,21 +1,23 @@ namespace Flight.Flights.ValueObjects; + using Flight.Flights.Exceptions; public class Price { public decimal Value { get; } - public Price(decimal value) + private Price(decimal value) { - if (value < 0) - { - throw new InvalidPriceException(); - } Value = value; } public static Price Of(decimal value) { + if (value < 0) + { + throw new InvalidPriceException(); + } + return new Price(value); } diff --git a/src/Services/Flight/src/Flight/Seats/ValueObjects/SeatId.cs b/src/Services/Flight/src/Flight/Seats/ValueObjects/SeatId.cs index 0269a4c..bd8c249 100644 --- a/src/Services/Flight/src/Flight/Seats/ValueObjects/SeatId.cs +++ b/src/Services/Flight/src/Flight/Seats/ValueObjects/SeatId.cs @@ -1,24 +1,24 @@ namespace Flight.Seats.ValueObjects; + using System; using Exceptions; - public record SeatId { public Guid Value { get; } private SeatId(Guid value) { - if (value == Guid.Empty) - { - throw new InvalidSeatIdExceptions(value); - } - Value = value; } public static SeatId Of(Guid value) { + if (value == Guid.Empty) + { + throw new InvalidSeatIdExceptions(value); + } + return new SeatId(value); } diff --git a/src/Services/Flight/src/Flight/Seats/ValueObjects/SeatNumber.cs b/src/Services/Flight/src/Flight/Seats/ValueObjects/SeatNumber.cs index 3c3e2e0..e6d4b2e 100644 --- a/src/Services/Flight/src/Flight/Seats/ValueObjects/SeatNumber.cs +++ b/src/Services/Flight/src/Flight/Seats/ValueObjects/SeatNumber.cs @@ -6,16 +6,18 @@ public record SeatNumber { public string Value { get; } - public SeatNumber(string value) + private SeatNumber(string value) + { + Value = value; + } + + public static SeatNumber Of(string value) { if (string.IsNullOrWhiteSpace(value)) { throw new InvalidSeatNumberException(); } - Value = value; - } - public static SeatNumber Of(string value) - { + return new SeatNumber(value); } diff --git a/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/Age.cs b/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/Age.cs index 7c43262..73524cf 100644 --- a/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/Age.cs +++ b/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/Age.cs @@ -1,21 +1,23 @@ namespace Passenger.Passengers.ValueObjects; -using Passenger.Passengers.Exceptions; +using Exceptions; public record Age { public int Value { get; } - public Age(int value) + + private Age(int value) + { + Value = value; + } + + public static Age Of(int value) { if (value <= 0) { throw new InvalidAgeException(); } - Value = value; - } - public static Age Of(int value) - { return new Age(value); } diff --git a/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/Name.cs b/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/Name.cs index a16e111..41c134f 100644 --- a/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/Name.cs +++ b/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/Name.cs @@ -5,16 +5,19 @@ using Passenger.Passengers.Exceptions; public record Name { public string Value { get; } - public Name(string value) + + private Name(string value) + { + Value = value; + } + + public static Name Of(string value) { if (string.IsNullOrWhiteSpace(value)) { throw new InvalidNameException(); } - Value = value; - } - public static Name Of(string value) - { + return new Name(value); } diff --git a/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/PassengerId.cs b/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/PassengerId.cs index 8a8dc16..f48d689 100644 --- a/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/PassengerId.cs +++ b/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/PassengerId.cs @@ -9,16 +9,16 @@ public record PassengerId private PassengerId(Guid value) { - if (value == Guid.Empty) - { - throw new InvalidPassengerIdExceptions(value); - } - Value = value; } public static PassengerId Of(Guid value) { + if (value == Guid.Empty) + { + throw new InvalidPassengerIdExceptions(value); + } + return new PassengerId(value); } diff --git a/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/PassportNumber.cs b/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/PassportNumber.cs index 41f31c3..9e4f922 100644 --- a/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/PassportNumber.cs +++ b/src/Services/Passenger/src/Passenger/Passengers/ValueObjects/PassportNumber.cs @@ -13,15 +13,16 @@ public record PassportNumber private PassportNumber(string value) { - if (string.IsNullOrWhiteSpace(value)) - { - throw new InvalidPassportNumberException(); - } Value = value; } public static PassportNumber Of(string value) { + if (string.IsNullOrWhiteSpace(value)) + { + throw new InvalidPassportNumberException(); + } + return new PassportNumber(value); } @@ -30,4 +31,3 @@ public record PassportNumber return passportNumber.Value; } } -