如何为 MVC3 生成服务层和存储库层

发布于 2024-12-04 20:08:53 字数 1187 浏览 2 评论 0原文

我最近为客户完成了一个 MVC3 项目,使用使用微型 ORM 实现的存储库模式,并为我的业务逻辑提供了服务层。恕我直言,该应用程序运行良好,客户很满意。

但每当我想要添加/实现新服务和关联的 DTO 时,我都必须编写大量样板代码。另外,在将 DTO 转换为视图模型并再转换回来时,存在大量代码重复,并带有相关的 PEBKAC。随着需求的发展,数据库模式发生了变化,需要直接传播到视图模型。

对于另一个客户端,我继承了一个 .netTiers 代码生成项目,这给我带来了很大的痛苦,直到我修复了基本模板中的一些缺陷,使用 MSBuild 自动生成代码,并使用 MSBuild 调整生成的代码以使其构建没有以前需要的手动调整目录。 .netTiers 最终生成了大量有用的代码,但存在大量重复、复杂性,感觉就像用大锤剥猫皮一样。

现在我正在研究另一个 MVC3 项目,我想避免自己编写所有样板文件,但我也想避免完整的 .netTiers 类型代码生成。我没用过EF。我倾向于认为对于我所承担的项目规模而言,它是一个太大的工具,但如果它可以为我省去一些手动步骤,那么这将节省大量时间。 EF 有哪些优点?它会为我搭建服务层吗?

我正在考虑的另一个选择是 LightSpeed 这需要我花一些美元(不多)但如果它能为我生成服务层代码,那钱花得值。 LightSpeed 支持这种类型的代码生成吗?

显然,随着领域模型和数据库模式的发展,服务需要更新以适应这些变化。 .netTiers 通过生成部分类来实现这一点。这些其他工具如何处理这些更改而不覆盖服务层中的任何自定义逻辑?

还有哪些其他选择?

更新:感谢您的所有反馈,有很多积极的选择可供查看。有人看过MVC 脚手架吗?

更新 #2:我将采用 MVCScaffolding 选项,该选项为 EF Code First 生成代码。它开箱即用地生成一个 Repository 类,然后不幸地将其与模型粘在一起,而模型在 MVC 中实际上是视图模型,而不是域模型。 MVCScaffolding 项目有一个针对服务层脚手架的拉取请求,因此我们将研究该选项。加上用于映射 POCO 的 AutoMapper <-> DTO。

I recently completed an MVC3 project for a client using a Repository pattern implemented using a micro ORM, and with a service layer for my business logic. IMHO the application works well and the client is happy.

But I had to write a lot of boilerplate code whenever I wanted to add/implement a new service and associated DTO's. Plus there was a fair amount of code duplication converting DTO's to View Models and back again, with associated PEBKAC. And as the requirements evolved there were changes to the database schema that needed to be propagated right through to the View Models.

For a different client I inherited a .netTiers code generation project that caused me a lot of grief until I fixed some defects in the base templates, automated the code generation using MSBuild and, also using MSBuild, massaged the resulting code to get it to build without the catalog of manual tweaks previously required. .netTiers ultimately generated a lot of useful code but with enormous amounts of duplication, a bucket of complexity, and it felt like using a sledgehammer to skin a cat.

Now I'm looking at another MVC3 project and I want to avoid having to write all the boilerplate myself but I also want to avoid a full .netTiers-type code generation. I haven't used EF. I've tended to think of it as too big a tool for the size of projects I undertake but if it can take away some of the manual steps for me then that would be a big time saving. What are the merits of EF and will it scaffold the service layers for me?

The other option I'm considering is LightSpeed which would require me to spend some dollars (not many) but if it can generate service layer code for me that would be money well spent. Does LightSpeed support this type of code generation?

Obviously as the domain model and database schema evolve the services need to be updated to accommodate those changes. .netTiers implements this by generating partial classes. How do these other tools handle those changes without overwriting any custom logic in the service layer?

What other options are there?

Update: Thanks for all the feedback, lots of positive options to look at. Has anyone had a look at MVC Scaffolding?

Update #2: I'm going to pursue the MVCScaffolding option which produces code for EF Code First. Out of the box it produces a Repository class and then somewhat unfortunately sticks it with the Model which in MVC is actually the View Model and not the Domain Model. There is a pull request on the MVCScaffolding project for Service layer scaffolding so will investigate that option. Plus AutoMapper for mapping POCOs <-> DTOs.

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

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

发布评论

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

评论(2

紧拥背影 2024-12-11 20:08:53

T4 模板工作得很好并且很容易修改。它们可用于数据层以及任何服务层。

我们仅用于 EF 代码的过程:

  1. 从数据库构建 edmx
  2. 安装 T4 POCO 类生成模板
  3. 生成 POCO 和上下文类
  4. 删除 edmx 和 T4 模板,保留 POCO 和上下文类

实体框架中的新工具非常有用他们为您提供了各种选项:

模型优先:

  1. edmx 中的模型
  2. 从 edmx 生成数据库
  3. 从 edmx 生成 POCO 类
  4. (可选) 删除 edmx,仅运行代码 代码

优先:

  1. 编写 POCO 类
  2. 生成数据库POCO 类
  3. (可选)从 POCO 类生成 edmx

数据库优先:

  1. 构建数据库
  2. 从数据库生成 edmx
  3. 从 edmx 生成 POCO 类
  4. (可选)删除 edmx,运行仅代码

附录 (14/01/2012):

Code First 迁移的测试版已发布已被释放。我们还没有调查过,但看起来很有趣。

T4 templates work very well and are easily modifiable. They can be used for the datatier layer as well as any service layers.

The process we use for EF Code Only:

  1. Build an edmx from our database
  2. Install the T4 POCO class generation templates
  3. Generate our POCO and context classes
  4. Delete our edmx and T4 templates, keeping the POCO and context classes

The new tools in Entity Framework are great as they give you various options:

Model First:

  1. Model in edmx
  2. Generate database from edmx
  3. Generate POCO classes from edmx
  4. (optional) Delete edmx, run Code Only

Code First:

  1. Write POCO classes
  2. Generate database from POCO classes
  3. (Optional) Generate edmx from POCO classes

Database First:

  1. Build database
  2. Generate edmx from database
  3. Generate POCO classes from edmx
  4. (optional) Delete edmx, run Code Only

Addendum (14/01/2012):

A beta release of Code First Migrations has been released. We haven't investigated it yet but it looks interesting.

临风闻羌笛 2024-12-11 20:08:53

我对实体使用自动映射器 -> dto 翻译

I use automapper for entities -> dto translation

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