New Posts New Posts RSS Feed: When use ItemViewModel
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

When use ItemViewModel

 Post Reply Post Reply
rbautistaole View Drop Down

Joined: 01-Apr-2011
Posts: 37
Post Options Post Options   Quote rbautistaole Quote  Post ReplyReply Direct Link To This Post Topic: When use ItemViewModel
    Posted: 11-Jun-2012 at 7:16pm
I has been studying the TempHire sample, and i observed that some entities are wrapped to ViewModels to be added to ObservableCollections like the Phone and Adress entities and another entities are not wrapped like Rate and Skill entities. My questios is which criteria is used to wrap or not wrap the entities in ViewModels?
thanks ind advanced
Ramiro Bautista.
Back to Top
mgood View Drop Down

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 11-Jun-2012 at 11:41pm
The criteria used to wrap an entity is whether you need additional properties and/or methods specifically for the UI. UI logic never belongs on the entity itself. The entity is a data container for your business data. It models your business not the UI. It's the responsiblity of a ViewModel to provide UI logic around your entities. So, in TempHire, for the addresses and phone numbers, I need a property to enable/disable the Delete button next to each item in the UI based on the state of the entity. This is pure UI logic and therefore the entity is wrapped in an ItemViewModel.
The following is the property for the AddressItemViewModel. It only enables the Delete button if there's more than one address and the current address is not the primary address.
        public bool CanDelete
            get { return !Item.Primary && (Item.StaffingResource.Addresses.Count > 1); }
which is then bound to the IsEnabled attribute of the Button.
                            <Button Width="60"
                                    IsEnabled="{Binding CanDelete}" />
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down