What is a DTO and BO? What is the difference?

Sandbox picture Sandbox · Jan 9, 2011 · Viewed 16.7k times · Source

I know DTO is a data transfer object and a BO is a business object. But, what does it actually mean? When should I choose one over the other? From, what I understand DTO is just used to transfer data and doesn't have business logic. Does this mean that a DTO doesn't have any method only properties(getter and setter)? But, it still has properties of a BO. Can someone please explain? Thanks.

Answer

Ladislav Mrnka picture Ladislav Mrnka · Jan 9, 2011

DTO is used to transfer data between layers/tiers. For such purpose it doesn't need any methos and sometimes it even should not have any methods - for example when DTO is exposed over web service.

Business object is clever object which contains data and methods which performs operations (change data) on this object. When you expose BO to upper layer, it can call your object's public methods. Sometimes you don't want this and for that reason you create DTO which only offers data but not methods.

DTO doesn't have to transport all BO data. When you follow strict DTO approach you create specific DTOs for each operation exposed on your business layer. For example if your object has audit data like CreatedBy, ModifiedBy, CreatedDate, etc. and you are creating Update method your incomming DTO (with updated object) doesn't need to have these properties because upper layer cannot modify them - only business logic can.