forked from OData/RESTier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example on how to customize submit processor
- Loading branch information
Vincent He
committed
May 10, 2016
1 parent
970bfb1
commit 8181c40
Showing
3 changed files
with
75 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...dTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Submit/CustomizedSubmitProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |