Last active
August 29, 2015 14:08
-
-
Save chrismckelt/67610b791ed0238467fd to your computer and use it in GitHub Desktop.
Strong typed anon objects from lambda expressions - sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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