2
votes

I'm struggling with some basic MVVM design principles in a Silverlight 4 app using RIA services & entities. Here's the basic scenario that seems to work OK:

DataViewModel.cs

public DataViewModel : NotificationObject

    private DataDomainContext _dataContext;

    public DataViewModel()  
    {
       _dataContext = new DataDomainContext();

       if (!DesignerProperties.IsInDesignTool)
       {
         Data = _dataContext.Data;
         dataContext.Load(_dataContext.GetDataQuery(), null, null);
       }
    }

    private IEnumerable<DataEntity> _data;
    public IEnumerable<DataEntity> Data   // INPC property
    {
        get { return _data; }
        set
        {
            if (value != _data)
            {
                _data = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Data"));
            }
        }
    }
}

A DataGrid in my view is bound one-way to DataViewModel.Data, and DataDomainContext is the RIA domain context exposed after compiling a domain service for the DataEntity object.

I want to decouple the view model from the DAL. I'd like a DataService class that will take care of asking the domain context for data:

public DataViewModel : NotificationObject

    private DataService _dataService;

    public DataViewModel(DataService dataService)  
    {
       _dataService = dataService;

       if (!DesignerProperties.IsInDesignTool)
       {
         Data = _dataService.Data;
         _dataService.GetData();
       }
    }

    ...
}

However, I can't seem to get it right. Is this easily done? I haven't designed a data service with callbacks before. I tried chaining the Data properties via INPC across the three classes, but the DataGrid in the UI comes up blank. I'd also like to convert to a collection of a new type, DataDto, so my presentation layer is not coupled to the backend. I tried something like this without luck:

DataService.cs

public DataService : INotifyPropertyChanged
{
   public DataService()
   {
     _dataContext = new DataDomainContext();
   }

   public event PropertyChangedEventHandler PropertyChanged;

   public void GetData()
   {
     DataEntities = _domainContext.Data;
     _dataContext.Load(_dataContext.GetDataQuery(), FinishedLoading, null);
   }

   private void FinishedLoading(...)
   {
      Data = DataEntities.Select(de => new DataDto(de));
   }

   public IEnumerable<DataDto> Data { ... }  // INPC property, used for binding in ViewModel

   public IEnumerable<DataEntity> DataEntities { ... }  // INPC property

   ...
}

Am I even on the right track here? Am I missing anything from a high level or do I just not have the details right?

Edit:

I was able to eventually figure this out. The answer involves passing a callback into the data service/repository call via an Action<>. The return type of the call is actually void, and event args are used to deliver the results. I'm happy to post some working code if anyone is interested, just leave a request in the comments.

1
Hi Chris, you should go ahead and post the results, it will help people who find this in the future. As you found out, the data service layer is quite different when done asynchronously as required in Silverlight. It sounds like you found the right solution :-)Joel Cochran

1 Answers

2
votes

I think you're on the right track, but in my opinion your solution is not correct if you are, in fact, trying to decouple your view model from your data service. I am working on an app very similar to this right now. Different people have different ideas about mvvm, and this is just my personal approach that I have learned from trial and error (using visual studio):

Start off by creating silverlight app project and hosting it in a .web project. The silverlight project will hold the views and view models. The view models should contain your models, not you're data service! However, the view models should have an instance of your data service to set the models. Where is my data service? I'm glad you asked :). Add another project, a WCF RIA Services Class Library. That is actually two projects, a ria service (server side) dll and a corresponding silverlight (client side) dll. You can add your entity framework or other database access code to the server side. After that, add a domain service to the server side project. Build it first (important) and then go to the client side ria service dll and create a data service class with your methods for the data service like so:

public void GetData( string filter, Action<LoadOperation<MyEntityType>> callback )
    {
        var q = from e in _context.GetDataQuery()
                where e.SomeField.Contains(filter)
                select e;
        _context.Load(q, LoadBehavior.RefreshCurrent, callback, null);
    }

Your data service should not implement Inotifyproperty changed because that is the view models role! Reference the ria service client side dll in your silverlight project, and also reference the ria service server side dll in your web host project the view model should call this data service like so:

IEnumerable<MyEnityType> Model {get;set;}
//NOTE: add notify property changed in setter!

private void GetData()
{
    _myDataService.GetData( _filter, loadOperation =>
            {
                if ( loadOperation.HasError )
                    HandleError(loadOperation.Error);    
                Model = loadOperation.Entities;
            } );
}

You can take it a step further and implement an interface for the data service if you really want to decouple them. Taking this approach allows for re-usability of your data service (in case you want a desktop app or phone app) I hope this helps clear things up!