I am working on a web application for managing files and folders. I am keeping files and folders in separate database tables, but when I retrieve a folder from the backend, it can have child items which can be either folders or files. So rather than deal with separate lists of files and folders in the frontend, I would like to combine them into a single Children property using a DTO class which has properties common to both files and folders. Then I can just display that single list.
Here's an example of how I'm mapping the items to that property:
CreateMap<Folder, FolderDetail>()
.ForMember(dst => dst.Children, opt => opt.MapFrom(src => src.SubFolders.Select(folder => new FolderChildItem()
{
Id = folder.Id,
ModifiedDate = folder.ModifiedDate,
Name = folder.Name,
Type = "Folder"
}).Concat(src.Files.Select(file => new FolderChildItem()
{
Id = file.Id,
ModifiedDate = file.ModifiedDate,
Name = file.Name,
Type = file.Type
}))));
This actually works fine, but the problem is I'd like to recursively map each subfolder's children, on down the tree until no more subfolders are found. Then I can have information contained about the entire tree underneath the main folder being returned by the backend and can display information about all files and folders contained underneath the main folder.
That necessitates adding a Children item to the FolderChildItem DTO. The Children item in the example is on the FolderDetail DTO which is returned at the top-level, the Children item itself being a collection of FolderChildItem's, which can have their own children. I've added a Children property to the FolderChildItem class, but I don't know how to get that mapping to go through recursively for all subsequent children.