开发者

Should I put all my common methods and utilities in a namespace or make them global?

I have a few utility methods that are used on almost every page and in other classes throught my application (eg - IsValidEmailAddress, IsAlphaNumeric, etc). Should these be in their own namespace (eg - site.common) or just left global where I don't have to add 开发者_JS百科the using to every page?


Common practice is to put them in their own namespace and then include the using


Put them in a namespace named Utility or something similar - it's cleaner this way.


in my opinion, you should not just create namespace for your utils function but also for each concept that you have in. This way if you want an util communication method you know to you should go in MyProject.Communication.Utils (Depending on how much function you have).


You could refactor them out into a single project, then reference the dll file in the project where you would like to use them.


instead of adding a using to each page just put it in the web.config

 <pages>
            <namespaces>
                <clear/>
                <add namespace="Site.Common"/>


Typically I'll put my utility methods in their own namespace/class/project, usually named [MyBase].Utils. Example: MyBase.Utils.DataIntegrity. The classes & methods are (typically) static, as well.

Make sure to create unit tests for each method.


Create a separate Utils namespace just to put all commonly used methods and other members.ITs easier and cleaner to maintain


Have you thought about using extension methods? They're basically a pretty way to make calls to static utilities.

http://msdn.microsoft.com/en-us/library/bb383977.aspx


I would recommend overriding the page object and add those methods to your override. When calling each page, you should then be able to call each method as though it was part of the page. Just my $.02 worth.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜