Projecting Results

A specification gives you the facts that match a set of conditions. But sometimes you want to transform the facts into a different shape. You can do that with the LINQ select keyword.

var postsInSite = Given<Site>.Match((site, facts) =>
  from post in facts.OfType<Post>()
  where post.site == site
  select new
  {
    hash = jinagaClient.Hash(post),
    createdAt = post.createdAt
  }
);

When you select the hash or a field of a fact, the resulting value is immutable. That's because facts themselves are immutable. If you want to project something that changes, you can select a sub-specification.

var postsInSite = Given<Site>.Match((site, facts) =>
  from post in facts.OfType<Post>()
  where post.site == site
  select new
  {
    hash = jinagaClient.Hash(post),
    createdAt = post.createdAt,
    titles =
      from title in facts.OfType<PostTitle>()
      where title.post == post
      select title.value
  }
);

If you prefer method syntax, use the Select method to project a field.

var postsInSite = Given<Site>.Match((site, facts) =>
  facts.OfType<Post>(post => post.site == site)
    .Select(post => new
    {
      hash = jinagaClient.Hash(post),
      createdAt = post.createdAt,
      titles =
        facts.OfType<PostTitle>(title => title.post == post)
          .Select(title => title.value)
    })
);

Continue With

Modeling

Jinaga is a product of Jinaga LLC.

Michael L Perry, President