什么是正确的C#命名空间的使用情况如何? [英] What is the proper C# namespace usage?

查看:167
本文介绍了什么是正确的C#命名空间的使用情况如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自一个Java的背景,我看到很多人说的命名空间=包,但环顾四周在可用的代码似乎对我没有人们使用的命名空间,如他们使用的包。

I come from a Java background, and I see a lot of people say namespaces = packages, but looking around at available code it doesn't seem to me that people use namespaces like they used packages.

目前我正在一个DLL来管理我所有的数据的两个Windows应用程序之间共享访问数据库。到目前为止,我一直在创造包,比如我会针对Java,所以我有一个域名,服务,DAO(我称之为存储库)子命名空间了我的最高水平。它是否正确?有没有人写对名称空间的最佳做法?我想这可能是一个非常小的点,但是我不想去关逆着纹理可以这么说。

Currently I'm working on a DLL to manage all my data access to a database to be shared between two Windows Applications. So far I have been creating packages like I would for Java so I have a Domain, Services, DAO (I call it Repositories) sub namespaces off my top level. Is this correct? Has anyone written a best practice for namespaces? I assume this is probably a very minor point, but I don't want to go off going against the grain so to speak.

推荐答案

略主观的,没有明确的,正确的答案。

Slightly subjective, there is no "definitive, correct" answer to this.

下面就是我如何做到这一点,意图使组件可之间的项目共享。

Here's how i do it, with the intention that assemblies can be shared amongst projects.

考虑我称之为一个公司名称的 FooBar的(+1创意人?)

Consider i have a company name called FooBar (+1 for originality anyone? :)

您所有的组件从这个根命名空间开始。我们有很多的共享组件。

All your assemblies start from this root namespace. We have many shared assemblies.

这样的话:


  • MVC(UI)HTML助手。这些走在一个集会。

  • 通用库。 Repository模式泛型实现,再利用。

  • LINQ扩展方法(传呼,一般的语法糖工厂)。同样,这些走在一个程序集

所以,我们的 FooBar的命名空间宇宙可能是这样的:

So, our FooBar Namespace Universe might look like this:

FooBar
|
 ---- FooBar.Common.Mvc
|
 ---- FooBar.Common.DataAccess
|
 ---- FooBar.Common.Linq
|
 ---- FooBar.ProjectOne (ASP.NET MVC Web Application)
|     |
|      --- FooBar.ProjectOne.Repository (makes use of FooBar.Common.DataAccess)
|     |
|      --- FooBar.ProjectOne.WebMvc (makes use of FooBar.Common.Mvc)
|
 ---- FooBar.ProjectTwo (WPF Application)
      |
       --- FooBar.ProjectTwo.Repository (makes use of FooBar.Common.DataAccess)
      |
       --- FooBar.ProjectTwo.BindingServices (makes use of FooBar.Common.Linq)

明白我的意思?

设置你的命名空间的方式,这感觉不错把常见的逻辑放到公共区域,基于异构空间。

Setup your namespaces in a way that it 'feels right' putting common logic into common areas, based on the heterogeneous namespace.

您会发现很多公司有多个共享项目顺应这种趋势。

You'll find a lot of companies with multiple shared projects follow this trend.

您的'子命名空间的思考正确的是(在我看来)。

Your thinking of 'sub-namespaces' is correct (in my opinion).

这篇关于什么是正确的C#命名空间的使用情况如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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