产品商标名用于名称相同的类/名称空间

时间:2022-01-26 22:52:33

Author post-edit: Chosen solution

(Original question remains below this box)

SUMMARY: You SHOULD NOT name a class the same as its namespace. Therefore should a product name be used for the namespace or the main class?

摘要:不应该将类命名为与其名称空间相同的名称空间。因此,应该为名称空间或主类使用产品名称吗?

Chosen solution: I decided to apply the product name to the namespace and add a suffix to the main class name (e.g., __Module).

选择的解决方案:我决定将产品名称应用到名称空间,并向主类名称(例如,__Module)添加后缀。

Rationale: Visual Studio defaults to using the project name for the namespace, assembly name, as well as the actual deliverable .exe or .dll -- These are the most visible items so I think it makes sense to name the namespace after my product name, then do as Jon Skeet recommends in his answer and name the main class as ___Main or __Program or __Module. No one right answer I suppose.

原理:Visual Studio默认使用的项目名称命名空间,组装的名字,以及实际交付. exe或dll——这些都是最明显的东西所以我认为是有意义的命名空间名称产品名称后,然后做Jon水瓢建议在他的回答和主类名___Main __Program或者__Module。我想没有一个正确的答案。


Original question:

最初的问题:

I COMPLETELY GET IT-- DON'T NAME A CLASS THE SAME AS ITS NAMESPACE!

我完全理解它——不要将类命名为与其名称空间相同的名称空间!

This is almost a duplicate question... except I've spent hours reading articles (see below) and can't find or think of a solution that seems right.

这几乎是一个重复的问题……除了我花了数小时阅读文章(见下文),却找不到或想不出一个似乎正确的解决方案。

Say I have a product called the ACME Foobarinator. It has a couple related types (e.g., settings, enums) but not enough to call for any sort of namespace hierarchy.

假设我有一个产品叫ACME Foobarinator。它有一些相关的类型(例如,设置,枚举),但不足以调用任何类型的名称空间层次结构。

It makes sense to create a single product namespace and throw everything into it:

创建一个产品名称空间并将所有东西都放入其中是有意义的:

namespace Acme.Web.Foobarinator
{
    public class Foobarinator { } // BAD! Same name as namespace!
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

BAD!!! Both namespace and class name are the same!!

糟糕! ! !命名空间和类名是相同的!

But, I'd also like the main class to be Foobarinator because it's the trademark product name and I'd like consumers to use it by trademark name: var fb = new Foobarinator();

但是,我也希望主类是Foobarinator,因为它是商标名,我希望消费者使用它的商标名:var fb = new Foobarinator();

Option 1: Eliminate the product namespace, promoting all types to the parent namespace. But this pollutes the parent namespace with types that are specific to the product (and not necessarily all public types). As the product evolves, the pollution stands to increase!

选项1:消除产品名称空间,将所有类型推广到父名称空间。但是,这将使用特定于产品的类型(不一定是所有公共类型)污染父命名空间。随着产品的发展,污染将会增加!

namespace Acme.Web
{
    public class Foobarinator { }
    public class FoobarinatorInfo { }
    public enum FoobarinatorMode { Disabled, Enabled }
    // More pollution in future...
}

Option 2: Add needless suffix to the main class. But this obfuscates the product trademark name!

选项2:向主类添加不必要的后缀。但这混淆了产品商标名称!

namespace Acme.Web.Foobarinator
{
    public class FoobarinatorMain { } // Not the name of the product!
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

Option 3: Use child classes/types for the related types. However, encapsulating everything in one class eventually violates separation of concerns as the product evolves:

选项3:对相关类型使用子类/类型。然而,将所有内容封装在一个类中,最终会在产品演化过程中违反关注点分离:

namespace Acme.Web
{
    public class Foobarinator
    {
        public class FoobarinatorInfo { }
        public enum Mode { Disabled, Enabled }
    }
}

YOUR THOUGHTS?

Where do I compromise?

在哪里我妥协吗?

Related questions and articles:

相关的问题和文章:

How to avoid having the same name for a class and it's namespace, such as Technology.Technology?

如何避免为类及其命名空间使用相同的名称,例如Technology.Technology?

Should a class have the same name as the namespace?

类是否应该具有与名称空间相同的名称?

Namespace with same name as a class name

与类名同名的名称空间

Should a class have the same name as the namespace?

类是否应该具有与名称空间相同的名称?

http://blogs.msdn.com/ericlippert/archive/2010/03/09/do-not-name-a-class-the-same-as-its-namespace-part-one.aspx

http://blogs.msdn.com/ericlippert/archive/2010/03/09/do-not-name-a-class-the-same-as-its-namespace-part-one.aspx

4 个解决方案

#1


3  

I would use:

我将使用:

namespace Acme.Web.Foobarinator
{
    public class Program { } // The entry point
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

or even:

甚至:

namespace Acme.Web.Foobarinator
{
    public class EntryPoint { }
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

The first is the Visual Studio default (at least for some project types) and the second makes it crystal clear what the role of the class is.

第一个是Visual Studio默认值(至少对于某些项目类型是这样),第二个是非常清楚类的角色是什么。

#2


1  

Create a more specific namespace that says what the classes are for:

创建一个更具体的名称空间,说明类的用途:

namespace Acme.Web.Foobarinator.Client {
   public class FoobarinatorInfo { }
   public enum Mode { Disabled, Enabled }
  public class Foobarinator { }
}

#3


0  

I would change the name of the namespace to be descriptive of the product rather than the name of the product. So if the Foobarinator has to do with graphics, make your namespace Acme.Web.Graphics.

我将名称空间的名称更改为产品的描述性名称,而不是产品的名称。因此,如果Foobarinator与图形有关,请将名称空间ac . web . graphics设置为。

#4


0  

One option would be to use the plural. I tend to do this, even though I don't have actual trademarked product names in my namespaces.

一种选择是使用复数。我倾向于这样做,尽管我的名称空间中没有实际的商标名称。

For example, suppose you have a ticketing system:

例如,假设你有一个售票系统:

namespace Acme.Web.Tickets
{
    public class Ticket { }
    public class TicketInfo { }
    public enum Mode { Disabled, Enabled }
}

You could apply that to your product:

您可以将其应用于您的产品:

namespace Acme.Web.Foobarinators
{
    public class Foobarinator { }
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

#1


3  

I would use:

我将使用:

namespace Acme.Web.Foobarinator
{
    public class Program { } // The entry point
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

or even:

甚至:

namespace Acme.Web.Foobarinator
{
    public class EntryPoint { }
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

The first is the Visual Studio default (at least for some project types) and the second makes it crystal clear what the role of the class is.

第一个是Visual Studio默认值(至少对于某些项目类型是这样),第二个是非常清楚类的角色是什么。

#2


1  

Create a more specific namespace that says what the classes are for:

创建一个更具体的名称空间,说明类的用途:

namespace Acme.Web.Foobarinator.Client {
   public class FoobarinatorInfo { }
   public enum Mode { Disabled, Enabled }
  public class Foobarinator { }
}

#3


0  

I would change the name of the namespace to be descriptive of the product rather than the name of the product. So if the Foobarinator has to do with graphics, make your namespace Acme.Web.Graphics.

我将名称空间的名称更改为产品的描述性名称,而不是产品的名称。因此,如果Foobarinator与图形有关,请将名称空间ac . web . graphics设置为。

#4


0  

One option would be to use the plural. I tend to do this, even though I don't have actual trademarked product names in my namespaces.

一种选择是使用复数。我倾向于这样做,尽管我的名称空间中没有实际的商标名称。

For example, suppose you have a ticketing system:

例如,假设你有一个售票系统:

namespace Acme.Web.Tickets
{
    public class Ticket { }
    public class TicketInfo { }
    public enum Mode { Disabled, Enabled }
}

You could apply that to your product:

您可以将其应用于您的产品:

namespace Acme.Web.Foobarinators
{
    public class Foobarinator { }
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}