Jazzy
Jazzy

Reputation: 519

Extend Entity Object to include a calculated property

Let's say I have an Entity Object 'Jewels' that has the properties 'Name' and 'Birthdate'. I want to implement a LINQ query that returns an object that has 'Name', 'Birthdate' and 'Birthstone'. So I extend 'Jewels' like this:

public partial class JewelStones : Jewels

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

I can get this far, and I THINK I'm on the right track, but I don't know how to write a LINQ query and get back an object that includes Birthstone, so I can bind that object to a grid that will show Birthstone, which I'm not storing anywhere, as it's always calculated (this is pretend data, sorry if it's not logical).

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

How do I fill up my JewelStone object with Name, Birthdate, and Birthstone?

If I'm not following best practice here, please let me know!

EDIT

I've tried adding a partial class to the Entity partial class. When I reference the Jewel class now, it 'sees' the Birthstone property, but it is null. I don't know why? Here is the partial class:

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

If I use LINQ to query the entity to get a list of Jewel records, I get all the info from the Entity, Jewel.Birthstone is there, but it is null. However if I do a foreach on the results ---

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

stone will equal the expected result (birthstone for that month).

Why doesn't my partial class return the birthstone??

Upvotes: 0

Views: 1678

Answers (3)

Khanh TO
Khanh TO

Reputation: 48972

I'm not sure I understand your requirement correctly. But if you don't want to store Birthstone but calculate it on the fly, just change your code to

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

Upvotes: 1

Fendy
Fendy

Reputation: 4643

For me, it depends on where the logic for the calculated column resides.

If it resides in database, then you must do join query in the Linq. I assume in this case, you has a table named BirthStoneTable, with the month as the relation. I don't suggest to add a ternary operation inside linq query, such as select j.BirthDate.Month == 1 ? "Diamond" : //etc etc. It is hard to debug and to track (moreover for code coverage reason).

If it resides in UI specific (only to improve the display), I usually add a type-casted class, such as:

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

If the calculated column is used in the business logic, usually I handle the calculation in service / business logic. All of it to ensure the good Separation of Concern.

NB: I may misunderstand your requirement though

Upvotes: 0

Darko Kenda
Darko Kenda

Reputation: 4960

Isn't your Jewels EntityObject in a partial class too? You can most likely just add a Jewels partial class to "extend" it and add the wanted property there.

Upvotes: 0

Related Questions