Search code examples
c#asp.net-mvcasp.net-mvc-4code-contracts

Preventing the use of certain dlls or service code in a given location


Is there a simple way (using code contracts perhaps?) to prevent the use of certainl dlls/namespace/assemblies within a given portion of an application?

My use case is that I'd like a warning mechanism when I try to use a company wide service layer within my controllers.

Instead we should have a layer beween that service layer and our controllers (the service layer is changing and we'll be switching it out bit by bit for our new SDK).

What are some ways of doing this? Can I simply specify somethig in a code contract to warn of the use of a given assembly in the controller methods?


Solution

  • Code Contracts won't really be useful here.

    Do you have access to Visual Studio Premium or Ultimate? There is a Layer Diagram tool built in, which can be used to validate architecture at build time.

    You create a new Modelling Project and add a Layer Diagram inside that. Then you can drag other projects into the diagram, and you can edit the relationships between them, etc. It's a very intuitive interface.

    Here's a blog post explaining how to turn on compile-time verification, and some more information about populating the diagram.

    Here is an example layer diagram. If, for example, code from Business accessed what was inside the Web layer, an error would be generated.

    An example layer diagram
    (source: msdn.com)