Need help on DTO layer RRS feed

  • General discussion

  • Hi Guys,

    I need your help a very simple query that confusing me. The query is

    1. I have simple architecture(3 tier) PL, BLL and DAL.

    2. In my DAL i am using entity framework i.e the EDMX version 4.0 i.e VS 2010 is placed in my DAL.

    3. The confusion or query is, i am exposing my entities directly to my PL for some scenario, Ok. i ll give you the exact scenario, I have created the store procedure, added it into edmx with complex type.

    So, my store procedure return me a list of complex type. in this scenario what happened on my PL i am directly calling entities or complex type store procedure for filtering out data from the list. For that i need to mentioned complex type into my list which is a direct reference of my edmx file on presentation layer(PL) which is not a proper way or not a good programming, because i am bypassing BAL and directly calling on PL. To avoid the scenario, am thinking to use DTO as layer in the application.


    But the query is that where i can placed this wrapper or DTO in the application and how it is secure?


    Your reply will really helpful.

    Saturday, December 17, 2011 7:22 PM

All replies

  • Typically, DTOs are just defined in a shared "common" assembly which is used by both the PL and the lower level assemblies. In a perfect world, the types within each layer should be kept entirely within that layer, and a DTO should be used only to exchange data between layers. As with all patterns and requirements, though, you have to balance whether the extra effort is really worth it for your scenario.

    So, for example, you should have your data entities defined within the DAL. Before sending data from the DAL to the BLL, you should map it to DTOs, then within the BLL map from your DTO to a "logical" entity. A lot of times this is handled by ORMs, so it's pretty common to just skip this mapping and use the same POCO classes in both the DAL and BLL, especially if you're using EF.

    From your BLL, you want to map all of your POCO entities to DTOs, then return the DTO to the PL. Mapping logic from a DTO to a POCO should reside in the business logic layer - the presentation layer should have no knowledge of the POCOs and shouldn't care about them. All of the mapping is an implementation detail of the BLL, so converting from DTOs to POCOs should be done there.

    In the PL, I typically recommend translating from the DTOs to a view models. View models may differ from your DTOs depending on how the UI is designed, and your UI shouldn't have to change significantly just because DTOs change. I recommend writing an extra layer to translate from DTOs to view models in the PL, but again this step is commonly skipped and DTOs are often used to drive the PL, especially in smaller applications.

    Check out My Blog for tech news, development tips, and other information for geeks like me.
    Monday, December 19, 2011 5:30 PM