2 Replies to “Using NTestDataBuilder with classes that have inheritance”

  1. I can see the first version getting out of control very quickly but I can’t see how to make the second version work when you have multiple levels of inheritance. So for example if you subclassed SportsCar to ItalianSportsCar and wanted to make an ItalianSportsCarBuilder you wouldn’t be able to derive it from the SportsCarBuilder to take advantage of the WithExhaust method?

    Similarly if your base class isn’t abstract you can’t have two levels of builders.

    1. Yes – you need the base classes to be abstract for it to work.

      Perhaps a combination of the two approaches is in order?

      Depending on how much is shared with the base class then perhaps it makes sense to have some duplication in order to make it easier?

      Once you have an object hierarchy that gets that complex you are going to have to be more clever about how to solve it (which replicates similar problems with constructing the objects in production code). At the end of the day you always have the option of a more traditional builder available to you, although I suspect that would lead to even more repetition and code…

Leave a Reply

Your email address will not be published. Required fields are marked *