C# – ASP.NET Web API and MVC Architecture for Web Application

asp.netasp.net-mvcasp.net-mvc-web-apicn-tier

enter image description here

We have a single solution Visual Studio web application with multiple projects.

One of the projects (Services project) has APIs for our App clients (Android/iOS). There's separate project for MVC application that powers our Desktop and Mobile website. There's one project for DTO that has POCO classes for request/response objects. One project is for Entities that has all our business Entities.

The ViewModel classes are saved in the MVC/UI project itself. The Adapter classes (to convert DTO to entities and entities to DTO) are in services project and in MVC/UI project respectively.

For Android/iOS the request-response looks like: Request parameters come to the services project and are mapped as DTO through service's project adapters, this DTO is converted to Entity and passed to BL, DAL, DB/MicroServices. The response comes back to the services project as Entity and gets mapped to a DTO through Adapter classes. This DTO is passed as response to the clients.

For website (mobile & desktop) the request-response looks like: Request parameters come to the MVC/UI project and are mapped as DTO through MVC/UI project adapters, this DTO is converted to Entity and passed to BL, DAL, DB/MicroServices. The response comes back to the MVC/UI project as Entity and gets mapped to a ViewModel through Adapter classes. This ViewModel is passed to the view which generates the HTML and passes HTML to the clients.

The primary difference between DTO and ViewModel we have is that DTO are flat objects while ViewModel might have nested objects too. Also, ViewModel might have composite fields i.e. fields like FullName (combination of FirstName and LastName) while DTO will only have simple properties like FirstName, LastName. The reason is that we want to give flexibility to Apps i.e. they might want to show FirstName in a different font as compared to LastName.

There are few queries we have regarding the above architecture?

  1. Should we have different projects for Web API and MVC/UI?
  2. Should we have ViewModel only for MVC applications and return plain DTO to the Apps and let them build ViewModel for their UI?
  3. Is it correct to accept only DTO from the clients and return back only DTO/HTML to them? Is it necessary to convert DTO to entity for each request and vice-versa for each response?
  4. The adapter classes (to convert DTO to entity and vice-versa), it should be in a separate project OR same project?

Best Answer

It looks like you are missing a service layer between you business logic and your websites.

If you draw in a horizontal slice on your diagram for it you can see immediately that you have a choice of whether to:

  • Move you existing "Services Project" into that layer and have the Website and Mobile Clients both consume it.

  • OR to create a new project comprising of the orchestration of you Business Objects which two slimmed down WebSite/WebAPI projects consume.

I wouldn't have more projects for the adaptors myself, as I would see that as the main role of the WebSite/WebApi after having moved any non view related logic to the service layer.

Is it correct to accept only DTO from the clients and return back only DTO/HTML to them? Is it necessary to convert DTO to entity for each request and vice-versa for each response?

You don't have much choice. You have to serialise the Entity, whatever you serialise it to is the DTO or ViewModel.

Your only question is really how similar the ViewModel/DTO is to the Entity. Usually the ViewModel/DTO simplifies the work of the View/Client by adding fields, flattening data etc as you say. But the simplest way is sometimes just to serialise the Entity as it is.