0

I'm currently working in a project that involves the encryption of a few columns in an existing database. There is quite a lot of code already written against the current schema, a lot of which is in the form of custom linq-to-sql queries. The number of queries is in the neighbourhood of a 5 figure number, so modifying and re-testing each and everyone of them would be way too expensive.

An alternative we found is to keep the DB schema the same --only altering the columns length slightly, which mean we don't need to change our current entity class definitions-- and instead, changing the expression trees on-the-fly, before they reach the l2sql IQueryProvider, and apply a decryption function on the columns I need. I do this by wrapping the pertinent Table<TEntity> properties of my DataContext with a custom IQueryable<TEntity> implementation, which allows me to preview every single query in the system.

In my current implementation, say I've got this query:

var mydate = new DateTime(2013, 1, 1);
var context = new DataContextFactory.GetClientsContext();

Expression<Func<string>> foo = context.MyClients.First(
    c => c.BirthDay < mydate).EncryptedColumn;

but when I catch the query, I change it to read:

Expression<Func<string>> foo = context.Decrypt(
    context.MyClients.First(c => c.BirthDay < mydate).EncryptedColumn);

I do this using the ExpressionVisitor class. In the VisitMember method, I check and see whether the current MemberExpression refers to an encrypted column. If it does, I substitute the expression for a method call:

private const string FuncName = "Decrypt";

protected override Expression VisitMember(MemberExpression ma)
{
    if (datactx != null && IsEncryptedColumnReference(ma))
        return MakeCallExpression(ma);
    }

    return base.VisitMember(ma);
}

private static bool IsEncryptedColumnReference(MemberExpression ma)
{
    return ma.Member.Name == "EncryptedColumn"
        && ma.Member.DeclaringType == typeof(MyClient);
}

private Expression MakeCallExpression(MemberExpression ma)
{
    const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public;
    var mi = typeof(MyDataContext).GetMethod(FuncName, flags);
    return Expression.Call(datactx, mi, ma);
}

datactx is an instance variable with a reference to the expression pointing at the current datacontext (which I look up in a previous pass).

My problem is that if I have a query such as:

var qbeClient = new MyClient { EncryptedColumn = "FooBar" };

Expression<Func<MyClient>> dbquery = () => context.MyClients.First(
    c => c.EncryptedColumn == qbeClient.EncryptedColumn);

I want it to be turned into:

Expression<Func<MyClient>> dbquery = () => context.MyClients.First(c =>
    context.Decrypt(c.EncryptedColumn) == qbeClient.EncryptedColumn);

instead, what I'm getting is this:

Expression<Func<MyClient>> dbquery = () => context.MyClients.First(c => 
    context.Decrypt(c.EncryptedColumn) == context.Decrypt(qbeClient.EncryptedColumn));

Which I don't want, because when I've got an in-memory object, the data is already unencrypted (besides, I don't want a nasty db function call against my objects!)

So, that's basically my question: Having a MemberExpression instance, how can I determine whether it refers to an in-memory object or a row in the database?

Thanks in advance

Edit:

@Shlomo's code actually solves the case I posted, but now one of my previous tests got broken:

var context = new DataContextFactory.GetClientsContext();

Expression<Func<string>> expr = context.MyClients.First().EncryptedColumn;

Expression<Func<string>> expected = context.Decrypt(
    context.MyClients.First().EncryptedColumn);

var actual = MyVisitor.Visit(expr);

Assert.AreEqual(expected.ToString(), actual.ToString());

In this case, the reference to EncryptedColumn isn't a parameter, but it should definitely be taken into account by the visitor!

1 Answer 1

1

A MemberExpression representing a DB row will be a descendent of a ParameterExpression. In-Memory objects will not, they'll most likely come from some form of a FieldExpression.

In your case, something like this will work for most cases (adding one method to your code, and revising your VisitMember method:

private bool IsFromParameter(MemberExpression ma)
{
    if(ma.Expression.NodeType == ExpressionType.Parameter)
        return true;

    if(ma.Expression is MemberExpression)
        return IsFromParameter(ma.Expression as MemberExpression);

    return false;
}


protected override Expression VisitMember(MemberExpression ma)
{
    if (datactx != null && IsEncryptedColumnReference(ma) && IsFromParameter(ma))
        return MakeCallExpression(ma);
    }

    return base.VisitMember(ma);
}
1
  • Thanks a lot to @Shlomo! Your code resolves the case I posted, but actually breaks one of my previous tests. See edit. Thank you anyways, I'm sure your answer got me much closer to a general solution than I was before.
    – alfredochv
    May 31, 2013 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.