I have the following code:
using Data;
using MyProject_API.Models.ViewModels;
using System.Linq;
using Utilities.Web.Authentication;
namespace MyProject_API.Services.EmailTemplates
{
/// <summary>
/// Defines the <see cref="EmailTemplatesSearch" />
/// </summary>
public class EmailTemplatesSearch
{
/// <summary>
/// Initializes a new instance of the <see cref="EmailTemplatesSearch"/> class.
/// </summary>
/// <param name="dataContext">The dataContext<see cref="DataContext"/></param>
/// <param name="user">The user<see cref="MyProjectIdentity"/></param>
public EmailTemplatesSearch(DataContext dataContext, MyProjectIdentity user)
{
_db = dataContext;
_user = user;
}
I've cleaned the code with help of CodeFormatter
and get the following code:
namespace MyProject_API.Services.EmailTemplates
{
using MyProject_API.Models.ViewModels;
using System.Linq;
using Utilities.Web.Authentication;
/// <summary>
/// Defines the <see cref="EmailTemplatesSearch" />
/// </summary>
public class EmailTemplatesSearch
{
/// <summary>
/// Initializes a new instance of the <see cref="EmailTemplatesSearch"/> class.
/// </summary>
/// <param name="dataContext">The dataContext<see cref="DataContext"/></param>
/// <param name="user">The user<see cref="MyProjectIdentity"/></param>
public EmailTemplatesSearch(DataContext dataContext, MyProjectIdentity user)
{
_db = dataContext;
_user = user;
}
As you can see, CodeFormatter
looses using Data;
. I've tried to add using Data;
to the list of usings, but the code can't be compiled, DataContext
which is in Data
namespace can't be found. I fixed the problem moving using Data;
outside from namespace, but it looks like a crutch. DataContext
is defined like a
namespace Data
{
public class DataContext
CodeFormatter
looses using Data;
?using Data;
doesn't work inside the namespace?I've searched for similar questions: LINQ to SQL Designer Bug , Should 'using' directives be inside or outside the namespace? , etc., but i still don't understand how to solve my issues
The solution was already given in the first comment by maccettura.
If you read my answer to the thread "Should 'using' directives be inside or outside the namespace in C#?" which you already linked, you should understand that when you have using Data;
inside your declaraion namespace MyProject_API.Services.EmailTemplates { /* ... */ }
, any namespace:
MyProject_API.Services.EmailTemplates.Data
or
MyProject_API.Services.Data
or
MyProject_API.Data
would match before the namespace Data
you actually want.
C#'s way to get around that, is to use the global
alias, like this:
namespace MyProject_API.Services.EmailTemplates
{
using global::Data; // <--- global alias used
using MyProject_API.Models.ViewModels; // <--- unless you have other name conflicts, 'using Models.ViewModels;' is sufficient now
using System.Linq;
using Utilities.Web.Authentication;
// your classes here
}
This will make your using
point to the namespace you want. And with that, it will no longer be an unused using
directive, and code formatting will no longer "lose" (i.e. remove) it.
The tool you used for moving using
s from the outside to the inside, apparently was not smart enough to add the needed global::
.