最佳实践:C#扩展方法的命名空间,促进扩展方法 [英] Best practices: C# Extension methods namespace and promoting extension methods
问题描述
我知道存在已经是一个帖子 ,描述几乎一样,但我认为我是一个有点不同。
I know there exists already a post, describing nearly the same, but I think mine is a bit different.
我想知道的是你如何组织在分配空间方面的扩展方法。目前 - 在我们的框架扩展方法 - 我用下面的命名模式
What I would like to know is how you organize your extension methods in terms of assigning the namespace. Currently - for the extension methods in our framework - I use the following namespace pattern
- MyCompany.Web.Utils
和里面我有扩展方法的类。这是罚款,我的缺点是扩展不会立即对我们的软件开发者可见。考虑这样我有一个StringExtender类,它提供了一个非常方便的的情况下扩展方法中的扩展了字符串
对象。有withing上述命名空间中的扩展方法,我们的程序员将无法看到扩展方法,除非他们明确包括其名称空间。相反,如果我会把扩展方法在系统
命名空间,每个人都会立即看到它,但我的阅读,这是不好的做法。
and inside I have the extension method classes. This is fine for me with the disadvantage that the extenders are not immediately visible to our software developers. Consider the case where I have a StringExtender class which provides a quite handy extension method "In" that extends the String
object. Having the extension method withing the above mentioned namespace, our programmers won't see the extension method unless they explicitly include its namespace. Instead, if I would put the extension method in the System
namespace, everyone would immediately see it, but I've read that this is bad practice.
所以我的问题是你如何推广你的扩展方法ST当您的开发人员使用。
So my question is how you do promote your extension methods s.t. they are used by your developers.
推荐答案
我们把他们都在自己的命名空间 Company.Common。扩展
。这样一来,如果您有任何我们的扩展方法,你有他们所有。另外,至少在我的店里,我们不必担心我们的开发者不知道有关扩展方法。我有相反的烦恼,扩展方法重载! :)
We put them all in their own namespace Company.Common.Extensions
. That way, if you have any of our extension methods, you have them all. Plus, at least at my shop, we don't have to worry about our developers not knowing about extension methods. I have the opposite worry, extension method overload! :)
这篇关于最佳实践:C#扩展方法的命名空间,促进扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!