Skip to content

Instantly share code, notes, and snippets.

@chrismckelt
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismckelt/67610b791ed0238467fd to your computer and use it in GitHub Desktop.
Save chrismckelt/67610b791ed0238467fd to your computer and use it in GitHub Desktop.
Strong typed anon objects from lambda expressions - sample
[Fact]
public void PopulatesFranchiseSummary()
{
When(_ => _handler.Handle(_createdEvent));
Then(session =>
{
var loanContract = session.Get<PersonalLoanContract>(_createdEvent.AggregateId);
//original - manual anon
var expectedOriginal = new
{
_createdEvent.LoanApplicationId,
AssessmentId = _createdEvent.AggregateId,
_createdEvent.CustomerId,
DocumentReadModel = new
{
Loan = new
{
Franchise = new
{
_franchise.DisplayName,
_franchise.LegalName,
_franchise.LendersAbn,
_franchise.LendersAustralianCreditLicence
}
}
}
};
// half lambda -- half anon
ExpectedObject expected1 =
_createdEvent
.ToDto(
a => a.LoanApplicationId,
a => new {CustomerId=a.AggregateId}, // notice anon property name set
a => a.CustomerId,
_ => new
{
DocumentReadModel = new
{
Loan = new
{
Franchise = new
{
_franchise.DisplayName,
_franchise.LegalName,
_franchise.LendersAbn,
_franchise.LendersAustralianCreditLicence
}
}
}
});
// full lambda -- strongly typeds anon object creation
ExpectedObject expected2 = _createdEvent
.ToDto(
a => a.LoanApplicationId,
a => a.AggregateId.WithName("AssessmentId"),
a => a.CustomerId,
a =>
_franchise.ToDto(
b => new
{
DocumentReadModel = new
{
Loan = new
{
Franchise = b.DisplayName,
b.LegalName,
b.LendersAbn,
b.LendersAustralianCreditLicence
}
}
}));
expectedOriginal.ToExpectedObject().ShouldMatch(loanContract);
expected1.ToExpectedObject().ShouldMatch(loanContract);
expected2.ToExpectedObject().ShouldMatch(loanContract);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment