Question

I have an n-tier application design problem with Entity Framework 4.

Server-side, my application has 3 layers : - one Service layer (WCF) - one Business layer - one DataAccess layer (working with EF4)

The Entities are POCO, place in a independent project/assembly.

I'm using Dependency Injection for creating objects of the business and the DataAccess layers, therefore i only work with interfaces, and I have no dependency to EF in my business layer.

I'd like to use EF Compiled Queries to improve performances.

But my (business) queries are defined in the business layer, since in my architecture, the DataAccess is only providing the CRUD methods.

So the compiled queries should be defined in business layer, but i have no dependency to EF and don't want one to keep loose coupling, hence, i can't call the ObjectContext from there.

And defining the queries in DataAccess does not fit my application design.

So does anyone know if there is a generic way to inject the business queries from the business layer to the DataAccess layer, so that i can use them with Compiled queries ?

I've tried many things and looked everywhere, and couldn't find an answer to this ... :( looks like EF does not fit well with that kind of n-tier application.

Was it helpful?

Solution

Compiled query belongs to data access layer because it is data access specific feature dependent on data access classes. If your design doesn't accept this it is simply not ready for data access specific features = either change your design or don't use data access specific features.

The simplest way is to add compliled queries to your context and expose them as methods on the context:

public IQueryable<SomeEntity> SomeQuery(string someParam) {
    return compiledQuery.Invoke(this, someParam);
}

Now you will add these methods to the interface for the context.

You can follow similar approach and expose compiled query on any data access interface visible in business layer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top