使用现有数据层和业务层的 MVC
我有一个带有数据层(主要 EF4)、业务层(自定义代码)和 Windows 应用程序的现有应用程序 - 现在我想使用 ASP.NET MVC 创建一个 Web 应用程序,但我不确定具体要做什么,特别是在我的模型中。
当我的数据和业务逻辑已经存在时,与 MVC Music Store v2.0 (http://mvcmusicstore.codeplex.com/) 等参考项目相比,我应该如何构建我的模型和控制器?我的所有实体都存储在我的数据层中,所有方法和逻辑都存储在我的业务层中,所以我想我不需要模型(除非我需要特定的 Web 模型),并且我想我的控制器只需要调用我的业务层中的方法作为我会在普通的网络表单应用程序中吗?
另一个问题,如果我需要在一页上显示 2 个列表,其中包含来自 2 个不同实体的数据,我想我需要创建具有 2 个属性的 af 模型(每个实体一个)?
现在的最后一个,如果由于某种原因,例如我的业务层的 Get 方法返回异常,应该如何在我的控制器/视图中处理?
I have an existing application with a datalayer (primary EF4), a businesslayer (custom code) and a windows application - now I want to create a webapplication using ASP.NET MVC but I am not sure exactly what to do especially in my models.
When my data and business logic already exist how should I structure my Models and Controllers compared to a referenceproject like MVC Music Store v2.0 (http://mvcmusicstore.codeplex.com/)? All my entities are stored in my datalayer and all my methods and logic are stored in my businesslayer so I guess I need no Models (unless I need specific web Models) and I guess my controllers will only need to call the methods in my businesslayer as I would to in a normal webform application?
Another question, if I need to display 2 lists with data from 2 different entities on 1 page I guess I need to create af Model with 2 properties (one for each entity)?
The last one for now, if, for some reason, e.g. a Get method from my businesslayer returns an exception how should this be handled in my Controller/View?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您的应用程序的复杂性。我至少会为每个视图引入 ViewModel,以便您可以为视图提供所需的数据。如果您的应用程序重量轻,那么在控制器中使用业务层可能会很好。但是,您可能希望引入一个与业务层交互的服务层,只是为了保持控制器的精简。
至于异常处理,您可能需要查看 HandleError 属性。
It depends on the complexity of your application. I would at the very least introduce ViewModels for each view so you can supply the view with the data that is required. If your application is light weight it might be fine to use your business layer in the controller. However, you might want to introduce a service layer that interacts with your business layer just to keep the controllers thin.
As for exception handling, you might want to look into the HandleError attribute.