Print Page | Close Window

When use ItemViewModel

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3492
Printed Date: 11-Jul-2020 at 5:06pm


Topic: When use ItemViewModel
Posted By: rbautistaole
Subject: When use ItemViewModel
Date Posted: 11-Jun-2012 at 7:16pm
Hi.
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.



Replies:
Posted By: mgood
Date 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"
                                    Height="25"
                                    Margin="2"
                                    HorizontalAlignment="Left"
                                    cal:Message.Attach="Delete($dataContext)"
                                    Content="Delete"
                                    IsEnabled="{Binding CanDelete}" />



Print Page | Close Window