Have the server perform post processing on a string before it is returned via a WCF RIA query?

120 Views Asked by At

I've read that you can put business rules on the server when dealing with WCF RIA services, but I'm at a loss for how to accomplish what I what. Basically what I have is, there's a string stored in the database that's encrypted. I have a view that returns that column, and I'd like it to be returned to the client unencrypted. I have a standard DomainService Get method

    public IQueryable<qry_ClientList> GetQry_ClientList(Guid Key)
    {
        return this.ObjectContext.qry_ClientList.OrderBy(p => p.ClientCode);
    }

Can someone point me in the right direction how I'd call the decrypt function on that field before it's returned? I have a reason I want to do this in code and not on the server, but I don't go into that here.

2

There are 2 best solutions below

0
On

You can do postprocessing by overriding DomainService.Query() method as follows:

public override System.Collections.IEnumerable Query(QueryDescription queryDescription, out IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> validationErrors, out int totalCount)
{
    IEnumerable result = base.Query(queryDescription, out validationErrors, out totalCount);

    // now you have collection with all client query operators applied and
    // you can apply post-processing
    if (queryDescription.Method.Name == "GetQry_ClientList")
    {
        result.OfType<qry_ClientList>().ToList().ForEach(c => Descrypt(c));
    }

    return result;
}
0
On

Materialize your entities to a list first, run your function, then convert your list back to an IQueryable before returning:

public IQueryable<qry_ClientList> GetQry_ClientList(Guid Key)
{
    List<qry_ClientList> clients = 
        this.ObjectContext.qry_ClientList.OrderBy(p => p.ClientCode).ToList();
    foreach (qry_ClientList c in clients) {
        Decrypt(c);
    }
    return clients.AsQueryable;
}