Search code examples
design-patternsmvp

Models In Model View Presenter MVP


Would I be correct in thinking that the 'Model' represented by the M in MVP could be a domain model or a presentation/view model?


Solution

  • Yes, the Model could be essentially any Model. The way I see it, the original intention of MVC was that it is a Domain Object, and that is certainly still possible.

    However, my experience have shown that a better fit is achieved if we introduce a specialized ViewModel/Presentation Model as a insulation between the Domain Model and the View.

    Even when the ViewModel seems to be semantically identical to the Domain Object, such an insulation enables us to vary the two independently and thus follow the Single Responsibility Principle.

    It often turns out that the View needs some logic that applies to the specific UI technology, and this logic fits badly in the Domain Model. Examples include

    • Logic that determines if a particular control should be enabled or disabled. Domain Models should know nothing about controls.
    • Logic that maps a state to a color. Colors are technology-specific - they are different CLR types across Windows Forms, WPF and ASP.NET.
    • Validation. Input forms normally allow input of invalid data without throwing exceptions. Instead, they provide feedback to the user that the data is invalid. Domain Objects, on the other hand, should protect their invariants, and thus throw on invalid input.

    More information can be found here