Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6474523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:35:49+00:00 2026-05-25T06:35:49+00:00

I’m struggling with some basic MVVM design principles in a Silverlight 4 app using

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:35:49+00:00Added an answer on May 25, 2026 at 6:35 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.