I will have multiple WCF component methods returning a common object. In some cases, fields A,B,C,D will be required to be non-null, but in other cases fields B,F,G,Q may be required to be non-null.
I want to indicate to whoever may be creating these methods which fields are required for any particular method. I’d also like to enforce these requirements as well.
I may not be able to change the common object – i.e. it’s defined by a different team.
Is there a pattern or strategy that can make this easy?
Thanks!
You need an adapter object that knows how to handle both (all) cases or (probably) better, multiple adapters that each know how to handle their own case.
If you don’t have control of the object, then you can’t enforce anything on its internals, all you can do is adapt to it in your own code. So the other team will still return the common object. What you do is in your code, create adapter objects that validate your required fields etc.
For example,
Your ‘SomeInternalBusinessMethod’ can then require specific fields, but you can’t really force the other team to return those fields, you can just check them in you code before using them.