Jesper Lund Stocholm
Jesper Lund Stocholm

Reputation: 2013

Preventing modification of specific properties on entity

I am exposing an entity from my database thru an OData feed based on WCF DataServices in .Net 4.0. Up until now everything has been completely open, but I am now in the process of limiting the operations possible on the entities.

I have an Order object with these properties (amongst others):

ID    
Name    
Amount    
CustomerID

I would like to be able to expose all values to the consumer of the service and allow them to update them. However, I don't want them to be able to update the CustomerID property of the entity.

How can I accomplish this? I have looked into QueryInterceptors, but I have not yet found the right way to either block the update call or modify the request.

Upvotes: 1

Views: 143

Answers (1)

Geoff
Geoff

Reputation: 9340

You can do this with a ChangeInterceptor

[ChangeInterceptor("Orders")]
public void OnChangeOrders(Order order, UpdateOperations operations)
{
    if (operations == UpdateOperations.Change)
    {
        //Get the record as it exists before the change is made
        var oldValue = CurrentDataSource.ChangeTracker.Entries<Order>().First();

        //You can compare the various properties here to determine what, if anything,
        //has changed, or just write over the property if you want

        order.CustomerID = oldValue.Entity.CustomerID;

    }
}

Upvotes: 1

Related Questions