实体框架类与POCO [英] Entity Framework classes vs. POCO

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

问题描述

我有意见,对建筑设计的一般区别,即使计算器不应被用来征求意见我想问一下这两个方法的优点和缺点,我将在下面描述:

I have a general difference of opinion on an architectural design and even though stackoverflow should not be used to ask for opinions I would like to ask for pros and cons of both approaches that I will describe below:

详细信息:
- C#应用程序
- SQL Server数据库
- 使用实体框架
- 我们需要决定哪些对象,我们将用它来存储我们的信息,并使用所有的整个应用程序

Details: - C# application - SQL Server database - Using Entity Framework - And we need to decide what objects we are going to use to store our information and use all throughout the application

方案1:
我们将使用实体框架的实体,通过我们的应用程序都通过周围,例如对象应该被用来存储所有信息,我们围绕它传递给了BL,最终我们WepApi将以此为实体和返回值。没有DTO的,也不波苏斯。

Scenario 1: We will use the Entity Framework entities to pass all around through our application, for example the object should be used to store all information, we pass it around to the BL and eventually our WepApi will take this entity and return the value. No DTOs nor POCOs.

如果数据库模式的变化,我们更新了实体,在其被使用的所有类修改。

If the database schema changes, we update the entity and modify in all classes where it is used.

方案2:
我们创建了一个中间类 - 称之为DTO或称之为POCO - 持有由应用程序所需的所有信息。有服用存储在实体和填充到POCO的信息的一个中间步骤,但我们把所有的EF code中的数据访问中,而不是在所有图层。

Scenario 2: We create an intermediate class - call it a DTO or call it a POCO - to hold all information that is required by the application. There is an intermediate step of taking the information stored in the entity and populated into the POCO but we keep all EF code within the data access and not across all layers.

什么是每个人的优点和缺点是什么?

What are the pros and cons of each one?

推荐答案

我会用中级班,即POCO而不是EF实体。

I would use intermediate classes, i.e. POCO instead of EF entities.

我看到直接使用EF实体唯一的好处是,它不太code写...

The only advantage I see to directly use EF entities is that it's less code to write...

优点使用POCO来代替:

Advantages to use POCO instead:

基本上,说你有一些 GetUsers 业务方法。如果你只是想用户列表来填充一个网格(即你需要他们的ID,姓名,首先例如名字),你可以只写这样的事情:

Basically, say you have some GetUsers business method. If you just want the list of users to populate a grid (i.e. you need their ID, name, first name for example), you could just write something like that:

public IEnumerable<SimpleUser> GetUsers()
{
    return this.DbContext
        .Users
        .Select(z => new SimpleUser
        {
            ID = z.ID,
            Name = z.Name,
            FirstName = z.FirstName
        })
        .ToList();
}

有晶莹剔透你的方法实际上返回。
现在想象相反,它返回一个完整的用户实体与所有你不希望暴露(如密码字段)...

It is crystal clear what your method actually returns. Now imagine instead, it returned a full User entity with all the navigation properties and internal stuff you do not want to expose (such as the Password field)...

这是更为明显的创建类似的业务方法。你肯定不希望使用用户实体参数,这将是非常复杂的,对消费者的服务,要知道的什么的属性实际需要的...

It's even more obvious for Create like business methods. You certainly don't want to use a User entity as parameter, it would be awfully complicated for the consumers of your service to know what properties are actually required...

想象一下下面的实体:

public class User
{
    public long ID { get; set; }
    public string Name { get; set; }
    public string FirstName { get; set; }
    public string Password { get; set; }
    public bool IsDeleted { get; set; }
    public bool IsActive { get; set; }
    public virtual ICollection<Profile> Profiles { get; set; }
    public virtual ICollection<UserEvent> Events { get; set; }
}

为你消耗 void创建(用户单位)哪些属性是必需的;


  • ID:不知道,也许它可能产生这不是

  • 名称/姓:好这些应设置

  • 密码:是一个纯文本的密码,一个散列版本?是什么呢?

  • 请将isDeleted / IsActive:我应该激活用户自己?是由商业方法呢?

  • 概况:?呜呜......我怎么影响一个配置文件,用户

  • 活动:到底是??

是的,我的的这个功能有多种原因。其中一些是:

Yes, I hate this feature for multiple reasons. Some of them are:


  • 非常难以有效地使用。我见过太多的时间code,产生数以千计的SQL请求,因为开发商不知道如何正确使用延迟加载

  • 极难管理例外。通过允许在任何时间被执行的SQL请求(即,当你懒负荷),则委托管理数据库例外的上部层的作用,即,业务层或甚至该应用程序。坏习惯。

使用POCO迫使你渴望加载您的实体,更好海事组织。

Using POCO forces you to eager-load your entities, much better IMO.

AutoMapper 是一个工具,允许你,自动将实体转化为波苏斯和副反之亦然等。我也不喜欢它。请参见 http://stackoverflow.com/a/32459232/870604

AutoMapper is a tool that allows you to automagically convert Entities to POCOs and vice et versa. I do not like it either. See http://stackoverflow.com/a/32459232/870604

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

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