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 EditDetailEntity() { if (SelectedEntities.Any()) { var detailKey = DetailsRepository.GetPrimaryKey(SelectedEntities.First()); DocumentManagerService.ShowExistingEntityDocument <TDetailEntity, TDetailPrimaryKey>(this, detailKey); } }
public override void RemoveDetailEntities() { if (!SelectedEntities.Any()) { return; } var entityKey = Repository.GetPrimaryKey(Entity); foreach (var selectedEntity in SelectedEntities) { var detailKey = DetailsRepository.GetPrimaryKey(selectedEntity); var junction = JunctionRepository.First(GetJunctionPredicate(entityKey, detailKey)); JunctionRepository.Remove(junction); } SaveChangesAndNotify(SelectedEntities); SelectedEntities.Clear(); }
public override void AddDetailEntities() { var availableEntities = DetailsRepository.ToList().Except(DetailEntities).ToArray(); var selectEntitiesViewModel = new SelectDetailEntitiesViewModel <TDetailEntity>(availableEntities); if (DialogService.ShowDialog(MessageButton.OKCancel, CommonResources.AddRemoveDetailEntities_SelectObjects, selectEntitiesViewModel) == MessageResult.OK && selectEntitiesViewModel.SelectedEntities.Any()) { foreach (var selectedEntity in selectEntitiesViewModel.SelectedEntities) { var junction = new TJunction(); var entityKey = Repository.GetPrimaryKey(Entity); var detailKey = DetailsRepository.GetPrimaryKey(selectedEntity); var junctionType = typeof(TJunction); junctionType.GetProperty(ExpressionHelper.GetPropertyName(getEntityForeignKey)).SetValue(junction, entityKey, null); junctionType.GetProperty(ExpressionHelper.GetPropertyName(getDetailForeignKey)).SetValue(junction, detailKey, null); JunctionRepository.Add(junction); } SaveChangesAndNotify(selectEntitiesViewModel.SelectedEntities); } }
protected void SaveChangesAndNotify(IEnumerable <TDetailEntity> detailEntities) { try { UnitOfWork.SaveChanges(); foreach (var detailEntity in detailEntities) { Messenger.Default.Send(new EntityMessage <TDetailEntity, TDetailPrimaryKey>(DetailsRepository.GetPrimaryKey(detailEntity), EntityMessageType.Changed)); } Reload(); } catch (DbException e) { MessageBoxService.ShowMessage(e.ErrorMessage, e.ErrorCaption, MessageButton.OK, MessageIcon.Error); } }