vkhorikov/CSharpFunctionalExtensions

CS1061 - trying to simplify creation of ValueObjects - failed :(

p-bojkowski opened this issue · 3 comments

Hi all :)

I'm trying to simplify creating of custom ValueObjects.
But I failed. I missing documentation for CSharpFunctionalExtensions.

What is working:

base class:

    [Serializable]
    public abstract class ValueObjectAsString<T> : SimpleValueObject<string>
    {
        protected ValueObjectAsString(string value)
            : base(value)
        {
        }

        protected abstract int MinValueLength { get; }

        protected abstract int MaxValueLength { get; }

        public abstract Result<T> Create(Maybe<string> objectOrNothing);

        protected Result<string> ValidateValue(Maybe<string> objectOrNothing)
        {
            return objectOrNothing
                .ToResult(errorMessage: Errors.General.ValueIsRequired().Message)
                .Tap(currentValue => currentValue.Trim())
                .Ensure(currentValue => !string.IsNullOrEmpty(currentValue), "Value should not be empty")
                .Ensure(currentValue => currentValue.Length < this.MinValueLength, "Value is too short")
                .Ensure(currentValue => currentValue.Length <= this.MaxValueLength, "Value is too long")
                .Finally(result => result.IsSuccess ? result.Value : result.Error);
        }
    }

inheritance:

    public class AccountNumber : ValueObjectAsString<AccountNumber>
    {
        protected AccountNumber(string value)
            : base(value)
        {
        }

        protected override int MinValueLength => 5;

        protected override int MaxValueLength => 255;

        public override Result<AccountNumber> Create(Maybe<string> objectOrNothing)
        {
            var currentResult = this.ValidateValue(objectOrNothing);

            if (currentResult.IsSuccess)
            {
                return new AccountNumber(currentResult.Value);
            }

            return Result.Failure<AccountNumber>(currentResult.Error);
        }
    }

Works fine!

But when I will simplify AccountNumber - like:

    public class AccountNumber : ValueObjectAsString<AccountNumber>
    {
        protected AccountNumber(string value)
            : base(value)
        {
        }

        protected override int MinValueLength => 5;

        protected override int MaxValueLength => 255;

        public override Result<AccountNumber> Create(Maybe<string> objectOrNothing)
        {
            return ValidateValue(objectOrNothing)
                .Finally(result => result.IsSuccess ? Result.Success<AccountNumber>(new AccountNumber(result.Value)) : result.Error);
        }
    }

VS tells me:

image

Did I miss something? What is wrong?

This has issues too :(

image

Fixed my issue! :)

@p-bojkowski This should work:

public override Result<AccountNumber> Create(Maybe<string> objectOrNothing)
{
    var currentResult = this.ValidateValue(objectOrNothing);

    if (currentResult.IsSuccess)
    {
        return new AccountNumber(currentResult.Value);
    }

    return Result.Failure<AccountNumber>(currentResult.Error);
}

=>

public override Result<AccountNumber> Create(Maybe<string> objectOrNothing)
{
    return ValidateValue(objectOrNothing)
        .Map(x => new AccountNumber(x));
}