Object value: Validation of parameters that must exist in the database

33 Views Asked by At

I have a problem with the implementation of Value Object.

If I've understood everything correctly, in the context of clean architecture :

1 - A Value Object is responsible for its validation, it cannot exist if it is not valid, this validation being done during its construction.

2 - A Value Object is located at the lowest level (at the center), so it cannot call/depend on the higher level (services/repositories).

My question is, if we consider these two things, how do we validate a Value Object that depends on an external source?

For example, if I take a Money Value Object made up of a Currency and a number that can't be negative

class Money {
    public Money(decimal value, Currency currency) {
        if(value <= 0)
            throw new ArgumentException();
        ....
    }
}

Currency can only exist if currency exists in my database, so I'll need to call on a repository to retrieve the data, which is located in the upper layers.

I often have this problem of validating a parameter that must exist in the database, how do you do it?

Thanks

0

There are 0 best solutions below