0

I have a ServiceOperation to query items available to a certain user at a certain time. In short this methode does:

var fullResult = from i in Items where ... select i; //get ALL possible items where..., 

Lets say this returns items {A, B, C, D}. A second query filters out which of those items the calling user has access to.

var clientResult = from ci in fullResult where (privilege's and schedule's are true)

This mite result in {A, C } and is returned. The result on the client side is: only the list of items the client has access to is displayed. This can be annoying since you don't know if you made a mistake in searching, or the item is just not available right now.

What I would like to be able to do is show all possible results to the client {A, B, C, D} yet FLAG B and D in this case as unavailable.

My entity has already a property isReadOnly I could use.

Can I write a query to not just filter out, but also flag any remaining results as read only? An ideal result would be {A, B.isREadOnly=true, C, D.isReadOnly=true}

Or did I reach the limit of what is doable and do I have to write a traditional WCF web method, creating a separate class, returning a list of results?

PS: this 'isReadOnly' property is only used for this, I don't mind it being changed in the DB at all

Thanx for any pointers

Andreas

Andreas
  • 681
  • 1
  • 8
  • 16

1 Answers1

1

If I were you I would consider not returning the entity directly out of your service and instead map it to something that has the ReadOnly property. For example, if your entity is:

public class A
{
    public string Name { get; set; }
}

Then you could have a data contract like this:

[DataContract]
public class AExtra
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public bool IsReadOnly { get; set; }
}

what this means is that you could do this in your query:

var res = from a 
          in Items 
          where (...whatever your original logic is) 
          select new AExtra
            {
                Name = a.Name,
                IsReadOnly = (...put your logic for determining ReadOnly in here)
            };

And then return res from your service operation.

Just an opinion really but I like to do things like this rather than send the entities directly out of the service - it always gives me a bit more freedom to change things without having too many knock-on effects.

kmp
  • 10,535
  • 11
  • 75
  • 125
  • ok, in this case I have a followup Q. I would need to create an Extended Entity in addition to my DB mapped entity in my Model (ie. Entity A (DB) , AExtra (MyOwnEntity). I understand the forward way of doing this, mapping the query into a new type. But how do I handle save changes? I guess I have to do some magic in the Change-interceptor. But what exactly would I have to do to save changes back to ? – Andreas Dec 16 '11 at 16:09
  • yes, you lose change tracking when you do this so when you need to update A you would need to grab it out of the context again, set it's properties from AExtra and save it. If the context is still around it might give you back a cached version so getting the entity back and setting the properties would not be so inefficient - try it and watch sql profiler or something to see the queries. I've always just used regular wcf services with entity framework backend so I can't say whether there is a way in data services that you can do this differently, sorry – kmp Dec 16 '11 at 19:21
  • I'm gonna give it a try, it seems to be the only option. Since I am closing this operation with another ServiceOperation call, loosing the change tracking mite not matter at all in this case. Thanx. – Andreas Dec 17 '11 at 00:21