using System.Collections.ObjectModel; using System.Linq; using BeWo.Validation; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; using BS.Shared.Extensions; namespace BeWo.ViewModel { public class CustomerTeamVM : AbstractDCMapperVM { public static string PropertyName_Members = "Members"; public static string PropertyName_Name = "Name"; public static string PropertyName_Notice = "Notice"; private ObservableCollection _Members; private string _Name; private string _Notice; public CustomerTeamVM(CustomerTeamDC pDataContract) : base(pDataContract, pDataContract.CustomerTeamOid == null) { } public ObservableCollection Members { get { return _Members; } set { if(AreDifferent(_Members, value)) { _Members = value; FirePropertyChanged(PropertyName_Members); } } } public string Notice { get { return _Notice; } set { if(AreDifferent(_Notice, value)) { _Notice = value; StoreDirtyInformation(DataContract.Notice != value, PropertyName_Notice); FirePropertyChanged(PropertyName_Notice); } } } [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)] public string Name { get { return _Name; } set { if(AreDifferent(_Name, value)) { _Name = value; StoreDirtyInformation(DataContract.Name != value, PropertyName_Name); FirePropertyChanged(PropertyName_Name); } } } protected override void InitByDataContract(CustomerTeamDC pDataContract) { _Members = new ObservableCollection(); if (!IsNew) { if(pDataContract.Members != null) { _Name = pDataContract.Name; _Notice = pDataContract.Notice; _Members.AddRange(pDataContract.Members.OrderBy(m => string.Format("{0},{1}", m.LastName, m.FirstName))); } } _Members.CollectionChanged += (s, e) => StoreDirtyInformation(!pDataContract.Members.ContainsSameItemsAs(_Members), PropertyName_Members); } protected override CustomerTeamDC MapToDataContract(CustomerTeamDC pDataContract, bool doCommit) { pDataContract.Name = _Name; pDataContract.Notice = _Notice; pDataContract.Members = _Members.ToList(); return pDataContract; } } }