public static bool TryGetInlineODataEntities( this ODataEntityDto oDataEntity, string navigationPropertyName, out IEnumerable <ODataEntityDto> includedODataEntities) { var navigationProperty = oDataEntity.NavigationProperties.FirstOrDefault(p => p.Name == navigationPropertyName); if (navigationProperty != null) { var collectionValue = navigationProperty.Value as IEnumerable <ODataEntityDto>; if (collectionValue != null) { includedODataEntities = collectionValue; return(true); } var value = navigationProperty.Value as ODataEntityDto; if (value != null) { includedODataEntities = value.Enumerate(); return(true); } } includedODataEntities = null; return(false); }
private static bool TryGetEntityReferenceLinks( ODataEntityDto oDataEntity, string navigationPropertyName, out IEnumerable <ODataPath> referenceLinks) { var navigationProperty = oDataEntity.NavigationProperties.FirstOrDefault(p => p.Name == navigationPropertyName); if (navigationProperty != null) { var collectionValue = navigationProperty.Value as IEnumerable <ODataPath>; if (collectionValue != null) { referenceLinks = collectionValue; return(true); } var value = navigationProperty.Value as ODataPath; if (value != null) { referenceLinks = value.Enumerate(); return(true); } } referenceLinks = null; return(false); }
private bool TryHandlePostInContainedNavigationProperty( IOperationContext context, ODataEntityDto oDataEntity, out IEntity createdEntity) { var navigationProperty = context.NavigationTarget as INavigationProperty; if (navigationProperty == null || !navigationProperty.IsContained()) { createdEntity = null; return(false); } var bindableModelContext = context.ModelContext.As <IBindableModelContext>(); var parentPath = context.Path.Take(context.Path.Count - 1); var parentEntity = this.entityReader.ReadEntitiesFromPath(bindableModelContext, parentPath).SingleOrDefault(); if (parentEntity == null) { throw new KeyNotFoundException("The preceeding entity could not be found."); } createdEntity = this.entityCreator.CreateInContainedNavigationProperty( bindableModelContext, navigationProperty, context.NavigationRoot, parentEntity, oDataEntity); return(true); }
protected void SetPropertyValues( IBindableModelContext context, IEntitySet navigationRoot, IEntity targetEntity, ODataEntityDto oDataEntity) { this.SetPropertyValues(context, navigationRoot, targetEntity, oDataEntity, Enumerable.Empty <string>()); }
public IEnumerable <ODataEntityDto> Handle(IOperationContext context, ODataEntityDto incomingODataEntity) { var entities = this.entityReader.ReadEntitiesFromPath(context.ModelContext.As <IBindableModelContext>(), context.Path); return(this.SerializeEntities( context.ModelContext.As <IBindableModelContext>(), entities, context.GetTargetEntityType(), context.QueryOptions.SelectExpandClause)); }
private static bool TryGetPropertyDependency(IStructuralProperty property, ODataEntityDto oDataEntity, out IDependency dependency) { object propertyValue; if (oDataEntity.TryGetPropertyValue(property, out propertyValue)) { dependency = new PropertyDependency(property, propertyValue); return(true); } dependency = null; return(false); }
public IEntity CreateInContainedNavigationProperty( IBindableModelContext context, INavigationProperty navigationProperty, IEntitySet navigationRoot, IEntity parentEntity, ODataEntityDto oDataEntity) { var dependencies = this.dependencyResolver.ResolveDependencies(context, navigationProperty, navigationRoot, oDataEntity); var createdEntity = navigationProperty.CreateEntityInContainedNavigationProperty(context, parentEntity, dependencies); this.SetPropertyValues(context, navigationRoot, createdEntity, oDataEntity, dependencies.Keys); return(createdEntity); }
public static bool TryGetPropertyValue(this ODataEntityDto oDataEntity, IStructuralProperty property, out object value) { var targetProperty = oDataEntity.Entry.Properties.FirstOrDefault(p => p.Name == property.EdmPropertyName); if (targetProperty != null) { var odataPropertyValue = targetProperty.Value; value = property.Deserialize(odataPropertyValue); return(true); } value = null; return(false); }
public IEnumerable <ODataEntityDto> Handle(IOperationContext context, ODataEntityDto incomingODataEntity) { var modelContext = context.ModelContext.As <IBindableModelContext>(); var entityToPatch = this.entityReader.ReadEntitiesFromPath(modelContext, context.Path).SingleOrDefault(); if (entityToPatch == null) { throw new KeyNotFoundException("The specified entity could not be found."); } this.entityUpdater.UpdateEntity(modelContext, context.NavigationRoot, entityToPatch, incomingODataEntity); return(context.CreatePatchOrPostResponse(entityToPatch, this.dtoBuilderFactory).Enumerate()); }
private bool TryHandlePostInEntitySet(IOperationContext context, ODataEntityDto oDataEntity, out IEntity createdEntity) { var entitySet = context.NavigationTarget as IEntitySet; if (entitySet == null) { createdEntity = null; return(false); } var bindableModelContext = context.ModelContext.As <IBindableModelContext>(); createdEntity = this.entityCreator.CreateInEntitySet(bindableModelContext, context.NavigationTarget.As <IEntitySet>(), oDataEntity); return(true); }
public static IEntityType GetEntityType(this ODataEntityDto oDataEntity, IModelContext context) { var edmEntityType = context.EdmModel.FindDeclaredType(oDataEntity.Entry.TypeName); if (edmEntityType != null) { IEntityType entityType; if (context.TryGetEntityType(edmEntityType.Name, out entityType)) { return(entityType); } } throw new KeyNotFoundException("The entity type " + oDataEntity.Entry.TypeName + " could not be found."); }
public IEnumerable <ODataEntityDto> Handle(IOperationContext context, ODataEntityDto incomingODataEntity) { IEntity createdEntity; if (this.TryHandlePostInContainedNavigationProperty(context, incomingODataEntity, out createdEntity)) { return(context.CreatePatchOrPostResponse(createdEntity, this.dtoBuilderFactory).Enumerate()); } if (this.TryHandlePostInEntitySet(context, incomingODataEntity, out createdEntity)) { return(context.CreatePatchOrPostResponse(createdEntity, this.dtoBuilderFactory).Enumerate()); } throw new InvalidOperationException("The POST operation cannot be performed against the specified URL."); }
protected override bool TryHandleStructuralProperty( IEntity targetEntity, ODataEntityDto oDataEntity, IStructuralProperty structuralProperty) { var structuralEntityProperty = structuralProperty as IStructuralEntityProperty; if (structuralEntityProperty != null && structuralEntityProperty.IsReadOnly(targetEntity)) { // According to OData v4 Part 1 (Protocol), section 11.4.3 and 11.4.4, properties which are non-updatable should be ignored // during Update and Upsert. Hence, we return true here, claiming the property was handled correctly. return(true); } return(base.TryHandleStructuralProperty(targetEntity, oDataEntity, structuralProperty)); }
private bool TryParseReferencedEntities( IBindableModelContext context, ODataEntityDto oDataEntity, INavigationProperty navigationProperty, List <IEntity> resultingEntities) { IEnumerable <IEntity> referencedEntities; if (this.referenceParser.TryParseReferencedEntities(context, navigationProperty, oDataEntity, out referencedEntities)) { resultingEntities.AddRange(referencedEntities); return(true); } return(false); }
public bool TrySetOnEntity(IEntity targetEntity, ODataEntityDto sourceODataEntity, IStructuralEntityProperty property) { object value; if (!sourceODataEntity.TryGetPropertyValue(property, out value)) { return(false); } if (property.IsReadOnly(targetEntity)) { return(false); } return(property.TrySetValueOnEntity(targetEntity, value)); }
private IEnumerable <ODataEntityDto> Execute( Operation operation, ODataPath path, QueryOptions queryOptions, ODataEntityDto incomingObject) { var operationContext = this.operationContextFactory.Create(this.modelContext, operation, path, queryOptions); IOperationHandler relevantHandler; if (!this.modelContext.TryGetHandler(operationContext, out relevantHandler)) { throw new InvalidOperationException($"No handler found that could handle the {operation} operation on {path.Last()}."); } // We need to make this call synchronous in the current thread, because otherwise we might not have a unit of work var operationResult = relevantHandler.Handle(operationContext, incomingObject).ToArray(); return(operationResult); }
protected virtual bool TryHandleStructuralProperty( IEntity targetEntity, ODataEntityDto oDataEntity, IStructuralProperty structuralProperty) { var structuralEntityProperty = structuralProperty as IStructuralEntityProperty; if (structuralEntityProperty == null) { return(false); } if (!this.structuralPropertyBinder.TrySetOnEntity(targetEntity, oDataEntity, structuralEntityProperty)) { return(false); } return(true); }
public bool TryGetLinkedEntities( IBindableModelContext context, ODataEntityDto oDataEntity, INavigationProperty navigationProperty, out IEnumerable <IEntity> entities) { if (navigationProperty.IsContained()) { throw new ArgumentException("Navigation property must be uncontained.", nameof(navigationProperty)); } var resultingEntities = new List <IEntity>(); if (this.TryParseReferencedEntities(context, oDataEntity, navigationProperty, resultingEntities)) { entities = resultingEntities; return(true); } entities = null; return(false); }
protected override bool TryHandleNavigationProperty( IBindableModelContext context, IEntity targetEntity, ODataEntityDto oDataEntity, INavigationProperty navigationProperty, IEntitySet navigationRoot) { if (navigationProperty.IsContained()) { throw new InvalidOperationException("Only uncontained navigation properties can be processed while updating the parent."); } IEnumerable <IEntity> entities; if (this.navigationPropertyParser.TryGetLinkedEntities(context, oDataEntity, navigationProperty, out entities)) { this.navigationPropertyBinder.SetUncontainedNavigationProperty(context, targetEntity, navigationProperty, entities); return(true); } return(false); }
public IDictionary <string, IDependency> ResolveDependencies( IBindableModelContext context, INavigatable navigationTarget, IEntitySet navigationRoot, ODataEntityDto oDataEntity) { //var changedPropertyNames = oDataEntity.GetChangedPropertyNames(); var changedPropertyNames = oDataEntity.Entry.Properties.Select(p => p.Name).Concat(oDataEntity.NavigationProperties.Select(np => np.Name)).ToArray(); // TODO var dependencies = new Dictionary <string, IDependency>(); foreach (var declaration in context.GetDependencies(navigationTarget)) { IDependency dependency; var property = declaration.DependableElement as IStructuralProperty; if (property != null && changedPropertyNames.Contains(property.EdmPropertyName) && TryGetPropertyDependency(property, oDataEntity, out dependency)) { dependencies[property.EdmPropertyName] = dependency; continue; } var navigationProperty = declaration.DependableElement as INavigationProperty; if (navigationProperty != null && changedPropertyNames.Contains(navigationProperty.Name) && this.TryGetUncontainedNavigationPropertyDependency(context, navigationProperty, navigationRoot, oDataEntity, out dependency)) { dependencies[navigationProperty.Name] = dependency; continue; } if (!declaration.IsOptional) { throw new NotSupportedException("The dependency is unknown and could not be resolved."); } } return(dependencies); }
private bool TryParseAndCreateInlineEntities( IBindableModelContext context, ODataEntityDto oDataEntity, INavigationProperty navigationProperty, IEntitySet navigationRoot, List <IEntity> resultingEntities) { IEnumerable <ODataEntityDto> inlineODataEntities; if (oDataEntity.TryGetInlineODataEntities(navigationProperty.Name, out inlineODataEntities)) { if (this.uncontainedEntitiesFactory == null) { throw new InvalidOperationException("Cannot create uncontained entities. Factory hasn't been set."); } var createdEntities = this.uncontainedEntitiesFactory(context, navigationProperty, navigationRoot, inlineODataEntities); resultingEntities.AddRange(createdEntities); return(true); } return(false); }
private bool TryGetUncontainedNavigationPropertyDependency( IBindableModelContext context, INavigationProperty navigationProperty, IEntitySet navigationRoot, ODataEntityDto oDataEntity, out IDependency dependency) { if (navigationProperty.IsContained()) { throw new InvalidOperationException("A contained navigation property cannot be included as a dependency."); } IEnumerable <IEntity> entities; if (this.navigationPropertyParser.TryGetLinkedOrInlineEntities(context, oDataEntity, navigationProperty, navigationRoot, out entities)) { dependency = new NavigationPropertyDependency(navigationProperty, entities); return(true); } dependency = null; return(false); }
protected void SetPropertyValues( IBindableModelContext context, IEntitySet navigationRoot, IEntity targetEntity, ODataEntityDto oDataEntity, IEnumerable <string> propertiesToExclude) { var entityType = oDataEntity.GetEntityType(context); var relevantProperties = oDataEntity.Entry.Properties.Where(p => !propertiesToExclude.Contains(p.Name)); foreach (var property in relevantProperties) { IStructuralProperty structuralProperty; if (entityType.TryGetStructuralProperty(property.Name, out structuralProperty)) { if (!this.TryHandleStructuralProperty(targetEntity, oDataEntity, structuralProperty)) { throw new InvalidOperationException("The included structural property \"" + property.Name + "\" could not be processed."); } continue; } INavigationProperty navigationProperty; if (entityType.TryGetNavigationProperty(property.Name, out navigationProperty)) { if (!this.TryHandleNavigationProperty(context, targetEntity, oDataEntity, navigationProperty, navigationRoot)) { throw new InvalidOperationException("The included navigation property \"" + property.Name + "\" could not be processed."); } continue; } throw new NotSupportedException("The included property \"" + property.Name + "\" is unsupported."); } }
public bool TryParseReferencedEntities( IBindableModelContext context, INavigationProperty navigationProperty, ODataEntityDto oDataEntity, out IEnumerable <IEntity> entities) { IEnumerable <ODataPath> referenceLinks; if (!TryGetEntityReferenceLinks(oDataEntity, navigationProperty.Name, out referenceLinks)) { entities = null; return(false); } var readEntities = new List <IEntity>(); foreach (var referenceLink in referenceLinks) { readEntities.AddRange(this.entityReader.ReadEntitiesFromPath(context, referenceLink)); } entities = readEntities; return(true); }
public void UpdateEntity(IBindableModelContext context, IEntitySet navigationRoot, IEntity targetEntity, ODataEntityDto oDataEntity) { this.SetPropertyValues(context, navigationRoot, targetEntity, oDataEntity); }
protected abstract bool TryHandleNavigationProperty( IBindableModelContext context, IEntity targetEntity, ODataEntityDto oDataEntity, INavigationProperty navigationProperty, IEntitySet navigationRoot);
public Task <ODataEntityDto> Patch(ODataPath path, QueryOptions queryOptions, ODataEntityDto incomingObject) { var result = this.Execute(Operation.Patch, path, queryOptions, incomingObject); return(Task.FromResult(result.FirstOrDefault())); }
public IEntity CreateInEntitySet(IBindableModelContext context, IEntitySet entitySet, ODataEntityDto oDataEntity) { var dependencies = this.dependencyResolver.ResolveDependencies(context, entitySet, entitySet, oDataEntity); var createdEntity = entitySet.CreateEntityInEntitySet(context, dependencies); this.SetPropertyValues(context, entitySet, createdEntity, oDataEntity, dependencies.Keys); return(createdEntity); }