In which scenario can I use those design patterns in n-tier architecture?
Comparing DTO objects with value objects is like comparing oranges and apples.
They serve completely different situations. DTO defines the object / class structure of how data will be transferred between layers while value objects defines the logic for equality when values are compared.
Let me explain you with examples, let us first try to understand value objects first :-
Value object is an object whose equality is based on the value rather than identity.
Consider the below code we have created two objects of money one is one rupee coin and the other is a one rupee paper currency.
Money OneRupeeCoin = new Money();
OneRupeeCoin.Value = 1;
OneRupeeCoin.CurrencyType = "INR";
OneRupeeNote.Material = "Coin";
Money OneRupeeNote = new Money();
OneRupeeNote.Value = 1;
OneRupeeCoin.CurrencyType = "INR";
OneRupeeNote.Material = "Paper";
Now when you compare the above objects the below comparison should evaluate to true because 1 rupee note is equal to 1 rupee coin in real world.
So either you are using “==” operator or you are using the “Equals” method the comparison should evaluate to true. By default “==” or “equals” will not evaluate to true so you need to use operator overriding and method overriding to get the desired behavior. You can see this link which explains how to achieve the same.
if (OneRupeeCoin==OneRupeeNote)
{
Console.WriteLine("They should be equal");
}
if (OneRupeeCoin.Equals(OneRupeeNote))
{
Console.WriteLine("They should be equal ");
}
Normally value objects are good candidates for immutability; you can read more about it from here. You can see this video which describes how immutable objects can be created.
Now let’s try to understand DTO:-
DTO (Data Transfer objects) is a data container for moving simplifying data transfer between layers.
They are also termed as transfer objects. DTO is only used to pass data and does not contain any business logic. They only have simple setters and getters.
For example consider the below call we are making two calls one to get customer data and the other to get product data.
DataAccessLayer dal = new DataAccessLayer();
//Call 1:- get Customer data
CustomerBO cust = dal.getCustomer(1001);
//Call 2:- get Products for the customer
ProductsBO prod = dal.getProduct(100);
So we can combine the Customer and Product class in to one class as shown below.
class CustomerProductDTO
{
// Customer properties
public string CustomerName { get; set; }
// Product properties
public string ProductName { get; set; }
public double ProductCost { get; set; }
}
Now with one call we will be able to get both customer and product data. Data transfer objects are used in two scenarios one to improve remote calls and second to flatten object hierarchy; you can read this article which explains more about data transfer objects.
//Only one call
CustomerProductDTO cust = dal.getCustomer(1001);
Below is the complete comparison sheet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With