C# MVC - 文件夹结构 - 在哪里放置类?

发布于 2024-08-05 11:09:11 字数 159 浏览 9 评论 0原文

在asp/net MVC中,项目分为模型、视图和控制器。到目前为止还好。

假设我有一个类“person”,人员控制器使用人员模型向其中填充数据。

我的 person.cs 类最好放在哪里?在控制器或模型文件夹中?你做什么工作?

谢谢

弗兰克

In asp/net MVC, the project is divided into Models, Views and Controllers. Fine so far.

Say I have a Class 'person', which the people controller populates with data, using the person model.

Where is it best to put my person.cs class? In the controllers or the models folder? What do you do?

Thanks

Frank

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欲拥i 2024-08-12 11:09:11

我将在解决方案中创建另一个类库项目,定义我的类,然后在 ASP.Net MVC 应用程序中引用我的库。

Web应用解决方案
|-MyMVCProject
  |-型号
  |-控制器
  |-视图
|-MyMVCProject.Domain(您可以将模型放在这里)
  |-Person.cs
|-MyMVCProject.Services(您可以将实体服务放在这里)

I'd create another class library project in the solution, define my classes and then reference my library in my ASP.Net MVC application.

WebApp Solution
|-MyMVCProject
  |-Models
  |-Controllers
  |-Views
|-MyMVCProject.Domain (you'd put your models here)
  |-Person.cs
|-MyMVCProject.Services (you'd put entity services here)
著墨染雨君画夕 2024-08-12 11:09:11

我会将 Person 视为模型并将其放在 Models 文件夹中。这是我保存视图模型的地方。业务模型通常保存在一个单独的数据项目中——我喜欢我的数据层是独立的,这样如果我愿意,我可以在其他项目中使用它。您可能需要考虑为其他类型的类(不适合控制器或模型类别的扩展方法、帮助程序等)创建单独的类库。

I would consider Person a model and put it in the Models folder. This is where I keep my view models. Business models are normally kept in a separate data project -- I like my data layer to be self-contained so I can use it in other projects if I want. You might want to consider creating separate class libraries for other types of classes -- extension methods, helpers, etc. that don't fit into the category of controllers or models.

不疑不惑不回忆 2024-08-12 11:09:11

目前在相当大的项目中工作,我将我的类分为不同的类库项目,这些项目名为 Repository(用于数据访问接口和业务数据类)、Utils(用于实用程序类)和 DataAccess(存储库接口实现)。在这些目录下,我将它们划分为子目录(或者我应该说命名空间),然后使用 Resharper 来强制执行这些命名空间。

Currently working at rather large project, I have my classes divided into different class library projects which are named Repository (for data access interfaces and business data classes), Utils (for utility classes) and DataAccess (repository interfaces implementations). Under those I have them divided in subdirectories (or should I say namespaces) and then use Resharper to enforce those namespaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文