Please see the code below:
public class Person { public DateTime DateOfBirth; public List<Offer> Offers = new List<Offer>(); public Person(DateTime DateOfBirth) { int age = DateTime.Now.Year-candidate.Year; if (DateTime.Now < candidate.AddYears(age)) age--; if (age < 18 || age > 99) throw new ArgumentException("Age must be greater than 18.", "DateOfBirth"); _dateOfBirth=dateOfBirth; } public void GetOffers(IOfferCalculator offerCalculator) { return offerCalculator.GetOffers(this); } public void AssignOffers(List<Offer> offers) { //Assign offers to Person.Offers here. } }
Notice that I pass the entire Person object to Person.GetOffers(). The reason I do this is because the person has to be over 21 for all of the offers and this is checked (validated) in the Person constructor. Is this a good reason to pass the Enquiry as an object to: offerCalculator.GetOffers? The reason I ask is because t3chb0t described this as a code smell in my question here: https://codereview.stackexchange.com/questions/186124/testing-the-process-of-assigning-offers-to-a-customer
Alternatively I could just pass the DateOfBirth like this: return offerCalculator.GetOffers(DateOfBirth). However, this would mean that I would have to validate the DateOfBirth in every offer.
I am trying to follow the principle of least astonishment these days and find myself overthinking a lot. My two questions are:
1) Is it a good appraoch to pass the Person object to offerCalculator.GetOffers as Person already validated the date of birth?
2) Should I be checking the date of birth against every Offer, even though every Offer requires the person to be over 21? I don’t think this will evert change i.e. the person will always have to be over 21.