为什么循环依赖允许在C#中的命名空间? [英] Why is circular dependency allowed with namespaces in c#?

查看:415
本文介绍了为什么循环依赖允许在C#中的命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中你允许有文件a.cs(其中有MyApp.A的命名空间)声明:

In c# you're allowed to have a statement in file a.cs (which has namespace of MyApp.A):

using MyApp.B;



而在文件b.cs(其中有MyApp.B的命名空间)已经声明

while file b.cs (which has namespace of MyApp.B) already have the statement

using MyApp.A;

如果有类似的依赖性会存在于不同的DLL(A.DLL的地方有一个参考B.DLL反之亦然),它不会被允许的,因为循环依赖错误的,那么,为什么它的命名空间(和编译器允许甚至不产生警告)?这难道不是一种代码味道这样做呢?

If a similar dependency would exist in different dll's (where a.dll has a reference to b.dll and vice versa) it wouldn't be allowed because of circular dependency error, so why is it allowed with namespaces (and compiler doesn't even produce a warning)? Isn't it a code smell to do so anyway?

推荐答案

Damien_The_Unbeliever 的写的命名空间是功能性的逻辑分组

汉斯帕萨特的写的命名空间不过是一个简单的提示编译器

我想阐述,这真的取决于你正在考虑在你的代码库的组件的东西。 A 组件的是一组类型。 A 组件的可以生成一个或多个组件定义一个或多个命名空间。重要的是,有组件之间没有依赖循环。因为组件是发展的单元,然后如果A组份和B相互使用对方,它们构成了一个更大的开发单位的,他们不能被独立开发的,他们形成的超组件的或者换句话说,这是根的面条代码

I'd like to elaborate that it really depends what you consider being a component in your code base. A component is a group of types. A component can spawn one or several namespaces defined in one or several assemblies. The important thing is that there is no dependency cycle between components. Because a component is a unit of development, and if component A and B are mutually using each other, they constitute a bigger unit of development, they cannot be developed independently, they form a super-component or in other words, this is the root of spaghetti code.

要回答你问题的为什么循环依赖允许在C#中的命名空间?的的的的你的问题的背后语句是命名空间用来定义的逻辑组件的。但是,命名空间还可以用来避免类型名称相撞,引起公共API的类结构化的方式,筛选一组扩展方法......因此,我想答案是, C#的设计者肯定没'不想限制命名空间的概念只逻辑组件化

To answer your question, Why is circular dependency allowed with namespaces in c#? the implicit statement behind your question is that namespaces are used to define logical components. But namespace can also be used to avoid type name collision, to present classes of a public API in a structured way, to filter a set of extension methods... Hence I guess the answer is that the C# designer certainly didn't want to restrict the concept of namespace only for logical componentization.

作为一个方面说明,许多开发人员正在使用的项目/组件的概念的定义组件。恕我直言,由于装配附带成本和维护(版本控制,部署,编译,动态加载CLR ...)的物理概念,这是错误的。大会作为的物理的概念应该被用于的物理的原因(如单位的部署,API单位,插件IMPL,编码/测试分离...)。我写两个白色的书对这个中的组装VS VS命名组件的话题如果能感兴趣。

As a side note, many developers are using the concept of project/assembly to define components. IMHO this is wrong because assembly is a physical concept that comes with cost and maintenance (versioning, deployment, compilation, dynamic CLR load...). Assembly as a physical concept should be used for physical reasons (like unit of deployment, unit of API, plugin impl, code/test separation...). I wrote two white books on this assembly vs namespace vs component topic if it can interest you.

这难道不是一种代码味道这样做呢?

在我看来,这是因为如果一个大型装配包含循环依赖众多的命名空间,一个人没有办法理解尝试从代码的整体架构。我在名为NDepend的一个.NET的静态分析仪,可以检查工作命名空间依赖循环并通过依赖图表和的相关矩阵。我们有超过400的命名空间,我们很高兴,让他们所有的分层在适当组件的一打。依赖关系矩阵提供了一个方便的方式来可视化一目了然分层结构的命名空间。

In my opinion it is, because if a large assembly contains many namespaces with dependency cycles, one has no way to try understanding the overall architecture from code. I work on a .NET static analyzer named NDepend that can check for namespaces dependency cycles and present results through dependency graph and dependency matrix. We have more than 400 namespaces and we are glad to keep them all layered properly in a dozen of assemblies. The dependency matrix offers a handy way to visualize the layered namespace structure at a glance.

这篇关于为什么循环依赖允许在C#中的命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆