Skip to content

Commit

Permalink
Add example on how to customize submit processor
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent He committed May 10, 2016
1 parent 970bfb1 commit 8181c40
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<IServiceProvider, ODataValidationSettings> validationSettingFactory = (sp) => new ODataValidationSettings
{
MaxAnyAllExpressionDepth =3,
Expand All @@ -152,7 +153,8 @@ protected override IServiceCollection ConfigureApi(IServiceCollection services)

return base.ConfigureApi(services)
.AddSingleton<ODataPayloadValueConverter, CustomizedPayloadValueConverter>()
.AddSingleton<ODataValidationSettings>(validationSettingFactory);
.AddSingleton<ODataValidationSettings>(validationSettingFactory)
.AddService<IChangeSetItemProcessor, CustomizedSubmitProcessor>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="Models\Trip.cs" />
<Compile Include="Models\TrippinModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Submit\CustomizedSubmitProcessor.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, object>();
var keyConvention = new Dictionary<string, object>();

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);
}
}
}

0 comments on commit 8181c40

Please sign in to comment.