开发者

Dynamic ViewModelBase<TContext, TEntity> for CRUD operations using RIA

I am looking for an efficient way to create a dynamic CrudViewModelBase<TConext, TEntity> that will be used as a protortype for all the ViewModels in the application, that are going to perform CRUD operations.

I don't know where is the efficient way to instantiate the DomainContext, should be application-level? ViewModel-level? please share me with your experience.

I am pretty new to MVVM, and I want to create a reusable ViewModelBase to perform these operation.

Any links, code-samples, or recommendations will be really welcommed.

I start writing some stuff (I am new to RIA as well), I will be out for few hours sorry for delay in comments, and thanks for cooperating:

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开发者_C百科 = 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


As giddy stated VM's and repositories are concerned with separate things. Just like with ASP.net MVC you wouldn't try and put that stuff in your controller. The ViewModel provides a model of the display and GUI things. Think of the ViewModel as the display. Take the view away and you should be able to test the functionality of the view by testing the ViewModel.

In my current project I'm making calls to my RIA domain services from the ViewModel and then mapping the results to my VM. If you want to use the repository pattern download the code from the silverlight firestarter event. http://channel9.msdn.com/Series/Silverlight-Firestarter/Silverlight-Firestarter-2010-Session-3-Building-Feature-Rich-Business-Apps-Today-with-RIA-Services session 3 Dan Wahlin, there is an example of this. The video is a good watch also.

This example by Shawn Wildermuth shows the client side model actually talks to RIA services. I haven't implemented it yet but it makes more sense to me as the model in his example feels more like a controller to me. http://wildermuth.com/2010/04/16/Updated_RIA_Services_MVVM_Example

I personally don't like binding Data model entities directly to ViewModels. In my current project I don't have a choice because all access is done through procs so I map proc results to ViewModels. If I did have table access I would probably still map data model entities to ViewModels. I'm not sure that's "correct" in the mvvm pattern but it allows you to keep your data model entities clean of display/validation attributes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜