How to persist a Value Object with EF

25 Views Asked by At

Good morning, I have a problem, I have this value object.

public record CodeVO : ValueObject
{
    public string MyCode { get; private set; }
 
    private CodeVO()
    {
        // EF constructor
    }
 
    private CodeVO(string myCode)
    {
        MyCode = myCode.Trim().ToUpper();
    }
 
    public static Result<CodeVO> Create(string code)
    {
        Result containWhitespacesCheckResult = CheckRule(new    CodeCannotContainWhitespacesRule(code));
        if (containWhitespacesCheckResult.IsFailed)
        {
            return containWhitespacesCheckResult;
        }
 
        Result maxLengthCheckResult = CheckRule(new CodeHasAllowedSizeRule(code));
        if (maxLengthCheckResult.IsFailed)
        {
            return maxLengthCheckResult;
        }
 
        return new CodeVO(code.Trim().ToUpper());
    }
}

This class is an attribute of an entity class. The issue is that when using the Result pattern and installing it using a static method, I don't know how to tell EF how to save it in the database and how to retrieve it without passing all the validations every time it is retrieved. The Result pattern is imperative and exceptions should not exist. I would appreciate your help. Thank you so much !

I have tried different EF configurations

0

There are 0 best solutions below