What is the recommendation regarding re-using DTO's as a child in another DTO?
I'm using ASP.NET Web API (C#) and consuming the results with Angular 1.x (not sure if that really matters).
I have the following DTOs
class SiteDto {
public int SiteId {get;set;}
public string Name {get; set;}
public DateTime CreatedDate {get;set;}
...
}
class SiteEventDto {
// pk
public int SiteEventId {get; set; }
// fk
public int SiteId {get; set;}
public DateTime EventStartDate {get; set;}
public DateTime EventEndDate {get; set;}
...
}
I want to return a list of SiteEvents with a site name (for example). Is there a recommendation for the link between SiteEvent and Site to get this parent information? For example, I see the following possibilities:
Re-use existing DTO: Use the existing Site DTO as a property within the SiteEvent DTO (e.g.
public SiteDto ParentSite { get; set; }
)- Pros:
- Code re-use
- Scalable
- Cons:
- Potentially a lot of extra bloat (for web-based/json-based calls especially) due to unneeded properties and possibly additional parent hierarchies
- Pros:
Create new parent DTO: Create a child-DTO specific to this task (e.g.
class SiteEventSiteDto {
... ) and reference that through a child property on SiteEventDto- Pros:
- Reduce additional bloat from unused fields
- Cons:
- Minimal code re-use
- Pros:
Flatten class structure: Flatten the class structure to simply include a "SiteName" property in the SiteEventDto (e.g.
public string SiteName { get; set; }
)- Pros:
- Reduce additional bloat (even more)
- Cons:
- Minimal code re-use
- Pros:
It probably makes sense to look at each case individually and assess the following questions:
- Do I foresee a need for additional properties off the parent object?
- How difficult is it to add a new property in a flat structure?
- How much bandwidth is the bloat going to really cost the user?
I guess my questions to the community are:
- Is it common practice to re-use a DTO for more than one controller (scenario #1, above) or is that considered a bad practice for some reason? Or, does it make more sense to create a new child DTO or use a flat structure when possible?
- And lastly, do my ramblings above seem like a good approach to this problem?