MVC critics and best practices

While MVC remains the go-to approach for designing a UI, many criticisms arose with its prevalence. Most critics are actually pointing a finger at the incorrect use of the pattern.

Anemic Domain Model

Eric Evans' influential book Domain Driven Design, also abbreviated as DDD, defines a set of architecture rules leading to a better integration of the business domain inside the code.

One of the core ideas is to take advantage of the object-oriented paradigms inside the domain objects. Going against this principle is sometimes referred to as Anemic Domain Model. A good definition of this problem can be found on Martin Fowler's blog (http://www.martinfowler.com/bliki/AnemicDomainModel.html).

An Anemic Model typically exhibits the following symptoms:

  • The model is constituted from very simple plain old Java objects (POJOs) with only getters and setters
  • All the business logic is handled inside a service layer
  • Validation of the model is found outside this model, for instance, in controllers

This can be a bad practice depending on the complexity of your business domain. Generally speaking, DDD practices require additional efforts to isolate the domain from the application logic.

Architecture is always a tradeoff. It is good to note that typical ways of designing a Spring application can lead to complicated maintenance somewhere along the road.

How to avoid domain anemia is explained here:

  • The Service layer is good for application-level abstraction like transaction handling, not business logic.
  • Your domain should always be in a valid state. Leave validation inside the form objects using validators or JSR-303's validation annotations.
  • translate the inputs into meaningful domain objects.
  • Think of your data layer in term of repositories with domain queries (refer to Spring Data Specification, for example)
  • Decouple your domain logic from the underlying persistence framework
  • Use real objects as much as possible. For instance, manipulate the FirstName class rather than a string.

There is much more to DDD than these simple rules: Entities, value types, Ubiquitous Language, Bounded Context, Onion Architecture, and anti corruption layers. I strongly encourage you to study these principles on your own. As far as we are concerned, with this book we will try to keep in mind the guidelines listed earlier as we craft our web application. These concerns will become more familiar to you as we advance through this book.

Learning from the sources

If you're familiar with Spring, you have probably already landed on Spring's website, http://spring.io. It is entirely made with Spring and the good news is that it is open source.

The code name of the project is sagan. It has numerous interesting features:

  • A gradle multimodule project
  • Security integration
  • Github integration
  • Elasticsearch integration
  • A JavaScript frontend application

The GitHub wiki associated with the project is really detailed and will help you get started easily with the project.

Note

Visit the following URL if you're interested in the Spring's architecture of a real world application:

https://github.com/spring-io/sagan

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.20.231