refactor: Refactor validation of value object from constructor to Of method

This commit is contained in:
Pc 2023-06-15 01:56:27 +03:30
parent ef53893263
commit 161f1fed09
25 changed files with 153 additions and 115 deletions

View File

@ -7,7 +7,7 @@ services:
postgres: postgres:
image: postgres:latest image: postgres:latest
container_name: postgres container_name: postgres
restart: on-failure restart: unless-stopped
ports: ports:
- '5432:5432' - '5432:5432'
environment: environment:
@ -30,6 +30,7 @@ services:
# sql: # sql:
# container_name: sql # container_name: sql
# image: mcr.microsoft.com/mssql/server # image: mcr.microsoft.com/mssql/server
# restart: unless-stopped
# ports: # ports:
# - "1433:1433" # - "1433:1433"
# environment: # environment:
@ -76,7 +77,7 @@ services:
eventstore: eventstore:
container_name: eventstore container_name: eventstore
image: eventstore/eventstore:21.2.0-buster-slim image: eventstore/eventstore:21.2.0-buster-slim
restart: on-failure restart: unless-stopped
environment: environment:
- EVENTSTORE_CLUSTER_SIZE=1 - EVENTSTORE_CLUSTER_SIZE=1
- EVENTSTORE_RUN_PROJECTIONS=All - EVENTSTORE_RUN_PROJECTIONS=All

View File

@ -21,7 +21,7 @@ services:
postgres: postgres:
image: postgres:latest image: postgres:latest
container_name: postgres container_name: postgres
restart: on-failure restart: unless-stopped
ports: ports:
- '5432:5432' - '5432:5432'
environment: environment:
@ -43,6 +43,7 @@ services:
# sql: # sql:
# container_name: sql # container_name: sql
# image: mcr.microsoft.com/mssql/server # image: mcr.microsoft.com/mssql/server
# restart: unless-stopped
# ports: # ports:
# - "1433:1433" # - "1433:1433"
# environment: # environment:
@ -77,7 +78,7 @@ services:
eventstore: eventstore:
container_name: eventstore container_name: eventstore
image: eventstore/eventstore:21.2.0-buster-slim image: eventstore/eventstore:21.2.0-buster-slim
restart: on-failure restart: unless-stopped
environment: environment:
- EVENTSTORE_CLUSTER_SIZE=1 - EVENTSTORE_CLUSTER_SIZE=1
- EVENTSTORE_RUN_PROJECTIONS=All - EVENTSTORE_RUN_PROJECTIONS=All

View File

@ -207,7 +207,7 @@ spec:
- containerPort: 1113 - containerPort: 1113
- containerPort: 2113 - containerPort: 2113
resources: {} resources: {}
restartPolicy: OnFailure restartPolicy: Always
status: {} status: {}
--- ---
apiVersion: v1 apiVersion: v1
@ -405,7 +405,7 @@ spec:
name: postgres name: postgres
ports: ports:
- containerPort: 5432 - containerPort: 5432
restartPolicy: OnFailure restartPolicy: Always
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service

View File

@ -9,16 +9,16 @@ public record AircraftId
private AircraftId(Guid value) private AircraftId(Guid value)
{ {
if (value == Guid.Empty)
{
throw new InvalidAircraftIdExceptions(value);
}
Value = value; Value = value;
} }
public static AircraftId Of(Guid value) public static AircraftId Of(Guid value)
{ {
if (value == Guid.Empty)
{
throw new InvalidAircraftIdExceptions(value);
}
return new AircraftId(value); return new AircraftId(value);
} }

View File

@ -1,21 +1,23 @@
namespace Flight.Aircrafts.ValueObjects; namespace Flight.Aircrafts.ValueObjects;
using Flight.Aircrafts.Exceptions; using Exceptions;
public record ManufacturingYear public record ManufacturingYear
{ {
public int Value { get; } public int Value { get; }
public ManufacturingYear(int value)
private ManufacturingYear(int value)
{
Value = value;
}
public static ManufacturingYear Of(int value)
{ {
if (value < 1900) if (value < 1900)
{ {
throw new InvalidManufacturingYearException(); throw new InvalidManufacturingYearException();
} }
Value = value;
}
public static ManufacturingYear Of(int value)
{
return new ManufacturingYear(value); return new ManufacturingYear(value);
} }

View File

@ -1,20 +1,23 @@
namespace Flight.Aircrafts.ValueObjects; namespace Flight.Aircrafts.ValueObjects;
using Flight.Aircrafts.Exceptions; using Exceptions;
public record Model public record Model
{ {
public string Value { get; } public string Value { get; }
public Model(string value)
private Model(string value)
{
Value = value;
}
public static Model Of(string value)
{ {
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
{ {
throw new InvalidModelException(); throw new InvalidModelException();
} }
Value = value;
}
public static Model Of(string value)
{
return new Model(value); return new Model(value);
} }

View File

@ -1,20 +1,23 @@
namespace Flight.Aircrafts.ValueObjects; namespace Flight.Aircrafts.ValueObjects;
using Flight.Aircrafts.Exceptions; using Exceptions;
public record Name public record Name
{ {
public string Value { get; } public string Value { get; }
public Name(string value)
private Name(string value)
{
Value = value;
}
public static Name Of(string value)
{ {
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
{ {
throw new InvalidNameException(); throw new InvalidNameException();
} }
Value = value;
}
public static Name Of(string value)
{
return new Name(value); return new Name(value);
} }

View File

@ -1,21 +1,23 @@
namespace Flight.Airports.ValueObjects; namespace Flight.Airports.ValueObjects;
using Exceptions; using Exceptions;
public class Address public class Address
{ {
public string Value { get; } public string Value { get; }
public Address(string value) private Address(string value)
{ {
if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidAddressException();
}
Value = value; Value = value;
} }
public static Address Of(string value) public static Address Of(string value)
{ {
if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidAddressException();
}
return new Address(value); return new Address(value);
} }

View File

@ -1,4 +1,5 @@
namespace Flight.Airports.ValueObjects; namespace Flight.Airports.ValueObjects;
using System; using System;
using Flight.Airports.Exceptions; using Flight.Airports.Exceptions;
@ -8,16 +9,16 @@ public record AirportId
private AirportId(Guid value) private AirportId(Guid value)
{ {
if (value == Guid.Empty)
{
throw new InvalidAirportIdExceptions(value);
}
Value = value; Value = value;
} }
public static AirportId Of(Guid value) public static AirportId Of(Guid value)
{ {
if (value == Guid.Empty)
{
throw new InvalidAirportIdExceptions(value);
}
return new AirportId(value); return new AirportId(value);
} }

View File

@ -5,16 +5,19 @@ using Exceptions;
public record Code public record Code
{ {
public string Value { get; } public string Value { get; }
public Code(string value)
private Code(string value)
{
Value = value;
}
public static Code Of(string value)
{ {
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
{ {
throw new InvalidCodeException(); throw new InvalidCodeException();
} }
Value = value;
}
public static Code Of(string value)
{
return new Code(value); return new Code(value);
} }

View File

@ -1,20 +1,23 @@
namespace Flight.Airports.ValueObjects; namespace Flight.Airports.ValueObjects;
using Flight.Airports.Exceptions; using Exceptions;
public record Name public record Name
{ {
public string Value { get; } public string Value { get; }
public Name(string value)
private Name(string value)
{
Value = value;
}
public static Name Of(string value)
{ {
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
{ {
throw new InvalidNameException(); throw new InvalidNameException();
} }
Value = value;
}
public static Name Of(string value)
{
return new Name(value); return new Name(value);
} }

View File

@ -35,7 +35,7 @@ public sealed class EventMapper : IEventMapper
{ {
return @event switch 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), 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, 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), e.ArriveDate, e.ArriveAirportId, e.DurationMinutes, e.FlightDate, e.Status, e.Price, e.IsDeleted),

View File

@ -8,15 +8,16 @@ public record ArriveDate
private ArriveDate(DateTime value) private ArriveDate(DateTime value)
{ {
if (value == null)
{
throw new InvalidArriveDateExceptions(value);
}
Value = value; Value = value;
} }
public static ArriveDate Of(DateTime value) public static ArriveDate Of(DateTime value)
{ {
if (value == default)
{
throw new InvalidArriveDateExceptions(value);
}
return new ArriveDate(value); return new ArriveDate(value);
} }

View File

@ -9,15 +9,16 @@ public record DepartureDate
private DepartureDate(DateTime value) private DepartureDate(DateTime value)
{ {
if (value == null)
{
throw new InvalidDepartureDateExceptions(value);
}
Value = value; Value = value;
} }
public static DepartureDate Of(DateTime value) public static DepartureDate Of(DateTime value)
{ {
if (value == default)
{
throw new InvalidDepartureDateExceptions(value);
}
return new DepartureDate(value); return new DepartureDate(value);
} }

View File

@ -1,21 +1,23 @@
namespace Flight.Flights.ValueObjects; namespace Flight.Flights.ValueObjects;
using Exceptions; using Exceptions;
public class DurationMinutes public class DurationMinutes
{ {
public decimal Value { get; } public decimal Value { get; }
public DurationMinutes(decimal value) private DurationMinutes(decimal value)
{ {
if (value < 0)
{
throw new InvalidDurationException();
}
Value = value; Value = value;
} }
public static DurationMinutes Of(decimal value) public static DurationMinutes Of(decimal value)
{ {
if (value < 0)
{
throw new InvalidDurationException();
}
return new DurationMinutes(value); return new DurationMinutes(value);
} }

View File

@ -8,15 +8,16 @@ public record FlightDate
private FlightDate(DateTime value) private FlightDate(DateTime value)
{ {
if (value == null)
{
throw new InvalidFlightDateExceptions(value);
}
Value = value; Value = value;
} }
public static FlightDate Of(DateTime value) public static FlightDate Of(DateTime value)
{ {
if (value == default)
{
throw new InvalidFlightDateExceptions(value);
}
return new FlightDate(value); return new FlightDate(value);
} }

View File

@ -1,4 +1,5 @@
namespace Flight.Flights.ValueObjects; namespace Flight.Flights.ValueObjects;
using System; using System;
using Exceptions; using Exceptions;
@ -8,16 +9,16 @@ public record FlightId
private FlightId(Guid value) private FlightId(Guid value)
{ {
if (value == Guid.Empty)
{
throw new InvalidFlightIdExceptions(value);
}
Value = value; Value = value;
} }
public static FlightId Of(Guid value) public static FlightId Of(Guid value)
{ {
if (value == Guid.Empty)
{
throw new InvalidFlightIdExceptions(value);
}
return new FlightId(value); return new FlightId(value);
} }

View File

@ -1,19 +1,23 @@
namespace Flight.Flights.ValueObjects; namespace Flight.Flights.ValueObjects;
using Exceptions; using Exceptions;
public record FlightNumber public record FlightNumber
{ {
public string Value { get; } public string Value { get; }
public FlightNumber(string value)
private FlightNumber(string value)
{
Value = value;
}
public static FlightNumber Of(string value)
{ {
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
{ {
throw new InvalidFlightNumberException(value); throw new InvalidFlightNumberException(value);
} }
Value = value;
}
public static FlightNumber Of(string value)
{
return new FlightNumber(value); return new FlightNumber(value);
} }

View File

@ -1,21 +1,23 @@
namespace Flight.Flights.ValueObjects; namespace Flight.Flights.ValueObjects;
using Flight.Flights.Exceptions; using Flight.Flights.Exceptions;
public class Price public class Price
{ {
public decimal Value { get; } public decimal Value { get; }
public Price(decimal value) private Price(decimal value)
{ {
if (value < 0)
{
throw new InvalidPriceException();
}
Value = value; Value = value;
} }
public static Price Of(decimal value) public static Price Of(decimal value)
{ {
if (value < 0)
{
throw new InvalidPriceException();
}
return new Price(value); return new Price(value);
} }

View File

@ -1,24 +1,24 @@
namespace Flight.Seats.ValueObjects; namespace Flight.Seats.ValueObjects;
using System; using System;
using Exceptions; using Exceptions;
public record SeatId public record SeatId
{ {
public Guid Value { get; } public Guid Value { get; }
private SeatId(Guid value) private SeatId(Guid value)
{ {
if (value == Guid.Empty)
{
throw new InvalidSeatIdExceptions(value);
}
Value = value; Value = value;
} }
public static SeatId Of(Guid value) public static SeatId Of(Guid value)
{ {
if (value == Guid.Empty)
{
throw new InvalidSeatIdExceptions(value);
}
return new SeatId(value); return new SeatId(value);
} }

View File

@ -6,16 +6,18 @@ public record SeatNumber
{ {
public string Value { get; } public string Value { get; }
public SeatNumber(string value) private SeatNumber(string value)
{
Value = value;
}
public static SeatNumber Of(string value)
{ {
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
{ {
throw new InvalidSeatNumberException(); throw new InvalidSeatNumberException();
} }
Value = value;
}
public static SeatNumber Of(string value)
{
return new SeatNumber(value); return new SeatNumber(value);
} }

View File

@ -1,21 +1,23 @@
namespace Passenger.Passengers.ValueObjects; namespace Passenger.Passengers.ValueObjects;
using Passenger.Passengers.Exceptions; using Exceptions;
public record Age public record Age
{ {
public int Value { get; } public int Value { get; }
public Age(int value)
private Age(int value)
{
Value = value;
}
public static Age Of(int value)
{ {
if (value <= 0) if (value <= 0)
{ {
throw new InvalidAgeException(); throw new InvalidAgeException();
} }
Value = value;
}
public static Age Of(int value)
{
return new Age(value); return new Age(value);
} }

View File

@ -5,16 +5,19 @@ using Passenger.Passengers.Exceptions;
public record Name public record Name
{ {
public string Value { get; } public string Value { get; }
public Name(string value)
private Name(string value)
{
Value = value;
}
public static Name Of(string value)
{ {
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
{ {
throw new InvalidNameException(); throw new InvalidNameException();
} }
Value = value;
}
public static Name Of(string value)
{
return new Name(value); return new Name(value);
} }

View File

@ -9,16 +9,16 @@ public record PassengerId
private PassengerId(Guid value) private PassengerId(Guid value)
{ {
if (value == Guid.Empty)
{
throw new InvalidPassengerIdExceptions(value);
}
Value = value; Value = value;
} }
public static PassengerId Of(Guid value) public static PassengerId Of(Guid value)
{ {
if (value == Guid.Empty)
{
throw new InvalidPassengerIdExceptions(value);
}
return new PassengerId(value); return new PassengerId(value);
} }

View File

@ -13,15 +13,16 @@ public record PassportNumber
private PassportNumber(string value) private PassportNumber(string value)
{ {
if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidPassportNumberException();
}
Value = value; Value = value;
} }
public static PassportNumber Of(string value) public static PassportNumber Of(string value)
{ {
if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidPassportNumberException();
}
return new PassportNumber(value); return new PassportNumber(value);
} }
@ -30,4 +31,3 @@ public record PassportNumber
return passportNumber.Value; return passportNumber.Value;
} }
} }