Archive for May, 2010

Sample Provider for Entity Framework 4.0 is available

Today Entity Framework team released an update to Sample Provider for Entity Framework 4.0. The provider is intended to be an example for ADO.NET provider writers but can also be used and hacked upon by everyone interested in the inner workings of an EF provider.

The sample can be downloaded from MSDN Code Gallery. It is licensed under Microsoft Public License (Ms-PL). EFQuerySamples includes additional examples of configuring and using the sample provider.

Tags: , ,

2 Comments


Update to EFProviderWrappers is available

I’ve just posted a tiny update to EFProviderWrappers sample. It fixes the issue which was reported to me by several users, where NotSupportedException was being thrown during SaveChanges() from caching provider code.  

You can download updated bits from MSDN Code Gallery. If you find any other issues, please report them as comments here.

Tags: , ,

4 Comments


Attaching LINQ expression trees to delegates

Inspired by discussion on Twitter today, I started exploring the possibility of attaching LINQ expression trees to compiled delegates. The idea is that you could pass around Func<T> instead of Expression<Func<T>> yet still be able to retrieve original expression tree used to compile the delegate. This makes some scenarios (testing, logging) much cleaner, because you don’t need to deal with expressions as much.

But how do you attach anything to a delegate and Func<T> in particular? Aren’t delegates essentially function pointers?

Well – in .NET each delegate holds information about both the target object instance and the method to invoke on it. This makes it possible to create a tiny wrapper object that would have a function which behaves like Func<T> but holds additional state. Let’s look at a simple wrapper for Func<T> which stores expression:

public class FuncWithExpression<TResult>
{
    private readonly Func<TResult> wrappedFunction;

    public FuncWithExpression(Expression<Func<TResult>> expression, Func<TResult> func)
    {
        this.Expression = expression;
        this.wrappedFunction = func;
    }

    public TResult Function()
    {
        return wrappedFunction();
    }

    public Expression<Func<TResult>> Expression { get; private set; }
}

Let us also define two of extensions methods: one which compiles Expression<Func<T>> and wraps the result with FuncWithExpression<T> wrapper and the other one which extracts the expression from Func<T>:

public static class ExtensionMethods
{
    public static Func<T> CompileAndWrap<T>(this Expression<Func<T>> expression)
    {
        Func<T> compiledExpression = expression.Compile();
        return new FuncWithExpression<T>(expression, compiledExpression).Function;
    }

    public static Expression<Func<T>> GetExpression<T>(this Func<T> func)
    {
        var exp = func.Target as FuncWithExpression<T>;
        if (exp != null)
        {
            return exp.Expression;
        }

        return null;
    }
}

That’s it! (Of course, in real life you would need to provide overloads for more Func<> types, since functions without arguments are not typically very useful). You can now write the code using just Func<T> and still be able to retrieve original expression tree used to compile the delegate:

class Program
{
    static void PrintAndExecute(Func<int> func)
    {
        Expression originalExpression = func.GetExpression();
        if (originalExpression != null)
        {
            Console.WriteLine("expression: {0}", originalExpression);
        }

        Console.WriteLine("result: {0}", func());
    }

    static void Main(string[] args)
    {
        var body = Expression.Add(Expression.Constant(3), Expression.Constant(9));
        Expression<Func<int>> expr = Expression.Lambda<Func<int>>(body);
        Func<int> compiledExpression = expr.CompileAndWrap();

        PrintAndExecute(compiledExpression);
    }
}

This will print:

image

No Comments


Improvements in generated SQL in EF v4.0

Kati from EntityFramework team has published a detailed post about improvments in generated SQL in Entity Framework v4.0. The improvements are really impressive and make generated SQL perform better and and look much closer to hand-written query code. The best thing is: almost all of the improvements are provider-agnostic, so you will see automatic improvement on queries running on 3rd-party providers. In some cases (LIKE optimization) you will need to use EF4-specific version of the provider.

Read more on http://blogs.msdn.com/adonet/archive/2010/05/10/improvements-to-generated-sql-in-net-4-0.aspx

Tags: ,

1 Comment



SetPageWidth