Skip to content

How to use "DomainType<SELF, REPR>" with complex values #1357

@HernanFAR

Description

@HernanFAR

As far as I see, the most ideal use case of DomainType, is for value objects that are only a wrapper around one property, like this:

public sealed class NonEmptyString : DomainType<NonEmptyString, string>
{
    private readonly string _value;

    private NonEmptyString(string value)
    {
        _value = value;
    }

    public override string ToString() => _value;

    public static NonEmptyString From(string repr) =>
        string.IsNullOrEmpty(repr) is false
            ? throw new InvalidOperationException("Value cannot be empty or null")
            : new NonEmptyString(repr);

    public string To() => _value;
}
```c#

But, how to we use DomainType with more complex value objects, like this one? 

```c#
public sealed class OfficeApprover : DomainType<OfficeApprover, (InternalUserId InternalUserId, string TextA, string TextB)>
{
    private OfficeApprover(InternalUserId internalUserId, string textA, string textB)
    {
        InternalUserId = internalUserId;
        TextA = textA;
        TextB = textB;
    }

    public InternalUserId InternalUserId { get; }

    public string TextA { get; }

    public string TextB { get; }


    public static OfficeApprover From((InternalUserId InternalUserId, string TextA, string TextB) value) 
        => new(value.InternalUserId, value.TextA, value.TextB);

    public (InternalUserId InternalUserId, string TextA, string TextB) To() => (InternalUserId, TextA, TextB);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions