开发者

How to have a class in a namespace that has the same name that is part of another namespace in a .net class library

ok...this gets confusing for me to explain, so i will show a class from one namespace and a seperate namespace...

Company.Product.Domain

(this represents the class that contains methods specific to that class, i named it "Domain" just so you can see how I'm using the same name in a separate namespace

Company.Product.Domain.Data.Contracts

(this namespace will be for a开发者_JS百科ll of the data contracts to be used in a WCF service)

If I am in the Company.Product.Domain class and I try to use the classes within Company.Product.Domain.Data.Contracts, when I attempt to type this out, i get to Company.Product.Domain and will not show intellisense any further. It also shows that that reference is a class instead of allowing me to get to the namespace.

I do have the Company.Product.Domain.Data.Contracts referenced in my Company.Product.Domain class.

Is it possible to have a namespace Company.Product with a class inside of it and also another namespace of Company.Product.Domain.Data.Contracts? If so, how?

Thanks


Yes, it's possible. However, you should avoid it like the plague. It makes everything much more complicated.

Eric Lippert has written a series of blog posts on this very topic recently. Admittedly it's not quite the same issue - he's talking about Foo.Bar.Bar (where Foo.Bar is the namespace, and Bar is the class) but I believe you'll run into many of the same issues. Indeed, I think you may actually end up making them worse than the scenario he's describing. Please don't do it.

  • Part One: Collisions amongst referenced assemblies
  • Part Two: Machine-generated code
  • Part Three: Bad hierarchical design
  • Part Four: Making the problem worse


You can alias your Company.Product.Domain.Data namespace.

So in your Domain class, instead of

using Company.Product.Domain.Data;

try something like

using myData = Company.Product.Domain.Data;
namespace Company.Product
{
  class Domain
  { ...
    myData::Data.method();
  }
}

I'm not positive that intellisense behaves the way you want with this, but it does disambiguate the Domain class vs. Domain namespace.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜