1

I created an ASP.NET Core Web API project, whose template is tailored for RESTful services. In my DB instance, I have a SQL table called Organizations and a view based on that table called Organizations_v. In the .NET project, I created a model under the Models folder called Organization. The model will be backed by the SQL view.

As this an API, I don't really have any pages/views. It's not a true ASP.NET Core MVC application in that sense, right? But convention says if your model is backed by multiple tables, the model class name should end with ViewModel.

What's the proper naming convention for the model class for my use case? Since I'm using a SQL view to back the model, should I name the model class OrganizationViewModel or Organization?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3621633
  • 1,681
  • 3
  • 32
  • 46
  • 1
    A [View Model](https://stackoverflow.com/q/11064316/2030565) is simply a "model" used by the page View. – Jasen Apr 04 '22 at 21:04
  • 1
    In the past, I have simply named the entity model the same as the database object (table or view). – Jasen Apr 04 '22 at 21:05

1 Answers1

2

The following is a pretty standard naming convention I've both used and consumed:

Using your example of Organizations:

Organization - represents your business entity: the entity you are pulling out of the db.

OrganizationModel - represents the entity you expose via your api.

It is crucial to have this separation from the very beginning. The fallout from not having this separation will haunt you, trust me. For example, if you have to change the entity (and you will) you are forced to release a new version of your api.

Shai Cohen
  • 6,074
  • 4
  • 31
  • 54
  • That makes too much sense. So, instead of naming the entity exposed to the API `OrganizationDTO`, in your example, you named it `OrganizationModel`. And since there's no concept of page or view in the API template, we wouldn't use the `ViewModel` naming convention. – user3621633 Apr 05 '22 at 14:35