Search code examples
c#design-patternsmappingn-tier-architecture

Requiring a mapping class to resolve newly added fields


I'm working on a c# project having some tiers with data type encapsulations. But whenever I add a field to a model in a top level layer (say Application Service), I need to remember where else should I change to get my application working properly.

I'm looking for a pattern or method to prevent getting potential logical errors followed by not updating my mapping classes. I think if I can require my mapping classes to resolve newly added fields (for example by throwing an exception if they're not resolved), the problem will be solved.

So any idea for a solution? or how I can implement my own idea?


Solution

  • You can use a library like automapper that will give you an error if not all properties are mapped properly (http://docs.automapper.org/en/stable/Configuration-validation.html), plus it saves you from writing all the code to map each objects.

    If you don't want to use a library, make sure to wrap the mappings in factories so that, at least, the code is centralised and easily discoverable, but that's still error prone. Using constructors instead of object initialisers also helps finding mappings at compile time.