C#C
C#3mo ago
Yarden

✅ Login & Register in asp.net

A month ago I created a basic register & logic classes + pages for my future project.
I'm not sure what I did, but maybe I've got a guide here and with AI of how creating it, and I was told to create them with records instead of classes (I don't remember why).

Should I create with records similar to this?
    public record RegisterViewModel(

    [Required, StringLength(30, MinimumLength = 1)]
    [RegularExpression("^[a-zA-Z0-9]+$")]
    string UserName,

    [Required, EmailAddress, StringLength(254)]
    string Email,

    [Required, DataType(DataType.Date)]
    DateOnly? DOB,

    [Required, StringLength(100, MinimumLength = 8)]
    string Password,
    [Required, StringLength(100, MinimumLength = 8)]
    string ConfirmPassword,

    ...

    Gender Gender
     );

or with classes similar to this?
using System.ComponentModel.DataAnnotations;

namespace ASPNETCoreIdentityDemo.ViewModels
{
    public class RegisterViewModel
    {
        [Required]
        [Display(Name = "First Name")]
        [StringLength(50, ErrorMessage = "First Name cannot be longer than 50 characters.")]
        public string FirstName { get; set; } = null!;

        [Display(Name = "Last Name")]
        [StringLength(50, ErrorMessage = "Last Name cannot be longer than 50 characters.")]
        public string? LastName { get; set; }

        [Required(ErrorMessage ="Email Id is Required")]
        [EmailAddress(ErrorMessage = "Invalid Email Address")]
        public string Email { get; set; } = null!;

        [DataType(DataType.Date)]
        [Display(Name = "Date of Birth")]
        public DateTime? DateOfBirth { get; set; }

       ... no more space for code...
    }
}


What are the advantages and disadvantages? What I need to be alert of?
I'm trying not to use AI anymore, because it doesn't teach me well + it generated bad code sometimes.
Thanks!
Was this page helpful?