Search code examples
c#.netasp.net-mvcdirectory-structure

c# mvc area folder structure and class name


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.


Solution

  • 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