Home  /  Questions  /  Question

88   96.5
May 11, 2012

E.F 4.1 CodeFirst,Generic Repository Pattern,UoW,MVP

 Hİ ,

    I developed a windows form project with using  E.F 4.1 CodeFirst model, Generic RepositoryPattern, Unit Of Work and MVP.
    I made 4 layers which are like below.

        DataAccessLayer (Entities like tblCustomers, tblUsers,  Repositories like CustomerRepository,UserRepository)
        UI -->Windows Forms.

    My Problem is carrying exceptions beetween the layers and  carriying the data between the layers. I made a DTO datatransfer objects which names like  CustomerDto, UserDto.
     When I query  generic repository      forexample      CustomerRepository<tblCustomers>  it returns tblCustomer type data , but i want to fill CustomerDto class , so i have to write query business layer, But  i have to this job in  Data Access Layer  ,  I dont want to carry tblCustomer entities between the layers.

     Is it correct what i have done, if wrong how can i do..


      Best Regards.


588   99.9
Jun 01, 2012

I've done similar things, and my suggestions are:
  1. AutoMapper - use this component to automatically map your DTOs to your business objects
  2. Service Layer - I have a service layer for this type of thing.  Gather the objects or collection from your repository, then in the service layer you can map the objects together.

You can find out about AutoMapper at AutoMapper.org.

I hope this helps.

King Wilder

// First configure the mapping...
AutoMapper.Mapper.CreateMap<Source.Entity.Categories, Destination.CategoriesDto>();

// Then map the objects and return the newly mapped object.
CategoryDto dto = AutoMapper.Mapper.Map<Source.Entity.Categories, Destination.CategoriesDto>(entity);

Hi King, thanks for your answer.I will research automapper, The second answer Service Layer will u use it instead of business layer or do u use it with businesslayer. I found a link Data Transfer Objects with GenericRepositoryPattern like below http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/ What is your opinion about the link ? Thanks. Best Regards. --- Volkan Genç  Jun 04, 2012
@Volkan - that blog post is good. It has good information. In his article he uses an &quot;assembler&quot; to map one object to another. I just call those objects &quot;mappers&quot;, but they do the same thing. DTO's in general are not needed everywhere, all the time. In my opinion, they are only needed if you want to flatten your model for presentation purposes. In other words you can make your DTO's contain properties that are only needed for the UI and not necessarily represent your entity. Regarding your Service Layer question, I use a Service Layer WITH my Business Objects, not instead of. I want my Business Objects (sometimes called Domain Objects), to be a clean representation of the data entities. This way they can be easily used by any layer. I hope this answers your questions. --- King Wilder  Jun 11, 2012