Skip to content

Change the Status to Failed when any ValidationError is added to the Result. #161

@Ewerton

Description

@Ewerton

I'm new using the Result so, first of all: congratulation and thanks for this amazing lib.

I like to develop my methods using the following pattern:

  • Instantiate the return of the method, in this case, the Result.
  • Run through the method evaluating business rules and adding errors to the Result (if applicable).
  • Return the Result at the end of the method

Seems like the Result don't like this approach because it forces me to define the Status at the creation of the Result, like so:

  • Result<Customer> result = Result.Success(customer);
  • Result<Customer> result = Result.Error("Some Error");
  • Result<Customer> result = Result.Invalid(new ValidationError("Some Error"));

Although, I tried to follow the pattern and discovered that I can instantiate the Result without defining the Status using new Result(...) but I found a "strange" behavior, here is the code:

public static void SomeMethod()
{
    Customer customer = new Customer() { Name = "", Email = "some@email.com" };
    Result<Customer> result = new Result<Customer>(customer); // I can instantiate a Result without defining the status and I know that the default is *Ok*
    var valueBefore = result.IsSuccess; // Correct. I didn't added any error yet
    result.ValidationErrors.Add(new ValidationError("Error 1"));
    var valueAfter = result.IsSuccess; // Incorrect. I added an error, so it should be false
}

I don't know if this is a bug but it would be more concise if the Status get changed to something like Invalid ou Error when any ValidationError is added to the Result.
Is it possible or plausible?

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