POCO classes and ViewModels in MVC3

2019-05-12 01:24发布

问题:

I am not an experienced MVC3 developer but I'm trying to be. I am familiar with POCO classes and also ViewModels, as the former describes each classes of the database and the latter is used for strong type views in mvc3. My question is not that complicated for the experienced developers but I am a little confused about that.

The matter is that, I have a solution containing three projects;

  1. The Model class library in which I have wrote my POCO classes. Here is an example:

.

public class Service
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int ServiceID { get; set; }
        //------------------------------------------------------------//
        [Required, MaxLength(30)]
        [LocalizedAttribute("Name")]
        public string Name { get; set; }
        //------------------------------------------------------------//
        [MaxLength(100)]
        [LocalizedAttribute("Description")]
        public string Description { get; set; }
        //------------------------------------------------------------//
        [Required]
        public long ModifiedByUserID { get; set; }
        [ForeignKey("ModifiedByUserID")]
        public virtual User OperatorUser { get; set; }
        //------------------------------------------------------------//
        [Required, MaxLength(10)]
        public int ModifiedDate { get; set; }
    }
  1. The repository and UnitOf Work class Library

  2. The MVC application

Now, Did I correctly address the POCO classes? (I am using EF Code First to generate the database of course) If So, are they inferred as ViewModels too? I have used them to generate Strongly-Type View.

What is the best and actually standard way to define POCO classes and ViewModels?

I would appreciate any kind guidance,

回答1:

To be honest, it depends on the size of your project.

If you look at most of the Microsoft examples, they use their POCOs as Models simply because their examples are small projects.

If however you are developing anything near an enterprise level application you really shouldn't be using your POCO's as models. There should be clear separation of concerns. Strictly speaking your Web project shouldn't even know about your POCO objects in those scenarios, a typical implementation is a common interface that both the POCO and the View Model can implement and see. That way saves you exposing your POCO objects to your Web layer.



回答2:

ViewModel is a middle layer between data(Poco) and View, which usually contains additional logic to control UI.

If ViewModel doesn't have any specific data, I don't see the reasons not to use Poco as ViewModel.

In other case, to keep data as Poco, you can create ViewModel with the same fields as your Poco class and use Automapper to Poco->ViewModel, ViewModel->Poco transformation.



回答3:

I agree with mt_serg. In my application, I use POCO classes directly if it is a straightforward case. However if in my view I also need to display for example drop-down lists that are populated from the db, then I create a ViewModel that includes the POCO classes with the additional lists and use the VM in the view passed in from the controller. I don't however redo my work and create the VM with the same fields as the POCO + additional fields. I find this method works for me, since I do not have to take care of transformations myself and I let the MVC framework sort that out for me. Hope this helps