protected AddRemoveDetailEntitiesViewModel(IUnitOfWorkFactory <TUnitOfWork> unitOfWorkFactory, Func <TUnitOfWork, IRepository <TEntity, TPrimaryKey> > getRepositoryFunc, Func <TUnitOfWork, IRepository <TDetailEntity, TDetailPrimaryKey> > getDetailsRepositoryFunc, Func <TEntity, ICollection <TDetailEntity> > getDetailsFunc, TPrimaryKey id) : base(unitOfWorkFactory, getRepositoryFunc, null) { this.getDetailsRepositoryFunc = getDetailsRepositoryFunc; this.getDetailsFunc = getDetailsFunc; SelectedEntities = new ObservableCollection <TDetailEntity>(); if (this.IsInDesignMode()) { return; } LoadEntityByKey(id); Messenger.Default.Register(this, (EntityMessage <TDetailEntity, TDetailPrimaryKey> m) => { if (m.MessageType != EntityMessageType.Added) { return; } var withParent = m.Sender as ISupportParentViewModel; if (withParent == null || withParent.ParentViewModel != this) { return; } var withEntity = m.Sender as SingleObjectViewModel <TDetailEntity, TDetailPrimaryKey, TUnitOfWork>; var detailEntity = DetailsRepository.Find(DetailsRepository.GetPrimaryKey(withEntity.Entity)); if (detailEntity == null) { return; } DetailEntities.Add(detailEntity); SaveChangesAndNotify(new TDetailEntity[] { detailEntity }); }); }
public virtual void AddDetailEntities() { var availalbleEntities = DetailsRepository.ToList().Except(DetailEntities).ToArray(); var selectEntitiesViewModel = new SelectDetailEntitiesViewModel <TDetailEntity>(availalbleEntities); if (DialogService.ShowDialog(MessageButton.OKCancel, CommonResources.AddRemoveDetailEntities_SelectObjects, selectEntitiesViewModel) == MessageResult.OK && selectEntitiesViewModel.SelectedEntities.Any()) { foreach (var selectedEntity in selectEntitiesViewModel.SelectedEntities) { DetailEntities.Add(selectedEntity); } SaveChangesAndNotify(selectEntitiesViewModel.SelectedEntities); } }