Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The N-Layer POCO/ DTO quandary

When there were only evil datasets and the microsoft application blocks your transfer objects between layers would be either datasets/datatables or DTO/POCO. I belong to the gang that likes using DTO/POCO.

Now with this sudden wave of mapping layers like SubSonic, Entity Framework, NHibernate etc, should I still be using my favourite POCOs?? I do this mostly and when working with ASP.net webforms 99% times end up using ObjectDataSource for binding to controls and the features specific to each type.

Should I give up this love for POCO and pass around IQueryables or Entities or things like that and make use of other DataSource objects??

What are the pros and cons of using these objects instead of DTOs ?? How will it hit my app design and performance?

EDIT: When will I get to use the other datasources like Linq Datasorce and Entity datasource etc?

like image 599
Perpetualcoder Avatar asked Dec 23 '22 13:12

Perpetualcoder


2 Answers

Long live POCOs and DTOs. They are lightweight, easily serializable, strongly typed, bindable. Never had any performance issues with them, always made my higher level code cleaner.

like image 142
Otávio Décio Avatar answered Jan 14 '23 12:01

Otávio Décio


Use nHibernate or another ORM that can map to POCO:-) Then you can decide to pass DTO or POCO based on how much seperation you want between your producer and consumer.

The question I'd ask is are you goign to pull the data into Entities / IQueryables and then convert them to DTO's? This is a very valid pattern especially if your going to transition a service boundary and don't want to expose your domain model; however there is a trade off. A minimal performance hit depending on the size of the data but that's something you'd need to measue.

Recently I had to display a list of a bunch of Organization objects in a drop downlist. Organization has lots of properties and other child objects. None of which I needed for my drop down all I needed was an ID and the name, and a couple of extra properties...

Here's my HQL query to search the DB and return a list of DTO's using nHibernate:

      return CurrentSession.CreateQuery(
            "select new OrganizationListDTO(o.Id,o.Name,o.xxx,o.xxx)" +
            " from Organization i where i.State=:state")
            .SetParameter("state", state)
            .List<IOrganizationListDTO>();

Point here is that even if your using an ORM you can still leverage DTO's for report like queries and let it do all the heavy lifting.

like image 41
JoshBerke Avatar answered Jan 14 '23 11:01

JoshBerke