From 8181c40efae1b0ded66f420aaaf76bf1f11dc6dd Mon Sep 17 00:00:00 2001 From: Vincent He Date: Tue, 10 May 2016 13:01:33 +0800 Subject: [PATCH] Add example on how to customize submit processor --- .../Api/TrippinApi.cs | 8 ++- ...estier.WebApi.Test.Services.Trippin.csproj | 1 + .../Submit/CustomizedSubmitProcessor.cs | 69 +++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Submit/CustomizedSubmitProcessor.cs diff --git a/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Api/TrippinApi.cs b/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Api/TrippinApi.cs index 4afcefd6..63940db2 100644 --- a/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Api/TrippinApi.cs +++ b/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Api/TrippinApi.cs @@ -9,9 +9,11 @@ using Microsoft.OData.Core; using Microsoft.Restier.Core; using Microsoft.Restier.Core.Model; +using Microsoft.Restier.Core.Submit; using Microsoft.Restier.EntityFramework; using Microsoft.Restier.WebApi.Model; using Microsoft.Restier.WebApi.Test.Services.Trippin.Models; +using Microsoft.Restier.WebApi.Test.Services.Trippin.Submit; namespace Microsoft.Restier.WebApi.Test.Services.Trippin.Api { @@ -142,8 +144,7 @@ protected bool CanDeleteTrips() protected override IServiceCollection ConfigureApi(IServiceCollection services) { - - // Add OData Query Settings and valiadtion settings + // Add customized OData valiadtion settings Func validationSettingFactory = (sp) => new ODataValidationSettings { MaxAnyAllExpressionDepth =3, @@ -152,7 +153,8 @@ protected override IServiceCollection ConfigureApi(IServiceCollection services) return base.ConfigureApi(services) .AddSingleton() - .AddSingleton(validationSettingFactory); + .AddSingleton(validationSettingFactory) + .AddService(); } } } \ No newline at end of file diff --git a/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Microsoft.Restier.WebApi.Test.Services.Trippin.csproj b/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Microsoft.Restier.WebApi.Test.Services.Trippin.csproj index 321b6caf..f0d2d042 100644 --- a/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Microsoft.Restier.WebApi.Test.Services.Trippin.csproj +++ b/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Microsoft.Restier.WebApi.Test.Services.Trippin.csproj @@ -64,6 +64,7 @@ + diff --git a/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Submit/CustomizedSubmitProcessor.cs b/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Submit/CustomizedSubmitProcessor.cs new file mode 100644 index 00000000..7ab0e9dd --- /dev/null +++ b/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Submit/CustomizedSubmitProcessor.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using System.Web; +using Microsoft.Restier.Core.Submit; + +namespace Microsoft.Restier.WebApi.Test.Services.Trippin.Submit +{ + public class CustomizedSubmitProcessor : IChangeSetItemProcessor + { + private IChangeSetItemProcessor Inner { get; set; } + + public Task OnProcessingChangeSetItemAsync(SubmitContext context, ChangeSetItem item, CancellationToken cancellationToken) + { + return Inner.OnProcessingChangeSetItemAsync(context, item, cancellationToken); + } + + public Task OnProcessedChangeSetItemAsync(SubmitContext context, ChangeSetItem item, CancellationToken cancellationToken) + { + var dataModificationItem = item as DataModificationItem; + if (dataModificationItem != null) + { + object myEntity = dataModificationItem.Entity; + string entitySetName = dataModificationItem.EntitySetName; + ChangeSetItemAction operation = dataModificationItem.ChangeSetItemAction; + + // In case of insert, the request URL has no key, and request body may not have key neither as the key may be generated by database + var keyAttrbiutes = new Dictionary(); + var keyConvention = new Dictionary(); + + var entityTypeName = myEntity.GetType().Name; + PropertyInfo[] properties = myEntity.GetType().GetProperties(); + + foreach (PropertyInfo property in properties) + { + var attribute = Attribute.GetCustomAttribute(property, typeof(KeyAttribute)) + as KeyAttribute; + var propName = property.Name; + // This is getting key with Key attribute defined + if (attribute != null) // This property has a KeyAttribute + { + // Do something, to read from the property: + object val = property.GetValue(myEntity); + keyAttrbiutes.Add(propName, val); + } + // This is getting key based on convention + else if(propName.ToLower().Equals("id") || propName.ToLower().Equals(entityTypeName.ToLower()+"id")) + { + object val = property.GetValue(myEntity); + keyConvention.Add(propName, val); + } + } + if (keyAttrbiutes.Count > 0) + { + // Use property with key attribute as keys + } + else if(keyConvention.Count > 0) + { + // Key is defined based on convention + } + } + return Inner.OnProcessedChangeSetItemAsync(context, item, cancellationToken); + } + } +} \ No newline at end of file