How can I implement Query Interception in a LINQ to Entities query? (c#)

北城以北 提交于 2019-12-24 04:53:54

问题


I'm trying to implement encrypted columns in EF4 and using the CTP5 features to allow simple use of POCO's to query the database. Sorry that this is a lot of words, but I hope the below gives enough to explain the need and the problem!

So, bit of background, and my progress so far:

The intention is that if you query the tables without using our DAL then the data is rubbish, but I don't want the developers to worry about if/when/how the data is encrypted.

For simplicity, at this stage I'm working on the assumption any string column will be encrypted.

Now, I have successfully implemented this for returning the data using the Objectmaterialized event, and for data commits using the SavingChanges event.

So given the following class:

public class Thing
{

    public int ID { get; set; }
    [Required]
    public string Name { get; set; }
    public DateTime Date { get; set; }
    public string OtherString { get; set; }
}

The below query returns all the required values and the POCO materialized has clear data in it.

var things = from t in myDbContext.Things
             select t;

where myDbContext.Things is a DbSet<Thing>

Likewise, passing an instance of Thing to Things.Add()
(with clear string data in the Name and/or OtherString values)
and then calling myDbContext.SaveChanges() encrypts the strings before it gets to the data store.

Now, the problem I have is in this query:

var things = from t in myDbContext.Things
             where t.Name == "Hairbrush"
             select t;

This results in the unencrypted value being compared to the encrypted value in the DB. Obviously I don't want to get all the records from the database, materialize them, and then filter the results based on any supplied Where clause... so what I need to do is: intercept that query and rewrite it by encrypting any strings in the Where clause. So I've looked at:

  • writing a query provider, but that doesn't seem like the right solution... (is it?)
  • writing my own IQueryable wrapper for the DbSet which will capture the expression, run over it using an expression tree visitor and then forward the new expression to the DbSet...

Attempts at both have left me somewhat lost! I prefer the second solution i think since it feels a bit neater, and is probably clearer to other developers in future. But I'm happy to go with either or another better option!!

The main thing I am struggling with is when/how the LINQ expression is applied to the object... I think i've got myself a bit confused as to where the expression executes in the IQueryable object thus I'm not sure which method I need to implement in my wrapper to then grab and manipulate the expression being passed in...

I'm sure I'm missing something fairly obvious here and I'm waiting for that light bulb moment... but its not coming!!

Any help will be very gratefully received!


回答1:


Thought I'd let you know what my final solution was. In the end I have gone a wrapper class which implements a Where method, but without going to the extent of implementing IQueryable entirely. LINQ will still execute against the class (at least to the extent that I want/need it to) and will call the Where method with the expression from the LINQ.

I then traverse this ExpressionTree and replace my strings with encrypted values before forwarding the new expressiontree to the internal DbSet. and then returning the result.

Its pretty crude, and has its limitation, but works for our particular circumstance without problem.

Thanks, Ben




回答2:


you should use the QueryInterceptor attribute, search here in SO or in google and you find examples on how to use it.

a snippet:

[QueryInterceptor("Orders")]
public Expression<Func<Order, bool>> FilterOrders() 
{
    return o => o.Customer.Name == /* Current principal name. */;
} 

// Insures that the user accessing the customer(s) has the appropriate
// rights as defined in the QueryRules object to access the customer
// resource(s).

[QueryInterceptor ("Customers")]
public Expression<Func<Customer, bool>> FilterCustomers() 
{
  return c => c.Name == /* Current principal name. */ &&
              this.CurrentDataSource.QueryRules.Contains(
                rule => rule.Name == c.Name &&
                        rule.CustomerAllowedToQuery == true
              );
}



回答3:


You can use David Fowler's Query Interceptor:

https://github.com/davidfowl/QueryInterceptor

One example of its use:

IQueryable q = ...; IQueryable modifed = q.InterceptWith(new MyInterceptor());

And on class MyInterceptor:

protected override Expression VisitBinary(BinaryExpression node) { if (node.NodeType == ExpressionType.Equal) { // Change == to != return Expression.NotEqual(node.Left, node.Right); } return base.VisitBinary(node); }



来源:https://stackoverflow.com/questions/5249473/how-can-i-implement-query-interception-in-a-linq-to-entities-query-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!