开发者

c# mvc area folder structure and class name

开发者 https://www.devze.com 2023-03-16 13:33 出处:网络
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

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 intellisen开发者_运维问答se 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号