Use EdmFunctionAttribute attribute without disclosing dependency details / Entity Framework implementation?

Is there a good way to use EdmFunctionAttributeEntity Framework / System.Data.Entity.dll dependency without introducing dependency?

I thought that I could have an interface with a method and a specific implementation that implements a method using EdmFunctionAttributeto map it to a database function.

I have a contextual interface IMyContextdefined in an assembly and an Entity Framework implementation MyContextin another assembly.

public interface IMyContext
{
    double SomeFunction(double first, double second);

    // other interface details here
}

public partial class MyContext : IMyContext
{
    [EdmFunction("MyNamespace", "MyDatabaseFunction")]
    public double SomeFunction(double first, double second)
    {
        throw new NotSupportedException("This method may only be called as part of a LINQ expression.");
    }

    // rest of interface implementation here
}

I use factory (using StructureMap behind the scenes) to get a context instance as an interface type:

using (IMyContext context = ContextFactory.GetNewContext())
{
    var results = context.Table.Select(t => context.SomeFunction(t.Col1, t.Col2)).ToList();
}

This calls a NotSupportException, saying that LINQ to Entities does not recognize the "Double SomeFunction (Double, Double)" method.

using (MyContext context = ContextFactory.GetNewContext() as MyContext)
{
    ...
}

, , .

, .

+5
2

IQueryable inteface , :

IQueryable FindAll(Func<T,bool> exp);
+1

, Linq , . , , - .

Expression Visitor .

TypeReplacer tr = new TypeReplacer();
tr.Visit(ex);

class TypeReplacer: ExpressionVisitor{
    protected override MethodCallExpression MethodCall(MethodCallExpression exp)
    {
       // compare exp.Method and 
       // replace it with concrete Type method
       return exp;
    }
}
0

All Articles