views:

29

answers:

1

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?

+1  A: 

I can think of a few ways, though the language (.net, C#, vb) does not provide a specific method as far as I know:

  1. Use source control (you should, anyway), after compilation, unload the project from your solution (rightclick > unload), check in and mark as final, readonly or whatever to prevent further change.
  2. Add a little mini-script that does a pre-compilation step which checks for disallowed references (you can add it under project properties).
  3. Add a #warning to your source which is always issued: "should not be compiled with XXX", please make sure it's not in".
  4. Use circular dependency against itself: add a reference to Transaction factory in your constants library, this will effectively prevent adding a reference the other way around.
  5. A bit nastier: dynamically check for the existence of the reference from the 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.
  6. Perhaps more basic: in your nUnit tests (or whatever test framework you use), simply add one test: 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.

Abel
#3 is certainly educational, albeit constantly annoying. ;) #4 seems interesting, but a bit too kludgey for my tastes. #6 is new to me, and that's probably what I'll do. Thanks!
Mike Hanson
Ok, I'm trying #6, but I can't find an nUnit test resembling "MustNotReference".
Mike Hanson
Hmm, if you have any test framework, you can have nUnit. If not, you'll first have to install and configure it (see http://www.nunit.org/index.php). The `MustNot...` was meant as an example name. As any test, you'll need to implement the method body yourself (it's not part of the Asserts, it's too specific for that). I can help with the reflection bit, if needed.
Abel