2

I have a web project which the folders are constructed within the areas. There, I have two main folders: Admin and Home. In Home section I will put everything concerning the end user whereas the admin will be my back office. Within the Admin folder, I divided Models and Views into sub-folders. Like the following example:

Areas
  - Admin
     - Controllers
        - CategoryController.cs
        - UserController.cs
     - Models 
        - Category
          - Edit.cs
          - Create.cs
          - Index.cs
        - User
          - Edit.cs
          - Create.cs
          - Index.cs
     - Views
        - Category
          - Edit.cshtml
          - Create.cshtml
          - Index.cshtml
        - User
          - Edit.cshtml
          - Create.cshtml
          - Index.cshtml
  - Home 
     ...

I guess it's fine so far. Since I am using Linq to Sql and I have a table named Category within it, the namespace and table name create some problem for me. Say in Category/Edit.cs:

namespace MyProject.Areas.Admin.Models.Category
{
    public class Edit
    {
        public IQuaryable<Category> // this throws an intellisense error since it
                                    // understands "Category" as the folder/namespace
                                    // instead of the table name in linq to sql class

Well, I know that the solution is easy: to change the table name to something else. But for the sake of the readablity of the code, I want to have a clear table name and to have a clear URL I want the folder name remain as category. So please can someone tell me how to differentiate the namespace from the table class?

EDIT

Though I like the answer, I redesigned my folder structure and class names. While I left the L2S class names as they were, I am naming the models as ModelCategory and trying to avoid subfoldering in Models folder with names of L2S classes. Furthermore, I merged Edit.cs - Create.cs - Index.cs in ModelCategory since they, more or less, use the same model. Therefore, I removed the Category sub-folder within the Models and the problem dissappeared.

2 Answers 2

4

In addition to Yuck's suggestions, you can also add a using statement with an alias to make the code less verbose, e.g:

using L2S = MyProject.SomeLinqToSqlNamespace;

And you can now refer to the category entity simply as L2S.Category

Sign up to request clarification or add additional context in comments.

Comments

3

Add a using statement to let the compiler know where the Category entity/table can be found:

using MyProject.SomeLinqToSqlNamespace;

namespace MyProject.Areas.Admin.Models.Category
{
    public class Edit
    {
        public IQueryable<Category> // this throws an intellisense error since it
                                    // understands "Category" as the folder/namespace
                                    // instead of the table name in linq to sql class
        // ...other code
    }
}

Or, prefix the class with the namespace inline:

public IQueryable<MyProject.SomeLinqToSqlNamespace.Category>

... but that can be rather messy to read.

2 Comments

Well, I guess the namespace MyProject.Areas.Admin.Models.Category is already defining a place to look for the compiler therefore there will be two defined Categories. How to handle that? Do I have to change the namespace every time I create a class?
No, but you can't just say Category if there are multiple possibilities. The compiler needs more information to distinguish MyProject.Areas.Admin.Models.Category from MyProject.something.something.Linq.Category. Since you are providing a namespace in the Edit class definition it will look there first for Category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.