5

I was wondering if someone could help me with this "architectural dilemma",

I'm retrieving some objects from different types (classes) say like: posts, docs and pictures

turns out that in the Presentation Layer I have to render them in a single container: think of 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 a property (let's say a PublishingDateTime)

I know that if I want to sort them, they should all implement the same interface, so Implemented them as (let's say ISortingCapable)

Now, in my Domain Layer, the interfaces look just fine,

but then, I realize that does not help because, these instantiated objects went to the Presentation Layer as DTOs (they are no longer the same Domain Objects) => think of the DTO as the Model for the View in MVC.

Simply put:

if in the Presentation Layer I have a

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

=> Simple objects, simple render.

Now I want to sort them in a single Content Stream.

My question is: Should the DTOs implement the exact same interface again? Or am I looking at the problem from the wrong side?

7
  • You can't sort the domain objects and then convert them to DTOs without destroying the ordering? Converting a business object to a DTO should be a pretty straightforward 1:1 mapping. Treating a DTO more than a simple set of properties seems like a code smell to me. Dec 1, 2012 at 21:54
  • @MushinNoShin yes you are right, but think of this: in the Presentation Layer you have a List<Docs> + List<Pictures> + List<Posts> => simple objects and simple render, but now I need to sort them in a single container (like a Stream), I can't sort them because they are not from the same type. Dec 1, 2012 at 22:00
  • Ok, if they don't have a common type then what are you doing trying to sort them? There must be some common properties, maybe not in implementation but likely in interface. Could the problem reside in the type hierarchy? Dec 1, 2012 at 22:15
  • @MushinNoShin think of the operating system folder, inside a folder you have other folders, pictures and documents, they are all different but you can sort them by a datetime property. My domain objects have a common interface since the domain, but not anymore as DTOs in the Presentation Layer. I really don't know if the problem is the whole world or the problem is just me :-/ Dec 1, 2012 at 22:22
  • 2
    This really sounds like it's solved by adding a common base class. As long as behavior isn't being added to DTOs, it's not breaking separation of concerns.. Dec 1, 2012 at 23:03

1 Answer 1

0

If at some point you need logic that will break them apart again from the view, say you want to view each individual item that goes to its own controller you may as well aggregate them and include 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 though in saying that each of them should already inherit from a base type, probably StreamItem in my case before hand.

You will just need Url (or some other identifier) from the view side to work out what it is on the way back.

If you take a look at the AccountModels in a default project of MVC you will see that the User is the DTO and is manipulated in many ways (LoginModel, ForgotPasswordModel, RegisterModel) but always ends up back at the DTO. Things aren't always what they seem when looking through the View to the DAL ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.