Matt Frear
Matt Frear

Reputation: 54871

oData WCF service - hide an element

I'm new to WCF. My web project has an ADO.NET Entity Data Model (aka EF edmx), which has the Entity Container Name JobSystemEntities.

I've created a simple oData WCF data service which uses JobSystemEntities, and it works great:

public class JobService : DataService<JobSystemEntities>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Jobs", EntitySetRights.ReadSingle);
    }

However, this exposes all of the properties on the Job. I would like to hide sensitive data, i.e. the Cost field/property/column of the Job table.

Upvotes: 2

Views: 3018

Answers (2)

George
George

Reputation: 430

I've done something similar to this. A good starting point is found here:

http://weblogs.asp.net/rajbk/archive/2010/05/15/pre-filtering-and-shaping-odata-feeds-using-wcf-data-services-and-the-entity-framework-part-1.aspx

Basically you will need to separate the protected properties of an entity into a separate entity that is linked as a property of the other. Once that is done user a Query Interceptor to restrict when that protected entity can be viewed.

[QueryInterceptor("YourObjectsProtectedProperties")]
public Expression<Func<YourObjectsProtectedProperties, bool>> OnReadYourObjectsProtectedProperties()
{
if (ShowEntityToUser())
   return o => true == true;
return o => true == false;
}

Upvotes: 0

Magnus Kragelund
Magnus Kragelund

Reputation: 370

I am posting this a but late, but it might help others.

You can use the IgnoreProperties attribute http://msdn.microsoft.com/en-us/library/system.data.services.ignorepropertiesattribute.aspx on your class.

You will have to define a partial Job class in order to do this. Something in the lines of:

namespace DAL.Entities
{
    [IgnoreProperties("Cost")]
    public partial class Job
    {

    }
}

Upvotes: 2

Related Questions