N-N relation, load the related objects as ContentParts?

Topics: General, Writing modules
Mar 28, 2012 at 3:04 AM

I have two content parts with an N-N relation between them. 

Let's say they are: ListPart & OrgPart. The ListPart has a property: List<OrgPartRecord> Orgs { get; set; }. It's all working, modeled after the N-N example in the docs. I'm having a little difficulty with this though because this gives me a collection of OrgPartRecords, but I want a collection of OrgPart's. Is it possible to do this without loading each with an additional query? I'm guessing I will have to write a query but just wanted to check to see if having the ContentPartRecord already loaded meant there might be an easy way to instantiate the ContentPart's too, without an additional trip to the database. 

Coordinator
Mar 28, 2012 at 4:31 AM

If you look at the way it's done in Nwazet.Commerce/Bundle feature, there are a few caveats to connecting content items instead of just records, because the relation is between records but you need to connect composites of those records, the content items.

Mar 28, 2012 at 5:06 AM
Edited Mar 28, 2012 at 2:46 PM

Thanks! That worked. 

In case anyone else is interested, the only extra step on top of the N-N docs example, to loading the data as Content Items, is to add a query in the Part Driver's Display() method: 

            protected override DriverResult Display(OrgListPart part, string displayType, dynamic shapeHelper)
            {

                var orgParts = _contentManager.GetMany(
                    part.OrgXrefRecords.Select(xref => xref.OrgPartRecord.Id).ToList()
                    , VersionOptions.AllVersions
                    , QueryHints.Empty
                );

                return ContentShape(
                    "Parts_OrgList"
                    , () => shapeHelper.Parts_OrgList(
                        Orgs: orgParts
                    );
            }