locked
Entity Framework and layered structure RRS feed

  • Question

  • I'm creating a new web application with Entity Framework. I want to use the following layered structure:

    User Interface Layer

    Business Object Layer

    Interface

    Entity Framework Layer

    The Business Object Layer now implements the interface on bottom layer using the Entity Framework Objects.

    In the user interface I use a Telerik component (radGrid) to show/edit/update/delete data. Now I can show entity data to user simply doing a simple (linq to entities) query like:

     var stabilimenti = from s in entities.Stabilimenti
                                   select s;

    My problem is to persist data on database when user changes them.

    In my user page i want to catch the "RadGrid1_UpdateCommand" and in some way persist data modification. Debugging i've seen that at the update event i've lost all data modification (the program executes a Page_Load just before).

    How can I persist modification? Have I to use an Entity DataSource and manually place there datas retrieved from Entity Framework entities? Or what else should I do?

    Monday, April 26, 2010 8:25 AM

Answers

  • I have not used Telerik controls, but they should provide some call-back mechanism for data edits. I am sure DevExpress controls does.

    Here is how I designed my application, and it worked well for me:

    • Service Layer (WCF Service) for hosting the .edmx, and Service Methods for CRUD operations (you may think of using the WCF DataServices for this).
    • Public classes with Static methods (in the Web Application) to query the Service Layer and return List<Entity> objects (for data binding), as well as Static Methods for Insert, Update and Delete operations (which take a concrete Entity type as input).
    • ObjectDataSource on ASPX Pages (bound to the above methods for CRUD operations).
    • DevExpress gridview bound to the ObjectDataSource, and CallBacks enabled (for data edits).
    Monday, April 26, 2010 1:47 PM
  • Montanari,

     

    If you want to create an enterprise-grade layered architecture that utilizes EF, you're going to need to break up your layers even further and adopt an MVVM approach that obviates the need for your presentation to be directly coupled to your persistence mechanism. Josh Smith has written a very good introduction to MVVM which can be found here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

    As for how to leverage EF in a layered architecture, I would suggest organizing your projects in the following manner:

    1. Create a project that contains the EDM (but not the generated code) and use the POCO generator to create your domain and context classes

    2. Create a project that contains the generated domain classes referenced in (1). Note that this project has no reference to the EF

    3. Create a project that contains the generated context referenced in (1). This project should reference EF and also the domain assembly specified in (2)

     

    I am using this approach on a new application I'm working on and, thus far, have found no show-stoppers in EF that would precluded my favored architectural styles. EF still has far to go, but it is viable at this point and the EF team has made significant progress in addressing the shortcomings identified in v1.0 that made it effectively unusable for real-world applications.

     

    Monday, April 26, 2010 2:23 PM

All replies

  • I have not used Telerik controls, but they should provide some call-back mechanism for data edits. I am sure DevExpress controls does.

    Here is how I designed my application, and it worked well for me:

    • Service Layer (WCF Service) for hosting the .edmx, and Service Methods for CRUD operations (you may think of using the WCF DataServices for this).
    • Public classes with Static methods (in the Web Application) to query the Service Layer and return List<Entity> objects (for data binding), as well as Static Methods for Insert, Update and Delete operations (which take a concrete Entity type as input).
    • ObjectDataSource on ASPX Pages (bound to the above methods for CRUD operations).
    • DevExpress gridview bound to the ObjectDataSource, and CallBacks enabled (for data edits).
    Monday, April 26, 2010 1:47 PM
  • Montanari,

     

    If you want to create an enterprise-grade layered architecture that utilizes EF, you're going to need to break up your layers even further and adopt an MVVM approach that obviates the need for your presentation to be directly coupled to your persistence mechanism. Josh Smith has written a very good introduction to MVVM which can be found here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

    As for how to leverage EF in a layered architecture, I would suggest organizing your projects in the following manner:

    1. Create a project that contains the EDM (but not the generated code) and use the POCO generator to create your domain and context classes

    2. Create a project that contains the generated domain classes referenced in (1). Note that this project has no reference to the EF

    3. Create a project that contains the generated context referenced in (1). This project should reference EF and also the domain assembly specified in (2)

     

    I am using this approach on a new application I'm working on and, thus far, have found no show-stoppers in EF that would precluded my favored architectural styles. EF still has far to go, but it is viable at this point and the EF team has made significant progress in addressing the shortcomings identified in v1.0 that made it effectively unusable for real-world applications.

     

    Monday, April 26, 2010 2:23 PM