Skip to content
This repository was archived by the owner on May 26, 2025. It is now read-only.
This repository was archived by the owner on May 26, 2025. It is now read-only.

Visually separating rules in specification #9

@bartoszlenar

Description

@bartoszlenar

Feature description

  • Specifications could be long and have nested inline specifications within. It looks very messy, sometimes even unreadable after a code cleanup (done by e.g., resharper).
  • Would be great to have something that could help with visually organizing the rules in the specification.

Feature in action

First variant; a property.

Specification<BookModel> bookSpecification = s => s
    .Optional()
    .And
    .Member(m => m.AuthorEmail, m => m.Optional().Email())
    .WithMessage("Test message")
    .WithExtraCode("Test code")
    .And
    .Member(m => m.Title, m => m.NotEmpty().LengthBetween(1, 100))
    .WithMessage("Test message")
    .WithExtraCode("Test code")
    .And
    .Member(m => m.Price, m => m.NonNegative());

Looks nice and cannot be taken as the rule or any part of the specification, because those are always functions. The flaw is that technically it needs to be available from all places. So this is legal:

Specification<BookModel> bookSpecification = s => s
    .Optional().And.And
    .And.And
    .Member(m => m.AuthorEmail, m => m.And.Optional().And.Email().And.And.And)
    .And.And
    .WithMessage("Test message")
    .And.And
    .And.And
    .WithExtraCode("Test code")
    .And.And
    .Member(m => m.Title, m => m.And.NotEmpty().And.LengthBetween(1, 100).And.And.And.And.And.And.And)
    .And.And.And.And.And.And.And
    .Member(m => m.Price, m => m.And.And.And.And.And.NonNegative().And.And.And.And)
    .And.And.And.And.And;

Second variant: a method:

Specification<BookModel> bookSpecification = s => s
    .Optional()
    .And()
    .Member(m => m.AuthorEmail, m => m.Optional().Email())
    .WithMessage("Test message")
    .WithExtraCode("Test code")
    .And()
    .Member(m => m.Title, m => m.NotEmpty().LengthBetween(1, 100))
    .WithMessage("Test message")
    .WithExtraCode("Test code")
    .And()
    .Member(m => m.Price, m => m.NonNegative());

Flaw; it might be mistaken for a rule (probably #8 needs to be finished first).
Advantage; fluent-api controls where it's allowed to place And()

Feature details

  • Property variant; a property in the specification interface.
  • Method variant: extension method, along with fluent api logic (e.g., it can be followed only by the rule commands)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions