none
Entity framework / multi layer architecture RRS feed

  • Question

  • 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). In my case all these layers are on the same machine so I don't think people saying use WCF is going to help me.

    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 (something to do with proxy properties) - ideally the reference would be the other way round. If the DAL has a reference 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. So in my BLL classes I would have methods like loadFromDAL() and saveToDAL() and the individual properties of the 2 objects are copies back and forth, and within the BLL class there would be a reference to its DAL object counterpart. It might seem a bit tedious (however I would have a code generator that I roll myself) but at least I would have the separation I want.

    I'd appreciate people's thoughts on the subject (I know this question has been asked before but the answers people give normally just confuse me even more rather than clearing things up), I really have spent hours and hours reading about EF but struggle to see how it can work in a multi layered approach, surely it must be a common thing people want to do, you'd expect there to be a few clear/complete examples / walk throughs somewhere.

    Thanks,

    Paul

    • Moved by Cookie Luo Wednesday, July 13, 2011 6:13 AM (From:Visual C# General)
    • Edited by puma75 Wednesday, July 13, 2011 10:59 AM
    Monday, July 11, 2011 8:13 AM

All replies

  • This sounds like a good question for the entity framework forum

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Tuesday, July 12, 2011 12:00 AM
  •  

    You can add a layer between ur UI and BLL i.e. a service layer that would call your BLL and you can base your BLL on interfaces which therefore would call your Data layer with interfaces.

    You can also have your POCOs in BLL that would map them to DTOs and thus would be returned ot UI via Service layer. 

    Follow the following articles on onion layered architecture. I am sure they would help.

    http://hendryluk.wordpress.com/2009/08/17/software-development-fundamentals-part-2-layered-architecture/

    http://jeffreypalermo.com/blog/the-onion-architecture-part-1/

    Regards,

    Samreen

    Tuesday, July 12, 2011 4:56 AM
  • Here's something that may work for you.  Create a WCF project that encapsulates the EF 4.1 model.  Then add a service reference to the "other main" project.  You now have a true DAL.  You can then move business logic to either the WCF project or you can put it into the model of the main project.  If you are working with Silverlight RIA goes even one step futher in that it automatically creates the WCF layer and then it injects a ORM mapping into the main project.  All of the DAL stuff is now accessable via true POCO/Class objects.
    JP
    Tuesday, July 12, 2011 5:24 AM
  • Thanks, but all the components (asp.net website / BLL project / DAL project) are on the same machine, surely you would only want to use WCF in client-server type cases where things get communicated between server and client components.

    Wednesday, July 13, 2011 10:10 AM
  • You can add a layer between ur UI and BLL i.e. a service layer that would call your BLL and you can base your BLL on interfaces which therefore would call your Data layer with interfaces.

    You can also have your POCOs in BLL that would map them to DTOs and thus would be returned ot UI via Service layer. 

    Follow the following articles on onion layered architecture. I am sure they would help.

    http://hendryluk.wordpress.com/2009/08/17/software-development-fundamentals-part-2-layered-architecture/

    http://jeffreypalermo.com/blog/the-onion-architecture-part-1/

    Regards,

    Samreen

    Thanks but as those links have nothing to do with entity framework so even if I tried to follow that approach which seems a more complicated than what im trying to do at the moment, I still wouldn't know how to use the approach in those links together with entity framework.
    Wednesday, July 13, 2011 10:53 AM
  • On 7/13/2011 6:53 AM, puma75 wrote:
    >     You can add a layer between ur UI and BLL i.e. a service layer that
    >     would call your BLL and you can base your BLL on interfaces which
    >     therefore would call your Data layer with interfaces.
    >
    >     You can also have your POCOs in BLL that would map them to DTOs and
    >     thus would be returned ot UI via Service layer.
    >
    >     Follow the following articles on onion layered architecture. I am
    >     sure they would help.
    >
    >
    >
    >     Regards,
    >
    >     Samreen
    >
    > Thanks but as those links have nothing to do with entity framework so
    > even if I tried to follow that approach which seems a more complicated
    > than what im trying to do at the moment, I still wouldn't know how to
    > use the approach in those links together with entity framework.
     
    Those links have something to do with n-tier architecture.
     
    The OP is talking about a service layer, and it doesn't matter what the
    client type application is ASP.NET, Windows desk, Console application or
    Windows service that is using the service layer.
     
    The Service Layer is being talked about in the link below. And I am not
    talking about a WCF service or an ASP.NET Web service either. You may
    want to use MVP for ASP.NET and Windows desktop UI solutions. However,
    what you need to look at here is what is happening with the Service
    Layer which is interchangeable with the client program type.
     
    The BLL, Service Layer and DAL should be interchangeable and the client
    application doesn't care it just uses them.
     MODEL-VIEW-PRESENTER
     
     
    click 'Shows'
     
    click 'Design Patterns Bootcamp: Model View * Patterns*
     
    view parts  1-5
     
     
    Wednesday, July 13, 2011 11:49 AM
  • Incorrect, you can put WCF on same machine. 
    JP
    Wednesday, July 13, 2011 12:22 PM
  • Ok forget WCF, just use Asynchrounous callbacks in conjuntion with EF.  Create a MVVM project where the model has the EF model.  Create a separate class in model that uses Asynchronous interface to EntityObjects (Gets, Inserts, Updates, Deletes etc).
    JP
    Wednesday, July 13, 2011 12:24 PM
  • "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. So in my BLL classes I would have methods like loadFromDAL() and saveToDAL() and the individual properties of the 2 objects are copies back and forth, and within the BLL class there would be a reference to its DAL object counterpart. It might seem a bit tedious (however I would have a code generator that I roll myself) but at least I would have the separation I want."

     

    I believe that will work.  This is same thing I mentioned above with the Asynch intermediary class.


    JP
    Wednesday, July 13, 2011 12:27 PM
  • > The OP is talking about a service layer, and it doesn't matter what the
    > client type application is ASP.NET, Windows desk, Console application or
    > Windows service that is using the service layer.
    I am the original poster and I wasn't talking about a service layer, I am talking about 3 different layers (not tiers if tiers means running on seperate machines) all running on the same server.
    I'm not trying to do anything with client server architecture I just want to split my projects into  ASP.NET web application  ->  BLL (Business Logic Layer) -> DAL (Data access layer)
    I just want to know how to make this situation work with entity framework, but people's answers always seem to go off on tangents to different design patterns and stuff (not that im criticising - I do appreciate people taking the time to answer).
    Where I got stuck is I created POCO classes using the T4 template thing then relocated these POCO classes into the BLL project, this requires a reference from the DAL to the BLL project which doesn't seen ideal, you'd want the reference the other way round.  But because I can't have a reference from the BLL project to the DAL project (because that would result in a circular reference) I dont know how I can deal with the object context that is inside the DAL from the BLL.
    I think I'll just end up doing my own thing and coming up with a solution that means I create the BLL classes, I just wanted to see if anyone else had any ideas or whether I was missing something first.
    Wednesday, July 13, 2011 3:14 PM
  • On 7/13/2011 11:14 AM, puma75 wrote:
    >  > The OP is talking about a service layer, and it doesn't matter what the
    >  > client type application is ASP.NET, Windows desk, Console application or
    >  > Windows service that is using the service layer.
    > I am the original poster and I wasn't talking about a service layer, I
    > am talking about 3 different layers (not tiers if tiers means running on
    > seperate machines) all running on the same server.
     
    Whatever..... call them logical tiers or layers sitting on a single machine.
     
    > I'm not trying to do anything with client server architecture I just
    > want to split my projects into ASP.NET web application -> BLL (Business
    > Logic Layer) -> DAL (Data access layer)
    > I just want to know how to make this situation work with entity
    > framework, but people's answers always seem to go off on tangents to
    > different design patterns and stuff (not that im criticising - I do
    > appreciate people taking the time to answer).
     
    Yeah, I am working on a project that's two-layers. The Web UI with
    BLL/DAL and the programmers did their on thing. The application is a
    total nightmare to maintenance, because Service layer is not there.
     
    > Where I got stuck is I created POCO classes using the T4 template thing
    > then relocated these POCO classes into the BLL project, this requires a
    > reference from the DAL to the BLL project which doesn't seen ideal,
    > you'd want the reference the other way round. But because I can't have a
    > reference from the BLL project to the DAL project (because that would
    > result in a circular reference) I dont know how I can deal with the
    > object context that is inside the DAL from the BLL.
     
    You don't deal with the context in the BLL. The context is in the DAL,
    poco are in the DAL and the BLL has reference to the DAL where the BLL
    can see those POCO(s). All the DAL does is the CRUD with the POCO based
    on it giving POCO(s) to the BLL or the BLL sends the POCO(s) to the DAL
    based on the BLL calling methods on the DAL for CRUD operations.
     
    > I think I'll just end up doing my own thing and coming up with a
    > solution that means I create the BLL classes, I just wanted to see if
    > anyone else had any ideas or whether I was missing something first.
     
    The ideas where given to you by two posters. You choose to ignore the
    information on what should be in place.
     
    Wednesday, July 13, 2011 6:27 PM
  • > The ideas where given to you by two posters. You choose to ignore the
    > information on what should be in place.
    I don't choose to ignore it, I either don't understand it or don't see how it solves my original question.
    Anyway there is now 2 different forum posts going on the same topic/question, this one started by me and the other one started by Jullin (which we have both posted on):
    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/7bd84c7b-31c6-487c-a51b-2f10e4231e8a
    I've posted some more recent posts over on the other one if you want to jump over there and leave this one, I had a question about DTO's within BLL objects!
    Thanks,
    Paul
    Wednesday, July 13, 2011 6:59 PM
  • On 7/13/2011 2:59 PM, puma75 wrote:
    >  > The ideas where given to you by two posters. You choose to ignore the
    >  > information on what should be in place.
    > I don't choose to ignore it, I either don't understand it or don't see
    > how it solves my original question.
    > Anyway there is now 2 different forum posts going on the same
    > topic/question, this one started by me and the other one started by
    > Jullin (which we have both posted on):
    > I've posted some more recent posts over on the other one if you want to
    > jump over there and leave this one, I had a question about DTO's within
    > BLL objects!
    > Thanks,
    > Paul
     
    I posted my response to paul here in this NG. You may want to direct him
    back here.
     
    Wednesday, July 13, 2011 7:28 PM