.NET REST – Combining Single Page Application and DDD with a REST API

domain-driven-designnetrest

I'm creating a web application which on the client side consists of a Single Page Application (with Durandal) and on the server side an ASP.NET MVC site with a Domain Driven Design. The two are connected with a REST Api (Web API).

I have a relation between two entities, which on the database level is many-to-many. (Hotels <=> RoomTypes). Since in DDD a best practice is not to have many-to-many relationships I built it so that it can only be navigated from the roomtype to the hotel (since a hotel can exist without roomtypes, but a roomtype should always be associated with a hotel). The relation itself also has attributes (availability for example).

My current issue is how to associate a hotel with a roomtype however. Following DDD practices I would add a method AddHotel to the RoomType-class.
The problem is that in the SPA, you can edit all the attributes of a roomtype (pictures, description, …) and associated hotels in a single view. After editing these attributes, an update is submitted via the rest api. This means that everything gets submitted in one call to the server.

How would you structure the server and the rest based API so that:

  • the domain stays in place and there's correct validation inside the RoomType-class
  • you can post atomic updates from the single page application through the rest
  • there's no code duplication on the server and the single page application

Best Answer

You seem to hint at your own answer: don't submit everything in one go. If you think about it, what kind of actions would you like to be able to do in your view? One of them is apparently assigning hotels to room types. So for every action you want to perform in your view you either have a method you call on a endpoint controller, or if you want to work with commands, you'd form a AssignHotelToRoomTypeCommand with all the necessary data for executing that procedure, and send that command to an endpoint.
The more I think about it though, assigning a hotel to a room type seems a bit weird/backwards to me... Is there a reason you are modelling a room type as an entity and not a value object?