Pass two parameters in constructor when using the valueConversion

Viewed 33

I have this ValueObject:

public class RoleName : ValueObject<RoleName>
{
    public string RoleNameFa { get; private set; }
    public string RoleNameEn { get; private set; }

    public RoleName(string RoleNameFa, string RoleNameEn)
    {
        ValidateInvariant();
        this.RoleNameFa = RoleNameFa;
        this.RoleNameEn = RoleNameEn;
    }

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return RoleNameFa;
        yield return RoleNameEn;
    }

    public void ValidateInvariant()
    {
        if (string.IsNullOrWhiteSpace(RoleNameFa))
            throw new ArgumentException("The RoleNameFa Cannot be Null", nameof(RoleNameFa));

        if (Regex.IsMatch(RoleNameFa, @"^[\u0600-\u06FF]+$"))
            throw new ArgumentException("The RoleNameFa Mustbe the Persian", nameof(RoleNameEn));

        if (Regex.IsMatch(RoleNameEn, @"^[\u0000-\u007F]+$"))
            throw new ArgumentException("The RoleNameEn Mustbe the English", nameof(RoleNameEn));

        if (string.IsNullOrWhiteSpace(RoleNameEn))
            throw new ArgumentException("The RoleNameEn Cannot be Null", nameof(RoleNameEn));
    }
}

and I need to use the HasConversion in configuration:

public class RoleConfiguration : IEntityTypeConfiguration<Role>
{
    public void Configure(EntityTypeBuilder<Role> builder)
    {
        builder.Property(x => x.RoleName).HasConversion(x => x.RoleNameFa , c => new RoleName(c));
    }
}

but I must pass two parameters to the constructor of RoleName. How can I do that?

0 Answers
Related