Record inheritance

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 10-Jan-2011 16:24:56   

Inheritance as used in your project is about the 'entity' using the tables to create an inheritance structure...I love it and it works great.

What I would like to explore is 'record inheritance'. This is where you have one row (Row A) with a bunch of fields and you want to create a new row (Row B in the same table) that links itself back to Row A. All set, no problem.

Now, that 'relationship' above is not the typical parent/child relationship that the above normally represents. In this case when I retrieve Row B, I will look to see if it has a link to a parent (Row A) and if it does, most field values will come from Row A - but some of the field values (if defined in Row B) will come from Row B! This allows you to have 'inherited records' so that one change to a parent is reflective in all the children, etc.

I can do all the above with LLBLGen and such....not a problem. However, what is the best way to retrieve the records?

If I was doing a custom projection in the linq to a named or anonymous type, there are NO issues with this scenario at all.

var answer=from thetable in metaData.Table
           join inheritTable in metaData.Table on thetable.TableIdInheritedFrom equals inheritTable.TableId into joinedTable
           from inheritTable in joinedTable.DefaultIfEmpty()
           select new{ description=inheritTable.Description ?? thetable.Description, ....}

However, may times I am selecting the LLBLGen entity as what is returned. Given that - is there a way to return the LLBLGen entity with modified fields?

var answer=from thetable in metaData.Table
           join inheritTable in metaData.Table on thetable.TableIdInheritedFrom equals inheritTable.TableId into joinedTable
           from inheritTable in joinedTable.DefaultIfEmpty()
           select thetable {substitute inheritTable.Description here if possible?}

Not sure if I was clear in explaining this or if there is anything I can do to get this all done in one linq query. I could pull back both the inheritTable and theTable entity and go through and change them, etc....

Thoughts?

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 10-Jan-2011 16:32:10   

I guess, in summary, wondering if there is any way to control/customize the 'projection to an entity' in LLBLGen?

Maybe creating a view in database would be best solution?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Jan-2011 05:38:19   

In your linq code above, all you are doing is a COALESCE. You can do it the same in LLBLGen API through LLBLGen DBFunctionCalls.

Now, at Entity level what I would probably do is create a partial class with some special properties that access the entity fields. Something like:

public string MyFieldA
{
     get
     {
            // (I think could be shortened)
            toReturn = this.FieldA;
            if (this.TheParent != null && !string.IsNullOrEmpty(this.TheParent.FieldA))
           {
                 toReturn = this.TheParent.FieldA;
           }
           return toReturn;
     }
}

I assume you only want one level parent-child. So do the same for all the fields and you have more or less want you are looking for.

David Elizondo | LLBLGen Support Team
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 11-Jan-2011 15:54:49   

I want all the data available to me in one linq query - just load the entity differently. After much thinking, I think it is probably the best solution to just use custom projections or custom views everywhere I need this.

Thanks.