简体   繁体   中英

How to handle same class name in different namespaces?

I am trying to create a common library structure. I am doing this by creating separate projects for every common lib I want.

I have the following 2 namespaces: MyCompany.ERP and MyCompany.Barcode

I need both of them to have a class named Utilities and be static. If I do that I will then need to specify the full namespace name before my static class in order to access it.

Is there any other preferred way to do it?

Or I should go for different names in classes like BarcodeUtils and ERPUtils ?

If i do that i will then need to specify the full namespace name before my static class in order to access it?

No, there is no need for that, though the details depend on the class that will use these types and the using declarations it has.

If you only use one of the namespaces in the class, there is no ambiguity and you can go ahead and use the type.

If you use both of the namespaces, you will either have to fully qualify the usages, or use namespace/type aliases to disambiguate the types.

using ERPUtils = MyCompany.ERP.Utilities;
using BCUtils = MyCompany.Barcode.Utilities;

public void MyMethod()
{
  var a = ERPUtils.Method();
  var b = BCUtils.Method();
}

There isn't any other way. You can make an aliases in using directives:

using MC=MyCompany.ERP;
using MB=MyCompany.Barcode;
...
public void Test()
{
  var a = MC.Utilities.Method();
  var b = MB.Utilities.Method();
}

It's the simplest way to manage them.

The MS guidelines have the following to say:

Do not introduce generic type names such as Element, Node, Log, and Message. There is a very high probability it would lead to type name conflicts in common scenarios.

and

Do not give the same name to types in namespaces within a single application model.

I concur that it's probably a good idea to use BarcodeUtilities and ErpUtilities instead. (Unless the utility classes are not meant to be used by client code, in which case you could name them Utilities and make them internal .)

"Utilities" is not a very good name for a class, since it is far too generic. Therefore, I think you should rename both of them to something more informative.

You can use an alias:

using BarcodeUtils  =  MyCompany.Barcode.Utilities;

on the pages you have clashes. But ideally rename them if this is happening in a lot of places.

I would suggest using different class names. If you really want to call both of them Utilities then you could use the alias feature on the using directive, eg

using ERP = MyCompany.ERP;
using Barcode = MyCompany.Barcode;

...
    ERP.Utilities.SomeMethod();
    Barcode.Utilities.SomeMethod();

You will have to use the full path when both are named the same. Otherwise you will get an ambiguous reference error.

You can use an alias however that will save you some typing:

using Project = PC.MyCompany.Project;

I would go for a different name that's somewhat more descriptive. A

It actually depends on the purpose of your classes. If you are going to distribute your Barcode.Utilities and ERP.Utilies seperately it is better stay like this. On the other hand, if you are going to use them only in same class, you may use 2. method for easiness of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM