MVC model design / inheritance


Forgive the vague title, I wasn't sure how to describe it.

If you have a generic model "Archive", how do you show different views/forms based on a user selected 'type'?

For example, the user creates a new "Archive", then gets the choice of video, book, audio etc. From there they get different forms based on the archive type.

Or would it be better to split them into different models – Video, Book, Audio?

Or can models inherit (like Video extends Archive). I guess this is basic OOP / classes, but have no idea how to apply that here.

Examples from any MVC framework are welcome!

Best Solution

Seems like you would not want to have the type inherit from Archive. "Always favor encapsulation/containment over inheritance".

Why not create a class called Archive and give it a type property. The type can use inheritance to specialize for Audio, Video, etc.

It would seem that you would specialize Archive based on some other criteria. "FileSystemArchivce", "XMLArchive", "SQLArchive" and the type would not change. But the agilist in me says that this may not be necesscary at first, and you can always refactor the design later...

In terms of a controller, you probably get the biggest bang for the buck by encapsulating the differences of presentation for each type in the view. So only the view changes based on the type. Likely the semantics and rules for each one are the same and you would not need to have seperate controllers for each type. The views will be different for each type as it will have different attributes.