Paul
Paul

Reputation: 1006

Entity framework in a multi layered application

Im trying to use EF4.1 in a setup where I would have a DAL (data access layer), BLL (business logic layer) and then a presentation layer (usually ASP.NET web app or console app maybe).

Currently I do all my projects using LINQ to SQL and have a set up that uses multi layers as descibed above.

When looking at EF though all the examples just seem to use 1 layer or maybe 2 layers its hard to find a full example that uses a DAL and BLL.

The closest I've got to what I want at the moment is to use the POCO code generator to create POCO classes and then re-locate these classes into my BLL project - and the .edmx and the object context remain in the DAL.

The problem with this though is the DAL needs a reference to the BLL so that it knows about the POCO classes - ideally the reference would be the other way round. If the DAL has a refernce to the BLL I cant also have a reference from the BLL to the DAL - because you can't have circular references, so I cant do anything with the object context like call the save changes method from the bll.

Anyway I've got in a bit of a mess with it all.

I'm considering doing things in a different way (similar to what I do currently with LINQ to SQL) where the EF entity objects stay in the DAL and I write my own BLL classes that just 'piggy back' onto their DAL object counter parts. That way the DAL can have a reference to the BLL and the BLL.

Sorry its a long question but appreciate people's thoughts, I really have spent hours and hours reading about EF but struggle to see how it can work in a multi layered approach.

Upvotes: 3

Views: 530

Answers (1)

David
David

Reputation: 270

Well I've asked myself the same question some months ago.

I've found a whitepaper from Microsoft about layering, and what I can recall is that they consider EF to be a BLL+DAL component in some scenarios.

and in my case, here's what I've been doing so far :

  • I only create two layer : presentation (ASP.Net) + Business Layer (containing EF classes, EDMX and DB Context)
  • I manage the DB Context within the Presentation layer (to have more control through Sessions or HttpRequests over my EF-objects life cycle)

In fact, I merge BLL + DAL. But I have to admit I'm not involved in projects with very complex business processes. I know it's not perfect from an strict architectural point of view, but it works well for the kind of project I have to handle.

In the first times I've been creating the 3 layers but it comes to a nightmare when you have to constantly create 'dumb' classes to map DAL and Presentation..

Depends on your data and business model complexity I guess..

Upvotes: 1

Related Questions