Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For an attribute which need to be validated, lets say for an entity we have country field as VO
This country field needs to be validated to be alpha-3 code as per some business logic required by domain expert.

***We need to persist this country data as it can have other values also and possible in future there can be addition, updating and deleting of the country persisted data***

What I have tried:

**Case 1.**

**Doing validation in application layer:**

If we call repository `countryRepo.getCountryByCountryAlpha3Code()` in application layer and then if the value is correct and valid part of system we can then pass the `createValidEntity()` and if not then can throw the error directly in application layer use-case. 

**Issue:**
 - This validation will be repeated in multiple use-case if same validation need to be checked in other use-cases if its application layer concern
 - Here the business logic is now a part of application service layer  
 

**Case 2**

Validating the country code in its value object class or domain service in Domain Layer

Doing this will keep business logic inside domain layer and also won't violate DRY principle.


    import { ValueObject } from '@shared/core/domain/ValueObject';
    import { Result } from '@shared/core/Result';
    import { Utils } from '@shared/utils/Utils';
    
    interface CountryAlpha3CodeProps {
      value: string;
    }
    
    export class CountryAlpha3Code extends ValueObject<CountryAlpha3CodeProps> {
      // Case Insensitive String. Only printable ASCII allowed. (Non-printable characters like: Carriage returns, Tabs, Line breaks, etc are not allowed)
    
      get value(): string {
        return this.props.value;
      }
    
      private constructor(props: CountryAlpha3CodeProps) {
        super(props);
      }
    
      public static create(value: string): Result<CountryAlpha3Code> {
        
        
        return Result.ok<CountryAlpha3Code>(new CountryAlpha3Code({ value: value }));
      }
    }

 - Is it good to call the repository from inside domain layer (Service
   or VO (not recommended) ) then dependency flow will change?
   
 - If we trigger event how to make it synchronous?

 - What are some better ways to solve this?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900