Gabriel Lopez
Gabriel Lopez

Reputation: 159

Object mapper vs Object wrapper

I would appreciate a little help here...

Lets say that in an application we have a Data Layer and a Business Logic Layer. In the DAL we have the following entity:

public class Customer {
    public string Name {get; set;}
    public ICollection<Address> Addresses {get; set;}
}

public class Address {
    public string Street {get; set;}
}

In the BLL we have the following POCOs:

public class CustomerDto {
    public string Name {get; set;}
    public ICollection<AddressDto> Addresses {get; set;}
}

public class AddressDto {
    public string Street {get; set;}
}

The entities in the DAL are populated with a ligth-weight ORM and retrieve from the BLL using a repository. Ex:

public class CustomerInformationService {

    private readonly ICustomerRepository _repository {get; set;}
    public CustomerInformationService (ICustomerRepository repository)
    {
         _repository = repository;
    }
    public class CustomerDto Get(int id)
    {
         var customerEntity = _repository.Get(id);

         var customerDto = /* SOME TRANSFORMATION HERE */

         return customerDTO;
    }
}

My questions is about the /* SOME TRANSFORMATION HERE */ part. There is a discussion in our team about how to do the "mapping".

One approach is to use a mapper either an automapper or a manual mapping. The second approach is to use sort of like a wrapper around Entity and reference the DTO in order to save a copying operation between object. Something like this:

public class CustomerDto
{
     private IEntity _customerEntity;

     public IEntity CustomerEntity { get {return _customerEntity;}}

     public CustomerDto(IEntity customerEntity)
     {
          _customerEntity = customerEntity;
     }

     public string Name 
     { 
          get { return _customerEntity.Name; }
     }
     public ICollection<Address> Addresses 
     { 
          get { return _customerEntity.Addresses; }
     }
}

The second approach feels a little weird to me because _customerEntity.Addresses feels like a leak (_customerEntity's reference) between my DAL and my BLL but I am not sure.

Are there any advantages/disavantages of using one approach over the other one?

Additional info: We usually pull a max. of 1000 records at a time that would need to be transform between Entity and DTO.

Upvotes: 2

Views: 1974

Answers (2)

Amit Joshi
Amit Joshi

Reputation: 16385

You did not mentioned your "ligth-weight ORM". I will answer in two sections.

If you are using ORM that creates proxies

You should avoid exposing Entities outside certain boundary. ORMs like NHibernate/EF implement lazy loading based on proxies. If you expose Entities to application/UI layer, you will have little control over ORM behavior. This may lead to many unexpected issues and debugging will also very difficult.

Wrapping Entities in DTOs will gain nothing. You are accessing Entities anyway.

Using DTOs and mapping them with some mapper tool like AutoMapper is good solution here.

If you are using ORM that does not create proxies

Do NOT use DTOs, directly use your Entities. DTOs are useful and recommended here in many cases. But the example you given in question does not need DTOs at all.

In case you choose to use DTOs, wrapping Entities in DTOs does not make sense. If you want to use Entity anyway, why wrap it? Again, tool like AutoMapper could help.

Refer this question. It's bit different; I am asking Yes/No and you are asking How. But still it will help you.

Upvotes: 1

Mat&#237;as Fidemraizer
Mat&#237;as Fidemraizer

Reputation: 64943

I bet for the service layer approach. Basically because something that looks like a business object or domain object has nothing to do with DTOs.

And, indeed, you and your team should use AutoMapper instead of repeating the same code tons of times which will consist in setting some properties from A to B, A to C, C to B...

Upvotes: 0

Related Questions