Can / if data transfer objects implement interfaces?

I was wondering if anyone can help me with this "architectural dilemma",

I am extracting some objects from different types (classes), for example: posts, docs and pictures

it turns out that in the presentation layer I have to display them in one container: think about the example of sharepoint libraries, where you have a folder, and you can have all kinds of objects inside.

I want to sort them by property (say PublisherDateTime)

I know that if I want to sort them, they must implement the same interface, so they implemented them like (let them say ISortingCapable)

Now, in my domain layer, the interfaces look just fine,

but then I understand that this does not help, because these objects with instantiation moved to the presentation level as DTO (they are no longer the same domain objects) => think of the DTO as a model for presentation in MVC.

Simply put:

if at the presentation level i have

List<PostDTO> List<PictureDTO> List<DocDTO> 

=> Simple objects, simple rendering.

Now I want to sort them in one content stream.

My question is: should DTO implement the same interface again? Or am I looking at the problem from a different perspective?

+4
source share
1 answer

If at some point you need logic that breaks them down again, say that you want to view each individual element that goes to its own controller, you can also aggregate them and enable some navigation properties:

  public IEnumerable<StreamItem> GetStream() { var posts = (from post in postsDb select new StreamItem() { ID = post.ID, Name = post.Name, Url = "Posts/Details/" }); var docs = (from doc in docsDb select new StreamItem() { ID = doc.ID, Name = doc.Name, Url = "Docs/Details/" }); var stream = posts.Union(docs); return stream; } public class StreamItem { public string Url { get; set; } public int ID { get; set; } public string Name { get; set; } } 

Mushin is right, although, saying that each of them should already inherit from the base type, StreamItem may be handy in my case.

You just need the URL (or other identifier) ​​from the view side to determine what it is facing.

If you look at AccountModels in the default MVC project, you will see that the User is a DTO and is managed in many ways (LoginModel, ForgotPasswordModel, RegisterModel), but always returns to the DTO. Things are not always what they appear when viewing a view in DAL;)

0
source

All Articles