0

Scenario: Need to gather a collection of objects from a web-service and present them in the GUI. That objects contain references to another objects (foreign keys), and I need to represent this related objects as well.

I've come with two possible options with their respective drawbacks:

  1. Send "trough the wire" the whole related objects.
  2. Send just the foreign keys and retrieve the related objects querying another collection (previously stored locally)

1st option makes the data-binding (I'm using WPF at the client-side) straight-forward, although it introduces a significant overhead in the communication (specially if the same related objects appear a lot of times). Thus, it seems like it is not the way to go.

2nd option achieves much smaller messages and it seems like it's the way to go. However, I can't find the way to perform the data-binding to this related objects.

Let me clarify the issue with an example:

Object -> Room || Related Object -> Building || Locally stored collection -> Buildings

The message coming from the Web Service contains room attributes (let's say "name", "size" ...) and the "BuildingId". I want to be able to show (automatically via data-binding) in the UI the information from the building, not just the "BuildingId". Thus, it seems logical that I must query the "Buldings" collection in order to get the proper Building Object, and then use its properties to populate the UI.

So, my questions are:

  1. How do I achieve that via data-binding in WPF? I guess there's an automatic way to achieve that. Isn't there?
  2. Is there any underlying concept (related to data-binding) which I'm missing? I'm new to the .NET Stack, so maybe this is easier than it looks to me!

Thank you guys! ;)

P.S.: Any documentation/link which could point me in the right direction would be highly appreciated ;)

2 Answers 2

1

To try and relate your two options to typical approaches in the .Net/WPF world (in reverse order):

Approach 2: The Model-View-ViewModel approach (MVVM) may be one way to go. Your DTOs are designed to optimise message passing using WCF (the Model). MVVM principles suggest you should not pollute them with concerns such as data binding. A separate class, the ViewModel, takes responsibility for that and can also manage issues such as mapping primitive identifiers (e.g. BuildingId) into bindable representations of objects using cached lookups etc. Rather than binding to your DTOs, your WPF Views then bind to the ViewModel.

If you want to pursue this option, get your head around the MVVM concept and then you may want to consider a tool to assist. (I find Caliburn Micro to be excellent but there are a few out there for WPF)

Approach 1: This is the simpler approach and means you write your WCF service so that it is supplying prefabricated ViewModels from the server to the client (i.e. your DTO and ViewModel are the same thing). As you have already observed, since responsibility for constructing the ViewModels is done at the server you may have issues with transmitting redundant data. This approach also means the client and server are very tightly coupled - each DTO/ViewModel is designed to be used in a specific View.

Ultimately, the best approach depends on the requirements for your application. If you're looking for a simple approach where you are not concerned about some redundant data communication and just want a solution ASAP, then Approach 1 is probably preferable.

However, if there are other clients you need to service, or it is important to avoid coupling the client and server for other reasons or efficient communication is required, then the MVVM approach should be considered.

3
  • Thanks Phil, your answer and links have helped me a lot clarifying this situation. Just wanted to ask you one more thing: Given that there will be like 10000 Rooms and just 10 Buildings ... wouldn't be stupid to go with the approach #1? I mean, it would create a lot of "repeated" objects. It's not an "efficient communication" issue, it's also about possible memory issues, isn't it? Dec 11, 2012 at 1:25
  • There's not enough information to indicate which approach would be preferable, and certainly not enough to indicate that one approach would be "stupid". I regard MVVM as a very powerful (and simplifying) architecture that should generally be used for client apps unless there are compelling reasons not to. Dec 11, 2012 at 2:07
  • Memory is not as much an issue as data communications. Assuming say 200 bytes each for a building object, absolute worst case for 10,000 redundant building objects would be 2MB - really pretty miniscule in memory terms. But if you were retrieving all 10,000 rooms it adds 2MB to each message. On a low bandwidth connection that could be very signficant. Dec 11, 2012 at 2:13
1

I would have a property on the Room object called Building, which can be empty (null) initially but set later when you load the building object. Your room object should implement INotifyPropertyChanged which will notify the UI when the object is finally loaded and cause all the bindings to refresh.

Sample INotifyPropertyChanged implementation:

private bool _Active;
public bool Active
{
    get { return _Active; }
    set
    {
        if (_Active == value)
            return;

        _Active = value;
        OnPropertyChanged("Active");
    }
}

#region PropertyChanged Implementation

        // Code Below Copied From : http://www.codeproject.com/KB/cs/PropertyNotifyPart1.aspx
        /// <summary>
        /// Raises the <see cref="E:PropertyChanged"/> event.
        /// </summary>
        /// <param name="propertyName">
        /// Name of the property that changed.
        /// </param>
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
            OnPropertyChanged(e);
        }

        /// <summary>
        /// Raises the <see cref="E:PropertyChanged"/> event.
        /// </summary>
        /// <param name="e">
        /// The <see cref="PropertyChangedEventArgs"/> instance
        /// containing the event data.
        /// </param>
        protected void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler temp = this.PropertyChanged;
            if (null != temp)
                temp(this, e);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler temp = this.PropertyChanged;
            if (null != temp)
                temp(sender, e);
        }

        #endregion
4
  • Hi Malcolm, thanks for your answer. However, I think you're not getting my doubt (probably I have asked it badly). I'm already using CollectionViewSource. With the 1st option the data-binding is trivial (let's say "Building.Name"), but with the 2nd one not (would be trivial if I could write something like Buildings.getById(BuildingId).Name), and that's my problem ;) Dec 10, 2012 at 16:51
  • 1
    I think I understand your issue more now, does the new answer make sense? Dec 10, 2012 at 17:03
  • Yeah, now it makes sense. Anyway, I don't like that approach. I mean, I see that way I can get the job done ... but thought that a cleaner way (without introducing extra-properties in the DTOs) should be possible. I'll wait until more answers (if any) arrive before accepting yours. Thank you very much for your time and help Mr Malcom! Dec 10, 2012 at 17:12
  • You shouldn't add properties like that to DTOs. You should have some sort of wrapper class for your DTO that adds functionality specific to your application. Dec 10, 2012 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.