I've created a factory assembly that can process any type of transaction (that supports ITransactionType
). I can add more transaction types in separate assemblies, all of which will work with the factory without it needing to be recompiled (which is one of the benefits of a factory).
There is an actual list of transaction types that can be handled, so I've defined a static class with const strings, which is kept in a separate assembly. This is referenced by other assemblies to ensure consistent transaction type names. This assembly is not referenced by the factory, as it doesn't need to know, and it would result in unnecessary compilations of that project.
All this works just fine. I'll never reference the master list assembly in the factory project, because it know it doesn't make sense.
However, I'm concerned that if the project is being maintained by someone who doesn't understand all this, they may mistakenly add the reference to the master list assembly. Is there some way to prevent this reference from being added? IOW, if someone (perhaps even myself in the future) adds the reference, is there some way for me to force a compile error pointing to an explanation of why the behavior is unacceptable?
I can think of a few ways, though the language (.net, C#, vb) does not provide a specific method as far as I know:
#warning
to your source which is always issued: "should not be compiled with XXX", please make sure it's not in".Transaction
factory in your constants library, this will effectively prevent adding a reference the other way around.cctor
(the static constructor) using reflection and throw an exception if found. This will throw a hard exception (but only on any test) if anybody ever attempts to add a reference to this lib.MustNotReferenceXXX
and in its body, dynamically check whether the lib is referenced.I'll update if I can think of anything easier, though I think points 5 and 6 are easiest to implement and future proof.