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:
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

View File

@ -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

View File

@ -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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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),

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
}
}