6

Binding directly the form to your model helps a lot to get rid of boiler plate code, but that means that your model must have a getter/setter for each property otherwise it wouldn't be possible. Another choice would be to create another layer (DTO) only to carry the data to/from the form and then you can have a rich domain model not necessarily with getter/setter for each attribute but that means duplication of fields and validation code.

For instance, right now I'm doing a web mail application. We know that we already have the Java Mail API, a good rich domain model. However, the way it is designed makes it impossible to bind my web form to that model. I am forced to create a DTO to capture the data and then pass it to the Java Mail API. Just like this example if my domain model would be like this one, the same would happen.

From Spring MVC documentation:

Instead, it is often preferable to bind directly to your business objects.

Reusable business code, no need for duplication. Use existing business objects as command or form objects instead of mirroring them to extend a particular framework base class.

Do MVC web frameworks such as Spring MVC and Struts 2, favor anemic domain model in order to avoid duplication?

Alfredo Osorio
  • 904
  • 1
  • 11
  • 18

2 Answers2

7

In Martin Fowler's article you linked, he says of the Anemic Domain Model:

At first blush it looks like the real thing. There are objects, many named after the nouns in the domain space, and these objects are connected with the rich relationships and structure that true domain models have.

The catch comes when you look at the behavior, and you realize that there is hardly any behavior on these objects, making them little more than bags of getters and setters. Indeed often these models come with design rules that say that you are not to put any domain logic in the the domain objects.

The fundamental horror of this anti-pattern is that it's so contrary to the basic idea of object-oriented design; which is to combine data and process together. What's worse, many people think that anemic objects are real objects, and thus completely miss the point of what object-oriented design is all about.

However, having said that, he also says this:

Putting behavior into the domain objects should not contradict the solid approach of using layering to separate domain logic from such things as persistence and presentation responsibilities. The logic that should be in a domain object is domain logic - validations, calculations, business rules - whatever you like to call it.

This is where View Models come in.


View Models contain two things:

  1. All of the data that is required to execute a view, and
  2. Any logic that is required to render the view, but which can be pushed back into the ViewModel, rather than cluttering the view.

If the ViewModel does not contain any logic, then it is could properly be called an Anemic Model. But it is not an anemic model of the domain. While it might contain data from domain objects, the ViewModel object's sole reason for existence is to separate domain logic from presentation, just as Fowler observes.

The resulting arrangement makes it much simpler to focus on layout and user interaction in the View, without being concerned about how that interaction might pollute the domain objects and their logic, or vice versa. It is the layer of separation that Fowler describes.

See Also
The View Model Pattern
Isn't MVC Anti-OOP?

Glorfindel
  • 3,137
  • 6
  • 25
  • 33
Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • There's nothing wrong with a `Validator`, a `Calculator` or a `CrookedBusinessRule`. Ultimately it's a question of reusability. Putting too much into the domain object for scenario A may make it look like a kitchen sink in scenario B. The domain object imho would first and foremost offer only methods that are absolutely necessary for keeping its state valid in the most basic scenario, and because they have to access its internal state. – herzmeister Aug 14 '13 at 16:37
  • 1
    Does a True MVC pattern have a view model? I know in some projects I have done I have created a view model, but in a pure MVC implementation (if there is such a thing) would you have one? – SoftwareSavant Aug 14 '13 at 20:45
  • 1
    @DmainEvent: It could be argued that View Models make MVC more pure by improving separation of concerns. – Robert Harvey Aug 14 '13 at 20:50
  • 1
    It's interesting that concepts that are used in some technology are not used in another one, for example the View Model you talk about is not known in Java (I've never heard it before either) and in Java, frameworks advice to bind directly to the domain objects. I've been reading about this in .NET (ASP.NET MVC) and the opinion is the total opposite, they say that you shouldn't bind directly to the domain objects and create what you said "view models". Very interesting how the opinions change depending on the technology. – Alfredo Osorio Sep 06 '13 at 14:00
  • The problem with view model is that you have to duplicate a lot of fields, and that's a lot of maintenance but the counter argument is that it is worth because if the model changes you don't have to update the views and also gives you more security ensuring that the view can't update fields that are not supposed to be updated in that view. – Alfredo Osorio Sep 06 '13 at 14:11
  • Anyway, even with this you still have to duplicate validation in the view model and the domain objects, suppose that you have a business rule that says that "Member" can only be age > 18, if you had used View Model you would have a field "age" in the View Model and also in the domain object, then you would have also the same validation in both the domain and the view model, that's a lot of duplication and if the business rule changes you would have to update all the view models that contain that validation. – Alfredo Osorio Sep 06 '13 at 14:12
  • @AlfredoOsorio you do have viewmodel in java, (it's called simply model) in swing, check out: http://docs.oracle.com/javase/tutorial/uiswing/components/model.html – Dan Dec 17 '14 at 00:16
  • @AlfredoOsorio I know this is old, but I'll still give my opinion anyway. First, I think in .Net people do advocate not to bind the view to the model. I think that's still personally for all the reasons you state. What I would use a VM for is for any glue needed between the view and the model which doesn't belong in either. But my VMs always have a Model property which the view can bind to directly as well. An example of something that might be on the VM is the list of choices for a dropdown. The selected item binds to a property on the model, but the model – Andy Jul 15 '17 at 18:02
  • ... isn't responsible for getting the list of possible choices itself; that's another model, and the VM brings together the model and other support models as needed, and presents them to the view for use. – Andy Jul 15 '17 at 18:02
0

Any web framework that covers both the front and back end has a fundamental problem. You can't use the same objects on both sides. They have to be serialised to send over the wire and you may not be using the same language on either side.

This forces you to have anaemic objects because you can send the fields, but not methods of an object over the wire.

Arguably this makes it an ADM approach.

However. You can imagine the scenario where you have a controller which instantiates OOP style models, calls their methods to perform logical operations and returns a view.

ie.

WeatherController
{
    GetWeather(string day)
    {
        var d = new Day(day)
        d.CalculateWeather();
        return new WeatherView(d.ForecastData);
    }
}

This is very OOP, Day contains the data and logic, where as an ADM approach would be

WeatherController
{
    GetWeather(Day day) //Day has no methods so we can directly bind it to the incoming data
    {
        var forecaster = new WeatherForcaster();
        var data = forecaster.CalculateWeather(d)
        return new WeatherView(data);
    }
}

Here Day is an anaemic class or struct and the logic is separated out into its own WeatherForecaster object.

Where as in the first approach I might subclass Day to produce different results, in the ADM approach I would replace the forecaster object with an alternate.

Is the automatic binding of the form data to Day, vs passing a string and instancing the model manually the key difference in these approaches? I would argue that it is not.

Ewan
  • 70,664
  • 5
  • 76
  • 161