ASP.NET MVC 模式
我对 MVC 相当陌生,但在使用它(MVC 3/Razor)之后,我就着迷了。
我有几个问题:
1)开发 MVC 应用程序的最佳或最广泛使用的模式是什么?存储库、DDD、UOW?
2) 我正在使用实体框架 4,所以有人可以向我解释一下或者给我指出一个可以解释带有 EF4 的存储库模式的好来源吗? EF4不就是作为业务层和数据访问层发生的吗?存储库模式是否提供了好处?
3)另外,最后一个问题,有人可以解释一下控制器、模型和视图之间的整个关系吗?我了解了基础知识,但也许更深入地了解了它的正确使用方法。视图模型 - 假设我有一个显示客户信息的视图和一个编辑该信息的视图,我应该有一个视图模型和一个编辑模型,还是可以传递?
4) 例子??
感谢您之前的帮助,
$("Sam")
** 编辑 **
我在正确的轨道上吗:
Public Class HomeController
Inherits System.Web.Mvc.Controller
Function Index(ByVal id As Integer) As ActionResult
Return View(New HomeModel)
End Function
<HttpPost()> _
Function Index(ByVal Model As HomeModel) As ActionResult
Return View(Model)
End Function
End Class
Public Class HomeModel
Private _Repository As IRepository(Of Customer)
Public Property Customer As Customer
Public Sub New()
End Sub
Public Sub New(ByVal ID As Integer)
_Repository = New CustomerRepository
Customer = _Repository.GetByID(ID)
End Sub
End Class
Public Interface IRepository(Of T)
Function GetByID(ByVal ID As Integer) As T
Sub Add(ByVal Entity As T)
Sub Delete(ByVal Entity As T)
End Interface
Public Class CustomerRepository
Implements IRepository(Of Customer)
Public Sub Add(ByVal Entity As Customer) Implements IRepository(Of Customer).Add
End Sub
Public Sub Delete(ByVal Entity As Customer) Implements IRepository(Of Customer).Delete
End Sub
Public Function GetByID(ByVal ID As Integer) As Customer Implements IRepository(Of Customer).GetByID
Return New Customer With {.ID = ID, .FirstName = "Sam", .LastName = "Striano"}
End Function
End Class
Public Class Customer
Public Property ID As Integer
Public Property FirstName As String
Public Property LastName As String
End Class
I am fairly new to MVC, but after playing with it (MVC 3/Razor), I am hooked.
I have a few questions:
1) What is the best, or most widely used pattern to develop MVC apps in? Repository, DDD, UOW?
2) I am using the Entity Framework 4, so could some please explain to me or point me to a good source that will explain the Repository Pattern w/EF4? Doesn't EF4 take place as the business layer and the data access layer? Does the Repository Pattern even provide a benefit?
3) Also, one last question, could someone explain the whole relationship between the Controller, the Model and the View? I get the basics, but maybe a little more in depth of the correct way to use it. View Models - Say I have a view that displays customer info, and one that edits it, should I have a view model and an edit model, or can the be passed around?
4) Examples??
Thanks for the help up front,
$("Sam")
** EDIT **
Am I on the right track here:
Public Class HomeController
Inherits System.Web.Mvc.Controller
Function Index(ByVal id As Integer) As ActionResult
Return View(New HomeModel)
End Function
<HttpPost()> _
Function Index(ByVal Model As HomeModel) As ActionResult
Return View(Model)
End Function
End Class
Public Class HomeModel
Private _Repository As IRepository(Of Customer)
Public Property Customer As Customer
Public Sub New()
End Sub
Public Sub New(ByVal ID As Integer)
_Repository = New CustomerRepository
Customer = _Repository.GetByID(ID)
End Sub
End Class
Public Interface IRepository(Of T)
Function GetByID(ByVal ID As Integer) As T
Sub Add(ByVal Entity As T)
Sub Delete(ByVal Entity As T)
End Interface
Public Class CustomerRepository
Implements IRepository(Of Customer)
Public Sub Add(ByVal Entity As Customer) Implements IRepository(Of Customer).Add
End Sub
Public Sub Delete(ByVal Entity As Customer) Implements IRepository(Of Customer).Delete
End Sub
Public Function GetByID(ByVal ID As Integer) As Customer Implements IRepository(Of Customer).GetByID
Return New Customer With {.ID = ID, .FirstName = "Sam", .LastName = "Striano"}
End Function
End Class
Public Class Customer
Public Property ID As Integer
Public Property FirstName As String
Public Property LastName As String
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用在服务类中实例化的通用存储库(使用 Ninject 的依赖注入)。
服务类本质上执行两个功能:
它提供控制器将使用的所有方法。
它有一个名为 ViewModel 的属性,本质上将视图所需的数据映射到 MyViewModel 类。
控制器使用服务类。使用这种“模式”,您的控制器看起来像:
ViewModel 类仅保存显示数据,没有方法(除了从另一个属性(例如 List<> 对象)检索数据的奇怪的非常简单的方法)。
效果非常好。服务类的一个例子:
可能不是每个人的选择,但是嘿,它对我有用......并且(更重要的是)我的客户和他们的用户。
编辑
按照下面评论中的要求,我使用的存储库:
和 IRepository 接口:
I use generic repositories that get instantiated in a service class (using Dependency Injection with Ninject).
The service class essentially performs two functions:
It provides all the methods that the controller will consume.
It has a property called ViewModel, that essentially maps the data that the views need into a MyViewModel class.
The Controller consumes the service class. With this "pattern", your controllers look like:
The ViewModel class only hold display data and no methods (except the odd really simple method to retrieve data from another property that is, for example a List<> object).
Works really well. An example of a service class:
May not be everyone's choice, but hey, it works for me... AND (more importantly) my clients and their users.
Edit
As requested in the comment below, the Repository that I use:
And the IRepository Interface:
这应该可以帮助您入门。有很多教程和视频可供使用;例如:
也可访问 www.asp.net;有一些教程/示例可以帮助您入门。例如 音乐商店示例
不幸的是,我对 EF4/Repository 模式不太熟悉。但是这里有一个关于此模式的博客文章。
This should help you getting started. There are a lot of tutorials and videos available; for example:
And also at www.asp.net; there are a few tutorials/examples to help you getting started. For example the Music Store sample
Unfortunately, I'm not so familiar with EF4/Repository pattern. But here's a blogpost about this pattern.
1)我想说存储库模式是使用最广泛的,然后也有控制反转。
2)我无法真正指出使用实体框架存储库的好处,除了控制器不应该知道如何访问数据而不是询问存储库。这使得有时可以轻松地将其切换出去。
您还可以预先加载数据,以确保视图不会在 foreach 的每次迭代中调用数据库,例如显示来自子实体的数据的用户集合。无论如何,您可能可以执行此操作,但我认为存储库是执行此操作的正确位置。
3)我无法更深入地告诉你这个概念,但我可以告诉你一些关于视图模型的信息。在我看来,只有当您想要发送到视图的实体不止一个时,您才应该使用视图模型,例如国家/地区列表。您还可以使用视图模型来“展平”非常复杂的对象。
1) I would say that the repository pattern is the most widely used, then there is inversion of controll too.
2) I can't really point out the benefits with using a repository for entity framework other than that the controller should not know about how to acces data other then asking a repository. This makes it easy to switch it out sometime.
You can also eager load the data to make sure that the view don't call the database in every iteration of a foreach, for example a collection of users to display data from a child entity. You can probly do this anyway, but I feel that the repository is the right place to do it.
3) I can't tell you about the concept in a more in depth way, but I can tell some about viewmodels. In my opinion you should only use viewmodels if there is anything more then one entity you want to send to the view, for example a list of countries. You can alo use a viewmodel to "flatten" out very complex objects.
我会挑衅地说存储库模式被大量使用。此模式可以与依赖注入一起使用。使用依赖注入使单元测试变得轻而易举,因为您可以将不同的存储库捕捉到抽象存储库。查看 http://ninject.org/ 以获得简单易用的 .NET 依赖注入器。
视图模型应该保存显示数据并将该数据从控制器传输到视图。如果您想编辑和显示客户信息,请查看 这个
I would defiantly say the repository pattern is used a lot. This pattern can be used with Dependency Injection. Using Dependency Injection makes Unit Testing a breeze because you can snap different repositories to an abstract repoistory. Check out http://ninject.org/ for a simple to use Dependecy injector for .NET.
View Models should hold display data and transfer that data from the controller to the view. If you want to edit and display customer info, take a look at this