private void ValidateProperty(IPropertyMetadata property) { var context = new ValidationContext(Entity, _serviceProvider, null) { MemberName = property.PropertyName }; var results = new List <ValidationResult>(); try { // even though this says Try, and therefore it should not throw an error, IT DOES when a given property is not part of Entity Validator.TryValidateProperty(property.Getter(Entity), context, results); } catch { } foreach (var result in DataProviders.Where(p => p.Property == property).SelectMany(p => p.Validate(Entity, _serviceProvider))) { results.Add(result); } var state = GetPropertyState(property); state.ClearMessages(); state.WasValidated = true; foreach (var result in results) { state.AddMessage(result.ErrorMessage); } OnValidationStateChanged?.Invoke(this, new ValidationStateChangedEventArgs(isValid: !HasValidationMessages())); }
public Task SetEntityAsync(FormEditContext editContext, IParent?parent) { _editContext = editContext; _parent = parent; var data = _relatedElementsGetter?.Getter(_property.Getter(_editContext.Entity)) ?? _property.Getter(_editContext.Entity); if (data is IEnumerable <IEntity> entityCollection) { _relatedIds = entityCollection.Select(x => (object)x.Id !).ToList(); } else if (data is IEnumerable <object> objectCollection) { _relatedIds = objectCollection.ToList(); } else if (data is IEnumerable enumerable && data is not string) { var list = new List <object>(); foreach (var element in enumerable) { if (element != null) { list.Add(element); } } _relatedIds = list; } return(Task.CompletedTask); }
public async Task <IReadOnlyList <IElement> > GetAvailableElementsAsync(IView view) { if (_editContext == null) { return(new List <IElement>()); } var parent = default(IParent?); if (_entityAsParent && _editContext.EntityState != EntityState.IsNew) { parent = new ParentEntity(_editContext.Parent, _editContext.Entity, _editContext.RepositoryAlias); } if (_repositoryParentSelector != null && _parent != null) { parent = _repositoryParentSelector.Getter.Invoke(_parent) as IParent; } view.CollectionAlias = _editContext.CollectionAlias; var entities = await _concurrencyService.EnsureCorrectConcurrencyAsync(() => _repository.GetAllAsync(new ViewContext(_editContext.CollectionAlias, parent), view)); return(entities .Select(entity => (IElement) new Element { Id = _idProperty.Getter(entity), Labels = _displayProperties.Select(x => x.StringGetter(entity)).ToList() }) .ToList()); }
private async Task SetElementsAsync() { if (_entity == null) { return; } _eventHandle?.Dispose(); _eventHandle = _repository.ChangeToken.RegisterChangeCallback(async x => { await SetElementsAsync(); OnDataChange?.Invoke(this, new EventArgs()); }, null); var parentId = _repositoryParentIdProperty?.Getter.Invoke(_entity) as string; var entities = await _memoryCache.GetOrCreateAsync(new { _repository, parentId }, (entry) => { entry.AddExpirationToken(_repository.ChangeToken); return(_repository.InternalGetAllAsync(parentId, Query.Default())); }); _elements = entities .Select(entity => (IElement) new ElementDTO { Id = _idProperty.Getter(entity), Labels = _labelProperties.Select(x => x.StringGetter(entity)) }) .ToList(); }
public async Task SetEntityAsync(IEntity entity, IParent?parent) { _entity = entity; _parent = parent; await SetElementsAsync(); var data = _relatedElementsGetter?.Getter(_property.Getter(entity)) ?? _property.Getter(entity); if (data is ICollection <IEntity> entityCollection) { _relatedIds = entityCollection.Select(x => (object)x.Id).ToList(); } else if (data is ICollection <object> objectCollection) { _relatedIds = objectCollection; } else if (data is IEnumerable enumerable) { var list = new List <object>(); foreach (var element in enumerable) { if (element != null) { list.Add(element); } } _relatedIds = list; } else { return; // throw new InvalidOperationException("Failed to convert relation property to ICollection<object>"); } UpdateRelatedElements(); }
private async Task SetElementsAsync() { if (_entity == null) { return; } _eventHandle?.Dispose(); _eventHandle = _repository.ChangeToken.RegisterChangeCallback(async x => { await SetElementsAsync(); OnDataChange?.Invoke(this, new EventArgs()); }, null); var parent = default(IParent?); if (_repositoryParentSelector != null) { // a bit weird (see CollectionRelationConfig) if (_repositoryParentSelector.ObjectType == typeof(ValueTuple <IParent?, IEntity>)) { parent = _repositoryParentSelector.Getter.Invoke((_parent, _entity)) as IParent; } else if (_parent != null) { parent = _repositoryParentSelector.Getter.Invoke(_parent) as IParent; } } var entities = await _memoryCache.GetOrCreateAsync(new { _repository, parent }, (entry) => { entry.AddExpirationToken(_repository.ChangeToken); return(_repository.InternalGetAllAsync(parent, Query.Default())); }); _elements = entities .Select(entity => (IElement) new ElementDTO { Id = _idProperty.Getter(entity), Labels = _labelProperties.Select(x => x.StringGetter(entity)) }) .ToList(); }
private async Task SetElementsAsync() { if (_editContext == null) { return; } _eventHandle?.Dispose(); _eventHandle = _repository.ChangeToken.RegisterChangeCallback(async x => { await SetElementsAsync(); OnDataChange?.Invoke(this, new EventArgs()); }, null); var parent = default(IParent?); if (_entityAsParent && _editContext.EntityState != EntityState.IsNew) { parent = new ParentEntity(_editContext.Parent, _editContext.Entity, _editContext.RepositoryAlias); } if (_repositoryParentSelector != null && _parent != null) { parent = _repositoryParentSelector.Getter.Invoke(_parent) as IParent; } var entities = await _memoryCache.GetOrCreateAsync(new { _repository, parent }, async (entry) => { entry.AddExpirationToken(_repository.ChangeToken); return(await _repository.GetAllAsync(parent, Query.Default(_editContext.CollectionAlias))); }); _elements = entities .Select(entity => (IElement) new Element { Id = _idProperty.Getter(entity), Labels = _labelProperties.Select(x => x.StringGetter(entity)).ToList() }) .ToList(); }
public async Task SetEntityAsync(FormEditContext editContext, IParent?parent) { _editContext = editContext; _parent = parent; await SetElementsAsync(); var data = _setup.RelatedElementsGetter?.Getter(_property.Getter(_editContext.Entity)) ?? _property.Getter(_editContext.Entity); if (data is ICollection <IEntity> entityCollection) { _relatedIds = entityCollection.Select(x => (object)x.Id !).ToList(); } else if (data is ICollection <object> objectCollection) { _relatedIds = objectCollection; } else if (data is IEnumerable enumerable) { var list = new List <object>(); foreach (var element in enumerable) { if (element != null) { list.Add(element); } } _relatedIds = list; } else { return; } UpdateRelatedElements(); }