.Net 4.0 中命名空间声明的错误或新约定

发布于 2024-09-09 09:59:45 字数 1163 浏览 6 评论 0原文

Visual Studio 2010,.Net 4.0

我正在生成一些域服务类,这就是 Visual Studio 包含命名空间的方式:

namespace MyCoolProject.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Linq;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using Columbus.Web.Data;
    using Microsoft.ServiceModel.DomainServices.LinqToSql;


    // Implements application logic using the JanDoetsDataContext context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class AnnouncementService : LinqToSqlDomainService<MydataDataContext>
    {......................

此类所需的命名空间包含在命名空间声明内,换句话说, using bla bla bla 行位于MyCoolProject.Web.Services 命名空间声明不在外面。这是新约定还是vs2010的bug?

Visual Studio 2010, .Net 4.0

I'm generating some Domain Service Classes and this is how visual studio includes the namespaces:

namespace MyCoolProject.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Linq;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using Columbus.Web.Data;
    using Microsoft.ServiceModel.DomainServices.LinqToSql;


    // Implements application logic using the JanDoetsDataContext context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class AnnouncementService : LinqToSqlDomainService<MydataDataContext>
    {......................

the namespaces needed by this class are included inside the namespace declaration, in other words the using bla bla bla lines are inside the MyCoolProject.Web.Services namespace declaration and not outside. Is this a new convention or a vs2010 bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

五里雾 2024-09-16 09:59:45

自 .NET 1.0 以来,可以将 using 指令放置在命名空间内。这样做是为了避免命名空间污染,因为 using 指令包含的类型仅在包含 using 语句的命名空间内可见。因此,他们只是更改了代码生成器,以遵循建议的做法,将 using 指令放在尽可能深的命名空间中。将 using 指令放置在任何命名空间之外会执行全局根命名空间中的包含操作。

查看 StyleCop 规则 SA1200 了解更多详细信息。

更新

答案有点不精确 - using 指令的位置是 C# 的事情,因此它应该显示为“自 C# 1.0 以来是可能的” 而不是“自.NET 1.0以来是可能的”

Placing using directives inside a namespace is possible since .NET 1.0. This is done to avoid namespace polution because the types include by the using directives are only visible inside the namespace containing the using statements. So they just changed the code generator to adhere to the recommended practice to put using directives in the deepest possible namespace. Placing the the using directives outside any namespace performs the include in the global root namespace.

Have a look at StyleCop rule SA1200 for some more details.

UPDATE

The answer is a bit unprecise - the placement of the using directive is a C# thing and therefore it should read "is possible since C# 1.0" instead of "is possible since .NET 1.0".

心碎的声音 2024-09-16 09:59:45

将 using 语句放置在命名空间内(基于 StyleCop 规则)一直是惯例,但 Visual Studio 中的默认模板将它们放在命名空间之外,因此这已成为最常见的用法。

自动生成的文件将 using 语句放置在命名空间内有一个很好的理由:它会影响引用的解析顺序。如果您在 MyCoolProject.Web 命名空间中定义的类与 Columbus.Web.Data 中使用的类共享名称,则该类的用法将解析为 MyCoolProject.Web.Class(如果使用在外部)或 Columbus.Web。 Data.Class(如果使用在内部)。因此,通过将 using 语句保留在命名空间内,自动生成的代码对于您可能在项目中所做的更改更加稳健。

这不会编译(因为 MyCoolProject.Web.Console 类没有定义 WriteLine):

using System;    
namespace MyCoolProject.Web.Services
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello there");
        }
    }
}

namespace MyCoolProject.Web
{
    public static class Console
    {

    }
}

而这将:

namespace MyCoolProject.Web.Services
{
    using System;
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello there");
        }
    }
}

namespace MyCoolProject.Web
{
    public static class Console
    {

    }
}

It has always been the convention to place using statements inside namespaces (based on StyleCop rules), but the default template in Visual Studio has them outside so that has become the most common usage.

There is a good reason why the auto-generated files place the using statements inside the namespace though: It affects the order that references are resolved. If you defined a class in the MyCoolProject.Web namespace that shared a name with a class used in Columbus.Web.Data then the usage of that class will resolve to MyCoolProject.Web.Class if the usings are outside, or Columbus.Web.Data.Class if the usings are inside. So, by keeping the using statements inside the namespace, the auto-generated code is more robust to changes that you may make in the project.

This won't compile (because the MyCoolProject.Web.Console class does not define WriteLine):

using System;    
namespace MyCoolProject.Web.Services
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello there");
        }
    }
}

namespace MyCoolProject.Web
{
    public static class Console
    {

    }
}

Whereas this will:

namespace MyCoolProject.Web.Services
{
    using System;
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello there");
        }
    }
}

namespace MyCoolProject.Web
{
    public static class Console
    {

    }
}
自控 2024-09-16 09:59:45

using 指令放入命名空间内是 Microsoft 内部代码风格约定的一部分(由 StyleCop 强制执行)。就我个人而言,我无法忍受,但事实就是如此。我认为我说得对,它真正能产生影响的唯一情况是,如果您在同一个代码文件中定义了多个类(在不同的命名空间中!),这被广泛认为禁忌。

我相信,自 C# 1.0 以来,它一直是可接受的语法。

Putting using directives inside the namespace is part of the internal Microsoft code style convention (as enforced by StyleCop). Personally I can't stand it, but it is what it is. I think I'm right in saying that the only circumstances under which it can actually make a difference are if you have multiple classes (in different namespaces!) defined in the same code file, which is widely considered a no-no.

It's been acceptable syntax since C# 1.0, I believe.

狼性发作 2024-09-16 09:59:45

我不确定这是否是一个根本原因,但请注意,至少在 .NET 3.5 中,如果您在部分类中的命名空间块之外声明 using 块,则存在一个错误,该错误会导致 Linq to SQL 无法工作你的数据上下文。

I'm not sure this is much of an underlying reason, but beware, at least in .NET 3.5, there's a bug that causes Linq to SQL not to work if you declare your using block outside of your namespace block in your partial class for your DataContext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文