Reputation: 2570
I got some services where I return DTOs, now I can reuse the same DTO for different methods.
let's say I have the following interface:
List<ProductDTO> GetProductsByReseller(int reseller)
List<ProductDTO> GetProductsByManufacturer(int Manufacturer)
List<ProductDTO> GetProductsByCategory(int Category)
using the same DTO for them is absolutely valid I think, as they all have really the same fields.
public class ProductDTO
{
public int Id { get; set; }
public string ProductName { get; set; }
public int Reseller { get; set; }
public int Manufacturer { get; set; }
public int Category { get; set; }
}
Now I have some other methods, for example:
List<ProductDTO> GetProductsWithAdminInfoByCategory(int Category)
For this method I have to extend the DTO with some additional fields.
#region only for admin
public int KnownDefects { get; set; }
public DateTime InStockSince { get; set; }
#endregion //only for admin
Is it ok to reuse the existing DTO and simply extend it? Or should I create a new DTO because some fields would never be used from that Method?
What I think: If only 1 or 2 fields are new (never used in the other cases), maybe it's ok, as I not have to create a duplicate DTO and the tradeof for maintaining 2 DTOs instead of 1 is ok. Probably it would be good if I derive a AdminProductDTO from ProductDTO, but I don't know if inheritation is valid for DTOs.
Well am I wrong? What would be a good treshold value (nbr of changes), from when on to create a new DTO?
Update: If I expose a service in WCF that would expose the ProductDTO, will it have to transmit the KnownDefects and InStockSince properties if they are not set or null? Or is WCF clever enough and not need to transmit undefined/null properties at all? (In that case I wouldn't mind to have only one DTO with 2 properties that are not always used.)
Another Update: Well if I inherit ProductAdminDTO from ProductDTO, can I still call the following service with it(that would only accept a ProductDTO)?
bool SaveProduct(ProductDTO)
Well I guess I could simply try that out myself, will update later :)
Upvotes: 1
Views: 823
Reputation: 879
Well it seems you are stuck at classic inheritance Vs Composition problem.
Think whether ProductionWithAdminInfo "ISA" Product? I don't think it satisfies ISA relationship. An extra admin information is just another property of the product.
So composition seems good choice here. ProductWithAdminInfoDTO contains ProductDTO and AdminInfoDTo.
Upvotes: 2