Entity Framework in n-layered application - Lazy loading vs. Eager loading patterns

Posted by Marconline on Stack Overflow See other posts from Stack Overflow or by Marconline
Published on 2010-04-09T23:44:38Z Indexed on 2010/04/09 23:53 UTC
Read the original article Hit count: 425

Hi all. This questions doesn't let me sleep as it's since one year I'm trying to find a solution but... still nothing happened in my mind. Probably you can help me, because I think this is a very common issue.

I've a n-layered application: presentation layer, business logic layer, model layer. Suppose for simplicity that my application contains, in the presentation layer, a form that allows a user to search for a customer. Now the user fills the filters through the UI and clicks a button. Something happens and the request arrives to presentation layer to a method like CustomerSearch(CustomerFilter myFilter). This business logic layer now keeps it simple: creates a query on the model and gets back results.

Now the question: how do you face the problem of loading data? I mean business logic layer doesn't know that that particular method will be invoked just by that form. So I think that it doesn't know if the requesting form needs just the Customer objects back or the Customer objects with the linked Order entities.

I try to explain better: our form just wants to list Customers searching by surname. It has nothing to do with orders. So the business logic query will be something like:

(from c in ctx.CustomerSet
where c.Name.Contains(strQry) select c).ToList();

now this is working correctly. Two days later your boss asks you to add a form that let you search for customers like the other and you need to show the total count of orders created by each customer. Now I'd like to reuse that query and add the piece of logic that attach (includes) orders and gets back that.

How would you front this request?

Here is the best (I think) idea I had since now. I'd like to hear from you: my CustomerSearch method in BLL doesn't create the query directly but passes through private extension methods that compose the ObjectQuery like:

private ObjectQuery<Customer> SearchCustomers(this ObjectQuery<Customer> qry, CustomerFilter myFilter)

and

private ObjectQuery<Customer> IncludeOrders(this ObjectQuery<Customer> qry)

but this doesn't convince me as it seems too complex.

Thanks, Marco

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about architecture