实体框架:在业务层(对象)中创建自定义属性

时间:2009-04-06 14:47:25

标签: c# entity-framework

使用Entity Framework创建DataLayer(DataLayer.dll) DataLayer.dll包含数据对象。 我需要为类'person'添加一些业务逻辑。

是的,我知道,我可以使用部分类来添加业务逻辑。 但在这种情况下(使用部分类)我们需要在DataLayer.dll中实现它!

我需要使用BusinessLayer.dll

因此创建了业务层(BusinesLayer.dll)

在BusinesLayer.dll中我们有文件myPerson.cs

using DataLayer;

namespace Businesslayer
{
public class myPerson : DataLayer.Person

public Property String FullName()
{
  get {return this.Firstname + " " + this.LastName}
}

}

实际上我需要在网格中显示数据,比如

 var data=context.Person;
 dataGridView1.dataSource = data;

是的,context.Person返回一个Person-Objects列表  因此在网格中,列'FullName'不可用

如何在myPerson-Objects上创建List?

1 个答案:

答案 0 :(得分:1)

首先,如果您只是创建FullName以便在网格中显示它,那么我会说它属于UI层,而不是业务层。

其次,您不需要继承。你可以使用委托。在构造函数中为您的UIPerson类提供Person的实例。然后,对于要向网格公开的Person的每个属性,执行以下操作:

public DateTime BirthDate
{
   get {return _person.BirthDate;}
   set {_person.BirthDate = value;}
}

ReSharper可以为您做到这一点,或者您可以使用其他工具或手动完成。然后,您可以以相同的方式添加FullName属性。

相关问题