Is it good practice to create a LoginViewModel as a struct instead of a class?

46 Views Asked by At

i want to ask if i want to create a LoginViewModel that just contains :

LoginViewModel {
     string UserName;
     string Password;
}

is it good practice to create it as a struct, because its a ValueType and lightweight, or should I create it as a class? I have the same question for RegisterViewModel too.

I have doubts about choosing class or struct type for this data type and I'm not sure about the performance of struct since it should pass a copy when sending it in methods

1

There are 1 best solutions below

0
Luke On

In ASP.NET, ViewModels are conventionally defined in classes, not structs. This isn't necessarily for performance reasons (the difference is almost certainly negligable in most cases, when discussing ViewModels), but because often you'll want constructors in your ViewModels, etc. It doesn't make sense to limit yourself by using structs.

When in doubt, do what Microsoft does (this isn't always good advice, but in most contexts including this one, it is): https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-7.0#strongly-typed-data-viewmodel