转换POCO实体业务实体 [英] converting POCO entity to business entity

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

问题描述

我愿意到实体框架结合起来,作为我的数据层。

I am willing to integrate the entity framework as my data layer.

我跟文章和产生的使用本教程POCO实体:<一href="http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx" rel="nofollow">http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx

I followed articles and generated poco entities using this tutorial: http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx

我有我自己的业务对象。这是我的目标BRACH:

I have my own business objects. Here is my business object Brach:

public class Branch
{
    public long BranchId { get; private set; }
    public string BranchName { get; set; }
    public string BranchCode { get; set; }

    public Branch() { }

    public void InsertBranch(Guid companyId)
    {
        using (var ctx = new Entities.Entities())
        {
            var branch = new T_STF_BRANCH() //This is generated POCO object
            {
                company_id = companyId,
                branch_name = BranchName,
                branch_code = BranchCode
            };
            ctx.T_STF_BRANCH.AddObject(branch);
            ctx.SaveChanges();
        }
    }

    public static IList<Branch> GetBranchesList(Guid companyId, long? branchId,
        string branchName)
    {
        using (var ctx = new Entities.Entities())
        {
            var branchs = ctx.T_STF_BRANCH.Where(x =>
                x.is_deleted == false &&
                (branchId == null || x.branch_id == branchId) &&
                (branchName == null || x.branch_name.Contains(branchName))).ToList();
        }
        //HERE I NEED SOMEHOW CONVERT THE POCO ENTITIES INTO MY BUSINESS ENTITIES...
    }
}

我不知道如何将POCO实体转化为我的业务实体。
我应该在哪里放置转换,从POCO和POCO?

I don't know how to convert the POCO entity into my business entity.
Where should I put the conversion from POCO and to POCO?

推荐答案

恕我直言,这太复杂了。为什么你有恒心和单独的对象POCO实体与加载到POCO实体数据的工作?听起来像是你的应用程序已经结束architectured。

IMHO this is too complicated. Why do you have POCO entity for persistence and separate object for working with data loaded into POCO entity? Sounds like your application is over architectured.

ORM手段对象关系映射。这意味着相对于世界和客观世界之间的映射。通常它也可以翻译为数据库和业务对象之间的映射。所以,你应该用你的POCO对象为你的业务对象。即使用波苏斯的全部意义。如果你不想使用它们作为业务对象不需要他们,你可以使用默认的实体对象直接。

ORM means object relational mapping. It means mapping between relation world and object world. Usually it can be also translated as mapping between database and your business objects. So you should use your POCO objects as your business objects. That is the whole meaning of using POCOs. If you don't want to use them as business objects you don't need them and you can use default entity objects directly.

如果你想使用波苏斯作为业务对象只是让EF生成这些波苏斯为您和增加部分类到每个POCO定义你的方法。

If you want to use POCOs as business object simply let EF generate those POCOs for you and add partial class to each POCO defining your methods.

顺便说一句。业务对象实际上看起来像实施活动记录模式。如果你想用这个模式也许你应该检查温莎活动记录这是基于前的NHibernate的。

Btw. your business object actually looks like implementation of Active Record pattern. If you want to use this patterns perhaps you should check Windsor Active Record which is based on top of NHibernate.

编辑:

嘛。您可以使用生成的POCO实体,而不是你的类。

Well. You can use your classes instead of generated POCO entities.

一种方法是放弃与EFv4和EDMX,并检查新的 EFv4.1和新的流畅API (又名code-第一)的映射。这是整个单独的问题,或简单地使用搜索这里左右。

One way is to give up with EFv4 and EDMX and check new EFv4.1 and its new fluent API (aka code-first) for mapping. This is whole for separate question or simply use search here on SO.

您可以做到这一点EDMX为好。还有,你必须遵循,使这项工作,因为这全是通过命名约定做了一些基本规则。因为你已经有了类,你必须修改这EDMX文件中,这样的概念模型是一样的业务对象:

You can do that with EDMX as well. There are some basic rules which you have to follow to make this work because this whole is done by naming conventions. Because you already have classes you must modify this in EDMX file so that conceptual model is the same as your business objects:

  • 具有保存或加载每个业务对象必须在概念模型实体
  • 在实体必须具有相同的名称作为业务对象。此外,还必须正确设置在属性窗口中的实体(抽象,访问级别和基实体必须与在业务对象)
  • 在业务对象的每个存储的属性必须在概念模型的实体的属性。同样,你必须正确设置每个属性(getter和setter访问,类型,可空,等等)。

EDMX由三层组成:

EDMX consists of three layers:

  • SSDL - 数据库的说明。这几乎总是发生,你不能在设计直接修改它。
  • 在CSDL - 实体描述,必须与您的业务对象。这是你在​​设计修改的内容。只要你想,你可以重命名字段。
  • 在MSL - SSDL和CSDL之间的映射。如果您在设计器中打开的任何实体上下文菜单中,您将看到表的映射。这将打开一个窗口,CSDL和SSDL之间的映射定义。

这些是最基本的规则,而是因为你已经拥有的业务对象,你将最有可能找到的情况下,将很难映射它。最好的办法是简单地问了具体的问题。这将极有可能对一些复杂性,导航性能或继承。

These are base rules but because you already have business objects you will most probably find situations where it will be hard to map it. The best way is simply to ask for that concrete issue. It will most probably be about some complex properties, navigation properties or inheritance.

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

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