I have the following domain model:
public class Playlist { public long Id { get; set; } public string Title { get; set; } public virtual ICollection<Song> Songs { get; set; } } public class Song { public long Id { get; set; } public string Name { get; set; } public virtual Playlist Playlist { get; set; } public virtual ICollection<Catalog> Matches { get; set; } } public class Catalog { public long Id { get; set; } public string Title { get; set; } }
My service has the following code:
public PlaylistResult FindByPlaylistId(long id) { Playlist playlist = playlistRepository.GetById(id); foreach (var song in playlist.Songs) { song.Matches = catalogRepository.GetMatches(song.Name).ToList(); } return new PlaylistResult(new PlaylistDTO(playlist), playlist.Songs.Select(x => new SongDTO(x))); }
My service receives a playlist and songs from the database, and then for each song in the playlist, it launches a query to get additional matches from the database (using full-text search on SQL Server) specific to this song.
Then the data is converted to DTO, added to the result object, and passed back to the controller. The code looks like this:
public class PlaylistResult { public PlaylistResult(PlaylistDTO playlist, IEnumerable<SongDTO> songs) { Playlist = playlist; Songs = songs; } public PlaylistDTO Playlist { get; private set; } public IEnumerable<SongDTO> Songs { get; private set; } }
Problem:
The PlaylistResult object has been working fine so far, but the recent introduction of matches has complicated the situation. It looks like I have no choice but to change my SongDTO to allow for matches and look like this:
public class SongDTO { public SongDTO(Song song, IEnumerable<CatalogDTO> matches) { Id = song.Id; Name = song.Name; Matches = matches; } public long Id { get; private set; } public string Name { get; private set; } public IEnumerable<CatalogDTO> Matches { get; private set; } }
But doesn't that violate the purpose of the DTO? I understand that DTO is a flat representation of the data, and this approach is not flattened. On the other hand, I donβt see how to do this, since each match depends on each song.
I know that I could make it easier for myself and throw away the DTO and pass the domain model directly to the controller and call it day. But I do not want to do this, because the goal is to learn how to work with DTO.
Any input is welcome.
design-patterns asp.net-mvc-3 domain-driven-design ddd-repositories
Thomas
source share