DTO or Domain Model Object in the View Layer?

前端 未结 9 1952
再見小時候
再見小時候 2020-12-22 17:32

I know this is probably an age-old question, but what is the better practice? Using a domain model object throughout all layers of your application, and even binding values

相关标签:
9条回答
  • 2020-12-22 18:23

    It really depends on the complexity of your application. Mixing domain objects into the view layer has two possible implications:

    1. You'll be tempted to modify your domain object to accommodate things you need in the view layer
    2. Your View layer will contain extra complexity caused by a mismatch between what your domain objects offer and what your view really needs. You might not be able to get around this complexity but it probably doesn't belong in the View layer.

    If your domain objects are simple and your views are few, skipping the DTOs might be the simplest thing.

    On the other hand, if your domain model is likely to evolve and become complex and if your views are likely to be numerous and varied, having view specific objects might be a good idea. In the MVC world, using ViewModels is common and makes a lot of sense to me.

    0 讨论(0)
  • 2020-12-22 18:25

    The behavior of a class, or its internal methods should not be exposed to layers not concerned with their behaviour. Transfer data, not behaviour. Use domain objects within the domain. The web is not a controlled domain, and UI developers need not be concerned with domain behaviour, just data.

    The domain has to be encapsulated, and protected from modification by someone not concerned with the health of the domain.

    Leaking behaviour is not the best habit.

    If it is a small project, built it with correct principes as well. That way we always keep in mind why we do what we do, and not just how.

    0 讨论(0)
  • 2020-12-22 18:27

    I think what we should consider here in the first place is the cost of introducing new layer. Take DTO for instance - doing that we need a mapping. As someone said, translation is evil and should be avoided whenever possible.

    On the other hand I think there are very few things that you generally should not do. Those who say all DTOs are evil are wrong - it always depends on the use case! Are they really justified?

    And finally, I personally believe the domain objects should be let go to the view itself. Imagine what the wicket integration is then like. But take Spring MVC for instance - domain would stay then in the application layer probably...

    0 讨论(0)
提交回复
热议问题