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?
Placing
usingdirectives inside a namespace is possible since .NET 1.0. This is done to avoid namespace polution because the types include by theusingdirectives are only visible inside the namespace containing theusingstatements. So they just changed the code generator to adhere to the recommended practice to putusingdirectives in the deepest possible namespace. Placing the theusingdirectives 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
usingdirective is a C# thing and therefore it should read “is possible since C# 1.0” instead of “is possible since .NET 1.0”.