POCO类和MVC3的ViewModels [英] POCO classes and ViewModels in MVC3

查看:106
本文介绍了POCO类和MVC3的ViewModels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个有经验的开发人员MVC3,但我想是。我所熟悉的POCO类和也的ViewModels,因为前者描述了数据库的每一类,后者用于MVC3强类型的意见。我的问题是不是有经验的开发人员复杂,但我有点困惑了。

这个问题是这样的,我有一个包含三个项目的解决方案;


  1. Model类的库中,我已经写了我的POCO类。下面是一个例子:

 公共类服务
    {
        [键]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)
        公众诠释服务ID {搞定;组; }
        // ------------------------------------------------ ------------ //
        [必需的,最大长度(30)]
        [LocalizedAttribute(姓名)]
        公共字符串名称{;组; }
        // ------------------------------------------------ ------------ //
        [MAXLENGTH(100)
        [LocalizedAttribute(描述)]
        公共字符串描述{搞定;组; }
        // ------------------------------------------------ ------------ //
        [需要]
        众长ModifiedByUserID {搞定;组; }
        [ForeignKey的(ModifiedByUserID)]
        公共虚拟用户OperatorUser {搞定;组; }
        // ------------------------------------------------ ------------ //
        [必需的,最大长度(10)]
        公众诠释ModifiedDate {搞定;组; }
    }


  1. 信息库和UnitOf工作类库


  2. 的MVC应用程序


现在,我有没有正确地解决POCO类? (我用EF code首先生成过程的数据库)。如果是这样,他们也推断为的ViewModels?我用它们来生成强类型视图。

什么是定义POCO类和的ViewModels最好的和实际标准的方式?

我想AP preciate任何指导,


解决方案

说实话,这取决于你的项目的大小。

如果你看看大多数的微软的例子,他们用自己的波苏斯作为模型仅仅是因为他们的榜样是小项目。

然而,如果你正在开发你附近的企业级应用什么真正不应该使用POCO作为车型。应该有关注明确分​​离。严格地说您的Web项目甚至不应该知道这些场景你的POCO的对象,典型的实现是,无论是POCO和视图模型可以实现,看到一个共同的接口。这样可以节省你暴露你的POCO对象到Web层。

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,

解决方案

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.

这篇关于POCO类和MVC3的ViewModels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆