0

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.

cost
  • 4,420
  • 8
  • 48
  • 80
  • Do you really need to postprocess entire collection? From the method signature (unused parameter `Guid key`) it seems it should return only one `qry_ClientList` entity instead of `IQueryable` – Jozef Benikovský Aug 07 '13 at 20:55
  • @JozefBenikovský The unused parameter is a authentication key. This system ties into a very old custom authentication system, I took out the code involving that for simplicity, but forgot to remove the parameter. – cost Aug 11 '13 at 21:35

2 Answers2

0

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;
}
sparks
  • 1,264
  • 1
  • 15
  • 29
0

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;
}
Jozef Benikovský
  • 1,121
  • 10
  • 9