Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use system namespaces for class libraries: good or bad

Is it a good idea to use "system namespaces" in my class libraries?

Sample:

namespace System.Web {
    public static class RequestExtensions {
        public static bool IsPost(this HttpRequest r) {
            return string.Compare(r.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase) == 0;
        }
    }
}

The advantage: no need to include additional uses-clauses (especially for extension methods), so all becomes available straight after adding reference to the library.

The best sample is NUnitEx project (which uses NUnit's namespace).

Disadvantages: potential name conflicts.

like image 887
Dmytrii Nagirniak Avatar asked Nov 29 '22 12:11

Dmytrii Nagirniak


2 Answers

I have to second everyone else who says its a BAD idea. Namespaces are an organizational tool, on many levels. Not only do they allow you to reuse identifiers for your own purposes without conflicting with other companies, they also allow different companies to isolate their product from your's or anyone else's. Putting code into the System namespace can be very confusing for the people who use your types.

Additionally, people know that anything in a System namespace is good, solid, tested, community vetted, thoroughly documented code that came from Microsoft. Thats a pretty important set of factors to live up to...by sticking your code in the same namespace, not only are you claiming your code is that good, but you have to live up to it.

like image 65
jrista Avatar answered Dec 06 '22 05:12

jrista


The design guidelines talk about namespace naming:

The general format for a namespace name is as follows:

<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>]

For example, Microsoft.WindowsMobile.DirectX.

Do prefix namespace names with a company name to prevent namespaces from different companies from having the same name and prefix.

There is no scope for reusing System or Microsoft here.

like image 27
Richard Avatar answered Dec 06 '22 05:12

Richard