0

Apologies if this has already been answered, but I am looking for the best practice when dealing with the below scenario.

I have a fairly large MVC5 application which will have numerous forms/pages.

For this example say I have a patient class like the below (this a stripped down version of a larger class)

    Public Class Patient {

    [Display(Name = "Patient Trial Number")]
    public int ID { get; set; }

    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Display(Name = "Patient DOB")]
    public DateTime PatientDOB { get; set; }

    [Required]
    [Display(Name = "Patient Gender")]
    public Gender PatientGender { get; set; }

    } 

I have various models for each form that needs to be completed. Some of these are very large 80+ properties.

Below is a stripped down example of a form model.

public class ExamplePatientForm

    {
        [Key]
        public Patient PatientID { get; set; }

        [Required]
        [Display(Name = "Was Sample One taken?")]
        public bool? SampleOneTaken{ get; set; }

        [Required]
        [Display(Name = "Date Sample One Taken")]
        public DateTime DateSampleOneTaken { get; set; }

        [Required]
        [Display(Name = "Was Sample Two taken?")]
        public bool? SampleTwoTaken{ get; set; }

        [Required]
        [Display(Name = "Date Sample Two Taken")]
        public DateTime DateSampleTwoTaken { get; set; }

        public int PatientRating {get;set;}

        public string Comments { get; set; }


    }

In reality, both the patient class and individual form classes are a lot larger.

Initially I was using html.HiddenFor to hold the details of the Patient class within the individual forms though this didn't feel right.

I then created ViewModels (see below)

        [Key]
        public int PatientID { get; set; }

        [Required]
        [Display(Name = "Was Sample One taken?")]
        public bool? SampleOneTaken{ get; set; }

        [Required]
        [Display(Name = "Date Sample One Taken")]
        public DateTime DateSampleOneTaken { get; set; }

        [Required]
        [Display(Name = "Was Sample Two taken?")]
        public bool? SampleTwoTaken{ get; set; }

        [Required]
        [Display(Name = "Date Sample Two Taken")]
        public DateTime DateSampleTwoTaken { get; set; }

        public int PatientRating {get;set;}

        public string Comments { get; set; }


    }

The ViewModels removed the relationship and stored the PatientID as an Integer. I then mapped this back to the entity model in my controller.

Though having to duplicate each form model to create a view model seems a but crazy to me, especially as some contain 80+ properties.

Does anyone know the best way to approach this? I will have around 50 unique forms.

Also I have created an editor template for the patient model so I can display some of the patient information at the top of each form. But only need to display certain properties so not sure if I need to create a separate PatientViewModel for this or just hide the other elements.

Hope this makes sense. Any help would be greatly appreciated.

Regards, Rob

Robert Kitching
  • 190
  • 1
  • 2
  • 10
  • One thing you could do is Divide your VERY LARGE Model into small View Models, and then use a Wizard to Create/Update a Patient. – Dawood Awan Sep 14 '15 at 11:31
  • And to display the information you could use Tabs - Display the common information on the Top and other information in Tabs. - Each tab can have it's own view model load data using Ajax. – Dawood Awan Sep 14 '15 at 11:58

3 Answers3

2

Using viewmodel is obviously a best approach, and you have to add all these 80+ properties in your viewmodel.You should not use properties direclty from your domain model.

Also your problem seems to be a UI design issue rather than a programming desing, so try to consider changing your UI design as other have suggested. You can surely divide the form into different forms.

Also have a look at these two links they will hopefully helpful for your concerns
Chris Pratt
Stackoverflow

Community
  • 1
  • 1
Dragon
  • 1,078
  • 2
  • 9
  • 31
1

In reality, no single form should probably be using all 80 properties at once. If they are then I would argue the form should be broken up into multiple screens.

Having said that, the ViewModel/InputModel approach is definitely best practice. It allows you to have all of the Patient properties as well as any supplemental information you need, such as lists of items for dropdown menus. You could use a tool like Automapper to do the mapping for you or you could just do it by hand, it's really up to you and depends how closely the ViewModel and Domain Model match. I typically just do a Select from Entity Framework directly to my ViewModel.

Keith Rousseau
  • 4,435
  • 1
  • 22
  • 28
1

I would strongly advice against using your models (entities) as your view models, since these are the models that connects directly to your ORM, e.g. though Entity Framework. This could mean that if someone is sneaky enough, they can update properties they might not should have access to (though POST or GET requests). Your domain models should also be very thin with as less attributes as possible - no DisplayName(...) and such.

If your view models are very large, you should split them up and reuse them as properties on smaller view models. Maybe even create a factory for building them.

If need to map them one-to-one tools like AutoMapper can be handy, but I would recommend to do the mapping by hand or using a simple self-made mapper.

janhartmann
  • 14,713
  • 15
  • 82
  • 138