问题
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.
回答1:
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).
来源:https://stackoverflow.com/questions/16941090/id-like-a-warning-about-circular-namespace-dependency