Full Object-Graph Mapping in 2.0.0-alpha1

25 views
Skip to first unread message

Tina Mouton

unread,
Dec 26, 2015, 2:08:28 PM12/26/15
to Simple.Data
Hi,

I have a small DB of four tables that looks like this
Segment > Family > Class > Commodity
I've also attached a diagram of the db if anyone is interested.

After reading this post from Mark's blog, I was really excited that this may actually work, so I tried...

var query = await db.Segment.FindAll(db.Segment.Id == code)
                       
.With(db.Segment.Family.As("families").Class.As("classes"))
                       
.FirstOrDefault();

...but, no luck.

after testing a few variations like...
var query2 = await db.Segment.FindAll(db.Segment.Id == code)
                       
.With(db.Segment.Family.As("families"))
                       
.With(db.Segment.Family.Class.As("classes"))
                       
.FirstOrDefault();
  

var query3 = await db.Segment.FindAll(db.Segment.Id == code)
                       
.With(db.Segment.Family.As("families"))
                       
.With(db.Family.Class.As("classes"))
                       
.FirstOrDefault();


I've seen several posts with conflicting thoughts on this.  This one where Mark himself suggest using "substrate()" (which I couldn't find any other reference to or get it to work), and this one where Mark himself says it just isn't possible.  Granted these posts are both pretty old and almost certainly refer to 0.19 or earlier, but it was all I could find

My classes are set up to have a nested list of each object...
public class Segment
   
{
       
public Segment()
       
{
            families
= new List<Family>();
       
}

       
public string id { get; set; }
       
public String description { get; set; }
       
public int search_id { get; set; }
       
public IList<Family> families { get; set; }

Family and Class objects are modeled in the same fashion.

Eventually I gave up and went with an old-fashioned loop, to get things loaded correctly.  It works but it's not ideal.

var query = await db.Segment.FindAll(db.Segment.Id == code)
                        .With(db.Segment.Family.As("families"))
                        .FirstOrDefault();
                    
var segment = (Segment)query;

foreach (var f in segment.families)
{
  f.classes = await db.Class.FindAll(db.Class.parent_family_id == f.id)
                        .With(db.Class.Commodity.As("commodities")).ToList<Class>();
}
It's not as tidy as the .With() call would be, but my main issue with it is that it will hit the database for EVERY family in the list.  Seems like there must be a better way!

Does anyone have any thoughts on what I'm doing wrong?  Any advice at all would be greatly appreciated.  Thanks.
unspsc-diagram.png

Jon Scolamiero

unread,
Jan 4, 2016, 4:42:05 PM1/4/16
to Simple.Data
Hi Tina,

Generally in these situations I've found that selecting the objects into straight lists, then filling out the relationships in C# has worked out really well.  Not to mention it's pretty darned fast.  I would be happy to walk you through some different ways of doing that depending on your situation if you'd like.

Regards,

Jon

Tina Mouton

unread,
Jan 4, 2016, 6:03:16 PM1/4/16
to simpl...@googlegroups.com
Hey Jon

I would love any kind of guidance or opinions you have to share.  Just tell me what specifically you want me to provide and I'll be happy to share away.

-t

--
You received this message because you are subscribed to a topic in the Google Groups "Simple.Data" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/simpledata/8882YTosIbE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to simpledata+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages