DTO (Data Transfer Object)


DTO usually refers to Data Transfer Objects. Few things I will discuss in this topic:

  • What are DTOs?
  • What is purpose the purpose of having DTO?
  • How to Create?
  • Example Codes.

What are DTO:

Communicating with Database (DB) developer always need a proper mechanism to ensure scalability and implementation of best practices. This also helps in understating the code by others and making it more reusable. This mechanism involves creation of two type classes in your application (remember by technical posts blog are strictly Object Oriented Approach :) ), One is DAO (Data Access Object) which is used to communicate (making query, fetching data, inserting/altering data) with DB (in short CRUD operations) and other is DTO. DTO (for better understating) can be referred as “Entity” in fact on this concept Microsoft has built Entity Framework. Moving further, a DTO class represents a DB entity (Table), table name could be class name and Field Names must be Data Members of class.

What is purpose the purpose of having DTO:

The basic purpose of using DTOs are to make your application independent of DB Entities so at anytime if you change your DB Entity it have no effect on application except few modifications in DAO class. It is very simple to understand, the whole application uses Data Transfer Objects and have nothing to do with DB Table Field Names or Field Count.

How to Create:

Suppose we have a table name “Employee” with fields “EmployeeID” INT, “EmployeeName” VARCHAR, “EmployeeDesignation” VARCHAR, “EmployeeDepartmentID” INT and “EmployeeSalary” Double. So in my application I can have a Class named “EmployeeDTO” with data members “int EmployeeID”, “string EmployeeName”, “string EmployeeDesignation”, “int EmployeeDepartmentID” and “double EmployeeSalary”.

It is not must that Table Field names should match the class Data Member names but it is one of best practices and is highly recommended. It is also in best practices that make Data Members of class “private” and provide “getters” and “setters”.

Example Codes:

Suppose we have a table name Employee with following fields

  • “EmployeeID” INT
  • “EmployeeName” VARCHAR
  • “EmployeeDesignation” VARCHAR
  • “EmployeeDepartmentID” INT
  • “EmployeeSalary” Double

The code in 2 different languages will be as follows

In PHP:

class Employee {
		private $employeeId;
		private $employeeName;
		private $employeeDesignation;
		private $employeeDepartmentID;
		private $employeeSalary;

		function __construct($employeeId, $employeeName, $employeeDesignation, $employeeDepartmentID, $employeeSalary) {
		$this->employeeId = $employeeId;
		$this->employeeName = $employeeName;
		$this->employeeDesignation = $employeeDesignation;
		$this->employeeDepartmentID = $employeeDepartmentID;
		$this->employeeSalary = $employeeSalary;
	}

	public function get_employeeId(){
		return $this->employeeId;
	}

	public function get_employeeName(){
		return $this->employeeName;
	}

	public function get_employeeDesignation(){
		return $this->employeeDesignation;
	}
	public function get_employeeDepartmentID(){
		return $this->employeeDepartmentID;
	}

	public function get_employeeSalary(){
		return $this->employeeSalary;
	}

	public function set_employeeId($employeeId){
		$this->employeeId = $employeeId;
	}

	public function set_employeeName($employeeName){
		$this->employeeName = $employeeName;
	}

	public function set_employeeDesignation($employeeDesignation){
		$this->employeeDesignation = $employeeDesignation;
	}

	public function set_employeeDepartmentID($employeeDepartmentID){
		$this->employeeDepartmentID = $employeeDepartmentID;
	}

	public function set_employeeSalary($employeeSalary){
		$this->employeeSalary = $employeeSalary;
	}
}

In C#:

public class Employee
{
	private int employeeId;
	private string employeeName;
	private string employeeDesignation;
	private int employeeDepartmentID;
	private double employeeSalary;

	public Employee(int employeeId, string employeeName, string employeeDesignation, int employeeDepartmentID, double employeeSalary) {
		this.employeeId = employeeId;
		this.employeeName = employeeName;
		this.employeeDesignation = employeeDesignation;
		this.employeeDepartmentID = employeeDepartmentID;
		this.employeeSalary = employeeSalary;
	}

	public int get_employeeId(){
		return this.employeeId;
	}

	public string get_employeeName(){
		return this.employeeName;
	}

	public string get_employeeDesignation(){
		return this.employeeDesignation;
	}
	public int get_employeeDepartmentID(){
		return this.employeeDepartmentID;
	}

	public double get_employeeSalary(){
		return this.employeeSalary;
	}

	public void set_employeeId(int employeeId){
		this.employeeId = employeeId;
	}

	public void set_employeeName(string employeeName){
		this.employeeName = employeeName;
	}

	public void set_employeeDesignation(string employeeDesignation){
		this.employeeDesignation = employeeDesignation;
	}

	public void set_employeeDepartmentID(int employeeDepartmentID){
		this.employeeDepartmentID = employeeDepartmentID;
	}

	public void set_employeeSalary(double employeeSalary){
		this.employeeSalary = employeeSalary;
	}
}