Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to refactor RIA data service calls to a service layer?
    primarykey
    data
    text
    <p>I'm struggling with some basic MVVM design principles in a Silverlight 4 app using RIA services &amp; entities. Here's the basic scenario that seems to work OK:</p> <p><strong>DataViewModel.cs</strong></p> <pre><code>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&lt;DataEntity&gt; _data; public IEnumerable&lt;DataEntity&gt; Data // INPC property { get { return _data; } set { if (value != _data) { _data = value; PropertyChanged(this, new PropertyChangedEventArgs("Data")); } } } } </code></pre> <p>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. </p> <p>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:</p> <pre><code>public DataViewModel : NotificationObject private DataService _dataService; public DataViewModel(DataService dataService) { _dataService = dataService; if (!DesignerProperties.IsInDesignTool) { Data = _dataService.Data; _dataService.GetData(); } } ... } </code></pre> <p>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:</p> <p><strong>DataService.cs</strong></p> <pre><code>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 =&gt; new DataDto(de)); } public IEnumerable&lt;DataDto&gt; Data { ... } // INPC property, used for binding in ViewModel public IEnumerable&lt;DataEntity&gt; DataEntities { ... } // INPC property ... } </code></pre> <p>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? </p> <p><strong>Edit:</strong></p> <p>I was able to eventually figure this out. The answer involves passing a callback into the data service/repository call via an Action&lt;>. 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload