Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POCO classes and ViewModels in MVC3

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,

like image 477
user2394196 Avatar asked Dec 16 '22 10:12

user2394196


2 Answers

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.

like image 171
mattytommo Avatar answered Dec 29 '22 16:12

mattytommo


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.

like image 43
mt_serg Avatar answered Dec 29 '22 14:12

mt_serg