model-view-controllerdomain-driven-designlayerapplication-layer

Layered architecture mvc


I'm creating a web app using an MVC framework. I thought of adding a layer between the controller and the domain models, I think it's called application layer in DDD, to avoid putting logic that is specific to a certain use case in the domain models. The controller will only interact with this layer and this layer will orchestrate the operation using the domain models. This layer will be kept as thin as possible pushing all the logic that is not use case specific to the domain model. I will call the classes that belong to this layer DomainCtrl.

Example login scenario: Model: LoginForm DomainCtrl: AuthCtrl UI: ui controller

1.ui controller receives request 2.creates instance of AuthCtrl 3.AuthCtrl creates instance of a LoginForm and fill it with request data passed to authCtrl 4.LoginForm performs the login 5.authCtrl does other things that are specific to this specific way of login -> returns errors to ui controller

Is this a good way to organize an app?


Solution

  • Your question

    Is this a good way to structure an app

    is a very loaded question. The simple answer is Yes, but the more correct answer is It depends.

    Let's start with the simple answer. Having your main application be unaware of the UI is generally a good idea. It means that you can easily consume your application from various places. In DDD this outer layer is usually called Application Layer. It is mainly responsible for orchestrating interactions between your domain, persistence and other resources that you might rely on. This also allows you to have your domain at the center unaware of everything else. This makes your domain easily testable and maintainable if implemented well.

    Now the "it depends" part of the answer. DDD is not the only successful way to build an application, and in some cases it might be more of a hinderance than anything else. You have to ask yourself what is my app doing. Are there many domain specific rules? Am I only fetching and storing basic data etc? These are all questions you need to answer before you choose an architecture and technologies.

    I would still say you probably won't go wrong by choosing the DDD approach as it is generally a good way to do things.

    *Note: Your example is not that clear to me but you should be careful of spilling UI concepts into your domain/application. A login form is completely a UI concept as is auth to a certain extent. You can probably have your application return the details of your user, but the UI layer should decide if the user is allowed to proceed or not.