I'd like a warning about circular namespace dependency

有些话、适合烂在心里 提交于 2020-01-03 17:25:44

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!