我正在寻找一种有效的方法来创建一个动态CrudViewModelBase<TConext, TEntity>
,它将用作应用程序中所有ViewModel
的原型,它们将执行CRUD操作。
我不知道实例化DomainContext
的有效方法在哪里,应该是应用程序级别的?视图模型级?请与您分享您的经验。
我对MVVM很陌生,我想创建一个可重用的ViewModelBase来执行这些操作。
任何链接,代码示例或推荐都会非常受欢迎。
我开始写一些东西(我也是RIA的新手),我会出去几个小时抱歉推迟评论,并感谢合作:
Imports System.ServiceModel.DomainServices.Client
Imports Microsoft.Practices.Prism.ViewModel
Imports Microsoft.Practices.Prism.Commands
Imports CompleteKitchens.Model
Namespace ViewModel
Public MustInherit Class CrudViewModel(Of TContext As DomainContext, TEntity As Entity)
Inherits notificationobject
Protected Sub New(context As DomainContext, query As EntityQuery(Of TEntity))
m_Context = context
m_Query = query
End Sub
Private ReadOnly m_Context As TContext
Protected ReadOnly Property Context() As TContext
Get
Return m_Context
End Get
End Property
Private ReadOnly m_Query As EntityQuery(Of TEntity)
Protected ReadOnly Property Query As EntityQuery(Of TEntity)
Get
Return m_Query
End Get
End Property
Private m_IsLoading As Boolean
Public Overridable Property IsLoading As Boolean
Get
Return m_IsLoading
End Get
Protected Set(value As Boolean)
m_IsLoading = value
RaisePropertyChanged(Function() IsLoading)
End Set
End Property
Private m_Items As IEnumerable(Of TEntity)
Public Property Items() As IEnumerable(Of TEntity)
Get
Return m_Items
End Get
Set(ByVal value As IEnumerable(Of TEntity))
m_Items = value
RaisePropertyChanged(Function() Items)
End Set
End Property
Private m_CanLoad As Func(Of Boolean)
Protected Overridable ReadOnly Property CanLoad As Func(Of Boolean)
Get
If m_CanLoad Is Nothing Then m_CanLoad = Function() True
Return m_CanLoad
End Get
End Property
Private m_LoadCommand As ICommand
Public ReadOnly Property LoadCommand() As ICommand
Get
If m_LoadCommand Is Nothing Then m_LoadCommand = New delegatecommand(AddressOf Load, CanLoad())
Return m_LoadCommand
End Get
End Property
Private Sub Load()
IsLoading = True
operation = Context.Load(Query, False)
End Sub
Private m_CanCancel As Func(Of Boolean) = Function() operation.CanCancel
Protected Overridable ReadOnly Property CanCancel As Func(Of Boolean)
Get
Return m_CanCancel
End Get
End Property
Private m_CancelCommand As ICommand
Public ReadOnly Property CancelCommand() As ICommand
Get
If m_CancelCommand Is Nothing Then m_CancelCommand = New DelegateCommand(AddressOf Cancel, CanCancel())
Return m_CancelCommand
End Get
End Property
Private Sub Cancel()
operation.Cancel()
End Sub
Private WithEvents operation As LoadOperation(Of TEntity)
Private Sub operation_Completed(sender As Object, e As EventArgs) Handles operation.Completed
If operation.IsComplete Then
Items = operation.AllEntities
ElseIf operation.IsCanceled Then
End If
End Sub
End Class
End Namespace
答案 0 :(得分:1)
正如头晕目眩的说,虚拟机和存储库都关注不同的事情。就像ASP.net MVC一样,你不会尝试将这些东西放在你的控制器中。 ViewModel提供了显示和GUI事物的模型。将ViewModel视为显示。远离视图,您应该能够通过测试ViewModel来测试视图的功能。
在我目前的项目中,我正在从ViewModel调用我的RIA域服务,然后将结果映射到我的VM。如果要使用存储库模式,请从silverlight firestarter事件中下载代码。 {3}}会议3 Dan Wahlin就是一个例子。该视频也很好看。
Shawn Wildermuth的这个例子显示客户端模型实际上与RIA服务进行了对话。我还没有实现它,但它对我来说更有意义,因为他的例子中的模型对我来说更像是一个控制器。 http://channel9.msdn.com/Series/Silverlight-Firestarter/Silverlight-Firestarter-2010-Session-3-Building-Feature-Rich-Business-Apps-Today-with-RIA-Services
我个人不喜欢将数据模型实体直接绑定到ViewModels。在我当前的项目中,我没有选择,因为所有访问都是通过procs完成的,所以我将proc结果映射到ViewModels。如果我确实有表访问权限,我可能仍会将数据模型实体映射到ViewModels。我不确定mvvm模式中的“正确”,但它允许您保持数据模型实体清除显示/验证属性。