简体   繁体   中英

why does this namespace resolve correctly?

I am just looking at a project and I'm seeing something working which I didn't think should work.

If you look at the snippet below I'm in the namespace CustomFields.DateTimeField.Drivers , and the only other using statements in that file are other namespaces below CustomFields.DateTimeField .

But if you look on the public class DateTimeFieldDriver line it is using a type Fields.DateTimeField .

Looking at the definition of DateTimeField thats in the namespace CustomFields.DateTimeField.Fields but I have only set usings for its sister namespaces.

So the question is twofold - why does this work? is this considered a bad practice?

Snippet in question:

using System;
using JetBrains.Annotations;
using Orchard;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Localization;

using CustomFields.DateTimeField.Settings;
using CustomFields.DateTimeField.ViewModels;

namespace CustomFields.DateTimeField.Drivers {

    [UsedImplicitly]
    public class DateTimeFieldDriver : ContentFieldDriver<Fields.DateTimeField> {

        public IOrchardServices Services { get; set; }

        private const string TemplateName = "Fields/Custom.DateTime"; // EditorTemplates/Fields/Custom.DateTime.cshtml

        public DateTimeFieldDriver(IOrchardServices services) {

            Services = services;
            T = NullLocalizer.Instance;
        }

The project can be downloaded here if you want to investigate it (MVC2 project).

When you declare code to be inside a particular namespace, for example, CustomFields.DateTimeField.Drivers , you are implicitly importing all the "parent" namespaces as well.

In this case, you have an implied using for CustomFields and CustomFields.DateTimeField . This is why you do not have to specify an explicit using statement for types in CustomFields.DateTimeField and furthermore, for subnamespaces contained therein.

Thus Fields.DateTimeField is found by combining CustomFields.DateTimeField (the implied namespace) with Fields.DateTimeField (the explicit namespace) to resolve to CustomFields.DateTimeField.Fields.DateTimeField .

And no, as far as I know this is not considered bad practice.

In C#, when you're within a namespace, you have implicit using for each of the parent namespaces. For example in this file:

using System;

namespace Foo.Bar.Baz {

    class Qux {
    }
}
\EOF

Is the same as this file:

using System;

using Foo;
using Foo.Bar;

namespace Foo.Bar.Baz {

    class Qux {
    }
}
\EOF

If you have a namespace imported, then you don't need to specify the full namespace to access members of a sibling namespace tree provided it has the same prefix as an imported namespace:

using Foo;
using Foo.Bar;

namespace Foo.Bar.Baz {

    class Qux {
    }
}

namespace Foo.Bar.Norf {

    class Guf : Bar.Baz.Qux { // namespace `Foo.` is implicitly assumed to prefix the namespace reference `Bar.Baz`
    }
}
\EOF

You're experiencing these two effects combined: implicit namespace prefix usage to an implicitly imported namespace.

As for "bad practice" - experienced C# developers will be familiar with this nuance, but generally it isn't a problem unless you have ambiguous names.

As a tip, try to minimize the number of namespaces and their depths. For example, in your case I see you're storing DateTime-specific stuff in its own DateTimeField namespace. This has a bad "code smell" to me. I would just put everything in the CustomFields namespace, especially as you've given each typename the prefix DateTime anyway, so it's just double-typing.

FxCop will complain if you have fewer than 5 types in a namespace too, btw.

Update: I took a look at the C# specification ( http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf ), section 16, but I can't find any part of it which states that parent namespaces are implicitly imported, which is strange.

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