【问题标题】:Extend Entity Object to include a calculated property扩展实体对象以包含计算属性
【发布时间】:2013-04-29 01:36:10
【问题描述】:

假设我有一个实体对象“珠宝”,它具有“姓名”和“生日”属性。 我想实现一个 LINQ 查询,它返回一个具有“姓名”、“生日”和“生日石”的对象。所以我像这样扩展“珠宝”:

public partial class JewelStones : Jewels

string Birthstone = null;
public void JewelsWithStone()
{
     this.Birthstone = "diamond";
      //(we figure out what stone applies to the month here)
}

我可以做到这一点,我认为我在正确的轨道上,但我不知道如何编写 LINQ 查询并取回包含生日石的对象,因此我可以将该对象绑定到网格这将显示生日石,我没有将其存储在任何地方,因为它总是经过计算(这是假数据,如果不合逻辑,请见谅)。

List<Jewel> jewels = new List<Jewel>;
using (jewelentities db = new jewelentities())
{
    jewels = (from j in db.Jewels select j).ToList();
}

如何用姓名、生日和生日石填充我的 JewelStone 对象?

如果我在这里没有遵循最佳做法,请告诉我!

编辑

我尝试向实体分部类添加分部类。当我现在引用 Jewel 类时,它“看到”Birthstone 属性,但它为空。我不知道为什么?这是部分类:

public partial class Jewel
{
    private string _birthstone;
    public string Birthstone
    {
        get { return _birthstone; }
        set
        {
            JewelBusiness jewelBusiness = new JewelBusiness();
            _birthstone = jewelBusiness.RequestBirthstone(birthmonth); 
        }
    }
}

如果我使用 LINQ 查询实体以获取 Jewel 记录列表,我会从实体中获取所有信息,Jewel.Birthstone 在那里,但它为空。但是,如果我对结果进行 foreach ---

foreach (Jewel j in jewels)
{
    string stone = jewelBusiness.RequestBirthstone(j.Birthmonth);
}

stone 将等于预期结果(该月的生日石)。

为什么我的部分班级不归还生日石??

【问题讨论】:

  • 查看我的更新答案
  • 我认为改变属性的get方法并检查该字段是否为空,是要走的路。但请记住,您不能在 entityframe-work 查询中对实体使用扩展属性,因此您必须先使用 toList,然后再查询扩展属性。

标签: c# linq entity-framework asp.net-4.5


【解决方案1】:

我不确定我是否正确理解了您的要求。但是,如果您不想存储 Birthstone 而是即时计算,只需将代码更改为

public partial class Jewel
{
    private string _birthstone;
    public string Birthstone
    {
        get 
        { 
             if (_birthstone == null)
             {
                  JewelBusiness jewelBusiness = new JewelBusiness();
                  _birthstone = jewelBusiness.RequestBirthstone(birthmonth); 
             }
             return _birthstone; 
        }
    }
}

【讨论】:

    【解决方案2】:

    你的 Jewels EntityObject 不也在一个部分类中吗?您很可能只需添加一个 Jewels 部分类来“扩展”它并在那里添加所需的属性。

    【讨论】:

    • 这就是我试图做的,我不确定如何实现它,或者这是否是在绑定之前向实体对象添加属性的最佳方式。
    【解决方案3】:

    对我来说,这取决于计算列的逻辑所在的位置。

    如果它驻留在数据库中,那么您必须在 Linq 中进行连接查询。我假设在这种情况下,您有一个名为 BirthStoneTable 的表,其中月份是关系。我不建议在 linq 查询中添加三元运算,比如select j.BirthDate.Month == 1 ? "Diamond" : //etc etc。很难调试和跟踪(此外出于代码覆盖率的原因)。

    如果它驻留在特定的 UI 中(只是为了改善显示),我通常会添加一个类型转换的类,例如:

    public class JewelUI{
      public explicit operator JewelUI(Jewel jewel){
        JewelUI jewelUI = new JewelUI();
        // assign birthdate and name
        jewelUI.BirthStone = GetBirthStone(jewel.BirthDate.Month);
      }
    
      public string BirthStone{get;set;};
    
      public string GetBirthStone(int month){
        if(month == 1) return "Diamond";
        //etc etc
      }
    }
    

    如果在业务逻辑中使用计算列,通常我在服务/业务逻辑中处理计算。所有这些都是为了确保良好的关注点分离。

    注意:虽然我可能误解了您的要求

    【讨论】:

    • 我想你确实理解了,你的例子很有意义。简而言之,我需要一个具有数据库属性的对象,然后是一些属性。
    猜你喜欢
    • 2012-11-29
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多