Search code examples
c#asp.net-mvcarchitectureclean-architectureardalis-cleanarchitecture

Clean Architecture - In ASP.NET MVC - Which layer I use to write the ViewModels?


The question is very simple,

I am using Clean Architecture for a project(web site) by using ASP.NET MVC. I download the Clean Architecture template sample code written by Ardalis from GitHub.

In that specific template, the writer didn't use the viewmodel.

Does anybody have any reference for a template using Clean Architecture and viewmodel for ASP.NET MVC etc or know where to put(layer) the view model.


Solution

  • I'm the author of the CleanArchitecture template in question

    The latest version uses ViewModel classes with Views as one of several options (the others being Razor Pages, APIs with Controllers, and APIs with API Endpoints). Typically when using ViewModel classes the usual convention is to put them into a ViewModels folder in the root of the Web project.

    This puts them near the Views folder so they are fairly easy to find (and for API Models used with API Controllers, frequently these are placed into an ApiModels folder that sorts near the API and/or Controllers folders typically used in these cases).

    It's worth mentioning that this problem goes away if you use Razor Pages, because the model is linked to the "view" (the Page) in the approach.

    Likewise for APIs, if you adopt the API Endpoints approach (another open-source package of which I am also the author), the Request and Response models used for each endpoint are typically linked to the Endpoint class there as well.