Help with MVVM and PRISM

Topics: Prism v2 - Silverlight 2
Jun 5, 2009 at 12:56 PM
Edited Jun 5, 2009 at 12:57 PM

hello,

I have started building an application with PRISM for Silverlight and in my Module I'm using the MVVM design pattern. However I have a feeling I'm doing something wrong because... I don't have the Model bit of the pattern ! Let me paste the relevant code :

ViewModel :

 

   15  public class SourceListViewModel

   16   {

   17     private ISourceManagerService _sourceManagerService;

   18     private IEventAggregator _aggregator;

   19 

   20     public SourceListViewModel(ISourceManagerService sourceManagerService,

   21                               IEventAggregator aggregator)

   22     { 

   23         Sources = new ObservableCollection<SourceDto> { new SourceDto() { Title = "some title" } };

   24         _sourceManagerService = sourceManagerService;

   25         _aggregator = aggregator;

   26 

   27         _aggregator.GetEvent<SourceEvent>().Subscribe(OnSearchComplete);

   28         _sourceManagerService.BeginSearch("air");         

   29     }

   30 

   31     public void OnSearchComplete(SourceDtoEventArgs args)

   32     {

   33       Sources.Clear();

   34       foreach (SourceDto source in args.SourceList)

   35       {

   36         Sources.Add(source);

   37       }

   38     }

   39 

   40     public ObservableCollection<SourceDto> Sources

   41     {

   42       get;

   43       private set;

   44     }

   45   }

View :

 

   10 public partial class SourceListView : UserControl

   11   {

   12     public SourceListView(SourceListViewModel viewModel)

   13     {

   14       InitializeComponent();

   15       DataContext = viewModel;

   16     }

   17   }

 

Service:  (I'm making a call to a WCF service from here) :

 

 

   11 public class SourceManagerService : ISourceManagerService

   12   {

   13     private IEventAggregator _aggregator;

   14     private MetricaServiceClient _client;

   15     

   16 

   17     public SourceManagerService(IEventAggregator aggregator)

   18     {

   19       _client = new MetricaServiceClient("BasicHttpBinding_ICompService");

   20

   21 

   22       _aggregator = aggregator;

   23     }

   24 

   25     #region ISourceManagerService Members

   26 

   27     public void BeginSearch(string query)

   28     {

   29       _client.GetSourceByTitleCompleted += GetSourceByTitleCompleted;

   30       _client.GetSourceByTitleAsync(query);

   31     }

   32 

   33     #endregion

   34 

   35     private void GetSourceByTitleCompleted(object sender, GetSourceByTitleCompletedEventArgs e)

   36     {

   37       _aggregator.GetEvent<SourceEvent>().Publish(new SourceDtoEventArgs {SourceList = e.Result});

   38     }

   39   }

 

As you can see I don't have any Model per see. The business classes such as SourceDto are automatically generated when i reference my WCF service.

 

What do you think of my code ? Is there a way I can improve it ? It's my first attempt with PRISM and MVVM so I'd really appreciate some insights.

 

Thanks !

 

Jun 5, 2009 at 9:07 PM
Edited Jun 5, 2009 at 9:07 PM

Hi graphicsxp,

 

I don't know your exact scenario, but the MVVM pattern is a presentation pattern (it is implemented in the presentation layer). The Model bit of the MVVM is usually also called ViewModel because it is not necessarily references the business classes, sometime it's an intermediate abstraction between the presentation layer and the business layer.

Having said that, it's not wrong if your views interacts with the business objects through a viewModel that consumes the actual business services.

 

The following links deal with the MVVM and other presentation patters that might help you:

·         MVVM discussed in the Composite Application Guidance Knowledge base: Model View ViewModel (MVVM)

·         Separated Presentation in the Prism documentation.

·         Several presentation patters explained by Derek Greek , which are well explain in my personal opinion: Interactive Application Architecture

 

 

Hope it helps!

 

Matias Bonaventura

http://blogs.southworks.net/matiasb