这个特定的服务类别在我的域中应该是什么样子?
我来自 WebForms 世界,其中所有逻辑都位于 aspx 页面的代码隐藏中。在阅读了几本有关 ASP.NET MVC 的书籍、听了一些播客并观看了 Tekpub 上的一些视频之后,我决定是时候以不同的方式处理问题了。
不幸的是,我已经被困住了。
我正在尝试构建某种小型且基本的 CMS,我可以在其中添加多个网站。
我知道我应该保持控制器的精简,所以我想我应该使用某种服务类(我们称之为 WebsiteService)来做到这一点。我使用实体框架进行数据访问,我的视图都使用特定的视图模型。当我创建或编辑网站时,应该发生以下四件事:
- 验证输入
- 将有关网站的信息添加到数据库(如果是编辑则更新信息)
- 在磁盘上创建目录(如果是编辑则可能重命名该目录)
- 添加IIS 网站的主机头(如果是编辑,则可以删除旧主机头和新主机头)
基本上,我想 WebsiteService 应该执行更高级的验证,写入数据库,创建/编辑目录,添加/删除主机头并向控制器返回一些内容以指示它是否成功。
这个类应该是什么样的?我有几个问题我不知道答案。
- WebsiteService 是否还应该将 CreateWebsite ViewModel 转换为实际的 Website 类,或者是否应该执行其他操作以便 WebsiteService 接受实际的 Website 对象?
- 基本输入验证是通过使用 ViewModel 上的 Validation 属性来完成的。还应该进行更广泛的验证(“数据库中是否已经存在具有该域名的网站?”)。 WebsiteService 也应该这样做吗?
- 所有 3 个步骤(保存到数据库、创建目录、向 IIS 添加主机头)是否应该在一个公共方法 (
WebsiteService.SaveWebsite(ViewModels.CreateWebsite website)
) 中完成,或者我应该提供单独的方法控制器必须打电话吗? (我想不是因为我认为调用顺序很重要。)
I'm coming from a WebForms world where all logic is located in the codebehind of the aspx-pages. After reading a few books on ASP.NET MVC, listening to some podcast and watching some videos on Tekpub, I've decided that it is time to approach things a bit differently.
Unfortunately, I'm already stuck.
I'm trying to build some sort of small and basic CMS in which I can add multiple websites.
I know I should keep my controllers thin, so I guess I should use some sort of service class (let's call it WebsiteService) to do this. I'm using Entity Framework for data access and my Views all use specific ViewModels. When I Create or Edit a Website, these four things should happen:
- Validate the input
- Add information about the Website to the database (Update information if it's an Edit)
- Create a directory on disk (Possibly rename the directory if it's an Edit)
- Add a host header to an IIS Website (Possible remove the old host header and a new one if it's an Edit)
Basically, I guess the WebsiteService should perform more advanced validation, write to the database, create/edit a directory, add/remove host headers and return something to the controller to indicate if it succeeded or not.
What should this class look like? I have a few questions to which I don't know the answer.
- Should the WebsiteService also translate the CreateWebsite ViewModel to the actual Website class or should something else do this so that the WebsiteService accepts an actual Website object?
- Basic input validation is done by using Validation attributes on the ViewModel. More extensive validation ("Is there already a Website with this domain name in the database?") should also be done. Should the WebsiteService do this as well?
- Should all 3 steps (save to database, create directory, add host header to IIS) be done in one public method (
WebsiteService.SaveWebsite(ViewModels.CreateWebsite website)
) or should I provide separate methods which the controller has to call? (I guess not because I suppose the call order is important.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题的答案部分是主观的,见仁见智,但以下是我的想法:
它可以。但更好的方法是使用 AutoMapper 来为您完成此操作。数据驱动的 MVC 应用程序“必须拥有”的开源工具之一。
是的 - 输入验证应该通过视图模型上的验证属性来完成。更广泛的/域/业务验证可以在服务中完成,也可以在域模型本身上完成。我更喜欢(并使用)第二个选项。我的域模型也是我的 POCO(由 EF 使用),但我有特定于域(但不是数据库)的额外方法/属性。
不,我认为这应该在单独的方法中,主要是为了可测试性/关注点分离。您需要进行三件事 - 数据库持久性、文件写入和 IIS 工作。对我来说这应该是三种不同的服务。我会让您的“WebsiteService”将这些任务委托给其他服务。
The answers to this is partially subjective, matter of opinion, but here are my thoughts:
It could. But a better approach would be to use AutoMapper to do this for you. One of the "must have" open source tools for data-driven MVC applications.
Yes - input validation should be done via validation attributes on the View Models. More extensive/domain/business validation can either be done in the service, or on the domain models themselves. I prefer (and use) the second option. My domain models are also my POCO's (used by EF), but i have extra methods/properties specific to the domain (but not the database).
No, i think this should be in seperate methods, mainly for testability/seperation of concerns. You've got three things going on - database persistence, file writing and IIS work. That to me should be three different services. I would have your "WebsiteService" delegate these tasks to other services.