4

I can arrange my code in assemblies to enforce proper namespace dependancies:

Company.Product.Domain assembly
Company.Product.DataAccessLayer assembly
  -References Company.Product.Domain
Company.Product.Application assembly
  -References Company.Product.Domain
  -References Company.Product.DataAccessLayer

Because circular assembly reference is not possible, I can be sure I am not referring to the DAL from within the Domain for example.

But this leads to a large number of assemblies when you have multiple domains, I'd like to simply use namespaces to organise the project like so:

Company.Product assembly
  -Company.Product.Domain namespace
  -Company.Product.DataAccessLayer namespace
  -Company.Product.Application namespace

Is there any way I can prevent, or warn of namespace dependancies I don't want.

I have resharper if there is a "Code Inspection->Custom Pattern" solution someone knows of.

weston
  • 54,145
  • 21
  • 145
  • 203
  • I'm not quite sure if I follow. You mean if you define all the namespaces within a _single_ project/assembly, can you still enforce namespace usage _like_ they're referenced assemblies/dependencies? EDIT: That is, if they're all in the same project/assembly, ensure that a class `Company.Product.Domain.Foo` cannot access (or warn about access) to `Company.Product.Application.Bar`? – Chris Sinclair Jun 05 '13 at 13:25
  • @ChrisSinclair yeah that's it. – weston Jun 05 '13 at 13:30
  • Hmmm, I'm not too sure. I suppose plausibly you could do it (not sure about Resharper), maybe with Roslyn, but you'd have to define these references/dependencies between namespaces _somewhere_, likely outside of what Visual Studio provides; might take some custom plugin or updating your "Custom Pattern" manually. (as an aside, this _feels_ like a bad idea. Perhaps you could investigate methods to merge the multiple assemblies instead? http://stackoverflow.com/questions/8077570/how-to-merge-multiple-assemblies-into-one) – Chris Sinclair Jun 05 '13 at 13:34
  • You may want to have a look at these products, if you are looking for a tool: http://structure101.com/products/index.php#/refactor – Marcel Jun 05 '13 at 13:53
  • @ChrisSinclair I like the idea of doing it in Roslyn, sounds like a good excuse to have a play with that. As for combining, yes good idea, when we choose an obfuscator it'll probably have that built in, most do. My objection is just having dozens of assemblies, perhaps I should try to get used to using the object browser more than I do, and not worry about the mess in the solution explorer. – weston Jun 05 '13 at 13:55
  • @weston 179 projects and counting in my solution here... :-/ (albeit the majority of them are duplicated/linked for multi-platform simultaneous compilation) – Chris Sinclair Jun 05 '13 at 13:56
  • @Marcel thanks, that looks promising. – weston Jun 05 '13 at 13:57
  • @ChrisSinclair wow, and here's me worrying about 40 odd! – weston Jun 05 '13 at 13:58
  • NDepend can do what you want, I think. – AakashM Jun 05 '13 at 14:29
  • Visual Studio Ultimate layer diagrams probably do what you want: http://msdn.microsoft.com/en-us/library/dd409462.aspx – Peter Ritchie Jun 16 '14 at 14:20

1 Answers1

3

I've had just the same issue myself and not finding the right tool I've created my own. Using (guess what): Roslyn.

It's open source and it's here: http://nsdepcop.codeplex.com

It works like this:

  • You have to define the allowed namespace dependencies in XML files (one file per project).
  • The tool is integrated into the MSBuild process so it runs every time you build and gives warnings/errors for each source code segment that violates the dependency rules.
  • If you install the Roslyn End User Preview (for VS2013) then it also integrates into Visual Studio and gives feedback as you type the code (no need to build to get the issues).
Vizu
  • 1,871
  • 1
  • 15
  • 21