Voltstriker
Voltstriker

Reputation: 121

Should I define class properties into an ASP.NET Core ViewModel

I am creating a small web application that allows users to create a project, before then being given the option to deploy that project onto a file system. Within my view, I am successfully displaying the data that is obtained from a list of my defined viewmodel ProjectsViewModel. A list of projects are queried from the database and populated into the Project related properties, before then checking if any of these projects have a deployment. If so, the Server.HostName and Deployment.FilePath values are obtained and populated into the appropriate properties.

In my view, a button is displayed if a project has not yet been deployed onto the file system - this button launches a modal. This modal dialog contains a form where users can select the server and the folder path to be deployed onto. Details about the project are also displayed in this dialog, being passed via an ajax call and populated into a single instance of ProjectsViewModel.

Now that I have a basic working setup, I am unsure whether I have structured my data models correctly to best support the workflow.

Should I be defining things like FilePath and Hostname as individual properties within my view model, where I would then populate them into a newly initialised Deployment object in my controller HttpPost action? Or would it be better practice to define a direct Deployment property within my viewmodel and access these values that way?

My models/viewmodels are as follows:

Projects

public partial class Project
{
    public int ID { get; set; }
    public string Name { get; set; };
    public string? Description { get; set; }
    public DateTime CreatedOn { get; set; }
}

Deployments

public partial class Deployment
{
    public int ID { get; set; }
    public int ServerID { get; set; }
    public int ProjectID { get; set; }
    public DateTime DeployedOn { get; set; }
}

Servers

public partial class Server
{
    public int ID { get; set; }
    public string Hostname { get; set; };
}

ProjectsViewModel

public class ProjectsViewModel
{
    [Key]
    [HiddenInput]
    public required int ProjectID { get; set; }

    [DisplayName("Project Name")]
    [StringLength(255)]
    public required string ProjectName { get; set; }

    [DisplayName("Description")]
    [StringLength(1024)]
    public string? ProjectDesc { get; set; }

    [DisplayName("Server")]
    [StringLength(255)]
    public string? Hostname { get; set; }

    [DisplayName("Folder Path")]
    [StringLength(255)]
    public string? FolderPath { get; set; }

    [DisplayName("Created On")]
    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm tt}", ApplyFormatInEditMode = true)]
    public required DateTime? CreatedOn { get; set; }
}

Upvotes: 0

Views: 16

Answers (0)

Related Questions