Is it possible to manipulate SOME entities in a ServiceOperation returning an IQueryable
-
17-03-2021 - |
문제
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
해결책
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.