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

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

问题描述

我希望将实体框架作为我的数据层进行整合。



我使用本教程跟踪文章和生成的poco实体: http://blogs.msdn.com /b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx



我有我的自己的业务对象。这是我的业务对象Brach:

  public class Branch 
{
public long BranchId {get;私人集合}
public string BranchName {get;组; }
public string BranchCode {get;组;

public Branch(){}

public void InsertBranch(Guid companyId)
{
using(var ctx = new Entities.Entities() )
{
var branch = new T_STF_BRANCH()//这是生成的POCO对象
{
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();
}
//这里我需要将POCO实体转化为我的商业实体...
}
}

我不知道如何将POCO实体转换为我的业务实体。

我应该从POCO和POCO转换?

解决方案

IMHO这太复杂了。为什么你有POCO实体的持久化和单独的对象来处理加载到POCO实体的数据?听起来像你的应用程序是结构化的。



ORM意味着对象关系映射。这意味着关系世界和对象世界之间的映射。通常它也可以翻译成数据库和业务对象之间的映射。所以你应该使用你的POCO对象作为你的业务对象。这是使用POCO的全部含义。如果您不想将它们用作业务对象,则不需要它们,您可以直接使用默认实体对象。



如果要将POCO用作业务对象只需让EF为您生成这些POCO,并为定义方法的每个POCO添加部分类。



Btw。您的业​​务对象实际上看起来像执行活动记录模式。如果您想使用这种模式,您应该检查基于以下的 Windsor Active Record NHibernate的顶部。



编辑:



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



一种方法是放弃EFV4和EDMX,并检查新的 EFv4.1及其新流畅的API (也称为代码优先)用于映射。这对于单独的问题是完整的,或者简单地在SO上使用搜索。



您也可以使用EDMX。有一些基本的规则,你必须遵循,使这项工作,因为这整体是通过命名约定完成的。因为您已经有类,所以您必须在EDMX文件中进行修改,以便概念模型与业务对象相同:




  • 每个业务对象必须保存或加载必须在概念模型中具有实体

  • 实体必须与业务对象具有相同的名称。您还必须在属性窗口中正确设置实体(抽象,访问级别和基础实体必须与业务对象中相同)

  • 业务对象中的每个存储的属性必须具有财产在概念模型中的实体。再次,您必须正确设置每个属性(getter和setter可访问性,类型,可空等)。



EDMX由三层组成:




  • SSDL - 数据库的描述。这几乎总是生成的,你不能直接在设计器中修改它。

  • CSDL - 必须与业务对象相同的实体的描述。这是你在设计师中修改的。您可以根据需要重命名字段。

  • MSL - SSDL和CSDL之间的映射。如果您在设计器中的任何实体上打开上下文菜单,您将看到表映射。它将打开一个窗口,其中定义了CSDL和SSDL之间的映射。



这些是基本规则,但是因为您已经有业务对象很可能会发现难以映射的情况。最好的办法就是要提出具体的问题。它最有可能是一些复杂的属性,导航属性或继承。


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

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

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...
    }
}

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?

解决方案

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 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.

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.

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.

Edit:

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

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.

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:

  • Every business object which have to be saved or loaded must have entity in conceptual model
  • Entity must have the same name as the business object. You must also correctly set up the entity in property window (abstract, access level and base entity must be the same as in your business object)
  • Every stored property in the business object must have a property in the entity in conceptual model. Again you must correctly set up each property (getter and setter accessibility, type, nullable, etc).

EDMX consists of three layers:

  • SSDL - description of the database. This is almost always generated and you can't modify it directly in the designer.
  • CSDL - description of entities which must be the same as your business objects. This is what you modify in the designer. You can rename the fields as you want.
  • MSL - the mapping between SSDL and CSDL. If you open context menu on any entity in the designer you will see Table mapping. It will open a window with definition of mapping between CSDL and 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天全站免登陆