/// <summary> /// Populate the sub entities of widgetList /// </summary> /// <param name="widgets"></param> /// <param name="toPopulate"></param> public override void Populate(IEnumerable <Widget> widgets, Flags toPopulate) { Log("Populate", widgets.GetIdenties(), toPopulate); // Implement breadth first loading of related entities. // Any entity that has been requested to be loaded, should be loaded at this level where possible. // Remove all sub entity types that this entity relates to directly. Flags stillToPopulate = toPopulate; stillToPopulate = stillToPopulate.Remove(EntityType.FieldTest); stillToPopulate = stillToPopulate.Remove(EntityType.FieldTest); stillToPopulate = stillToPopulate.Remove(EntityType.Thing); // Get sub entities: FieldTest if ((toPopulate & EntityType.FieldTest) == EntityType.FieldTest) { // Grab the ones that actually need populating IEnumerable <Widget> toBePopulated = widgets.Where(entity => entity.FieldTestListUsingForeignKeyFieldPopulated == false); // And load the sub entities for those ones. FieldTestList childFieldTestList = toBePopulated.Count() > 0 ? FieldTestRepo.GetByForeignKeyField( toBePopulated.GetIdenties(), stillToPopulate) : new FieldTestList(); Dictionary <int, List <FieldTest> > fieldTestListByForeignKeyField = childFieldTestList.MapByForeignKeyField; // Now go over all the entites. For ones that need popualting, populate collection // directly. For those already populated, make a check on sub entities to ensure // they are loaded to the required level FieldTestList toBeChecked = new FieldTestList(); foreach (Widget widget in widgets) { if (!widget.FieldTestListUsingForeignKeyFieldPopulated) { var FieldTestListsForWidget = fieldTestListByForeignKeyField.ContainsKey(widget.Identity) ? fieldTestListByForeignKeyField[widget.Identity] : null; if (FieldTestListsForWidget != null) { FieldTestListsForWidget.ForEach(entity => entity.ForeignKeyFieldWidget = widget); } widget.PopulateFieldTestListUsingForeignKeyField(FieldTestListsForWidget); } else { toBeChecked.AddRange(widget.FieldTestListUsingForeignKeyField); } } // If there's any "to be checked" (because they were already loaded) let the entities own // repo do whatever checks it needs to do if (toBeChecked.Count > 0) { FieldTestRepo.Populate(toBeChecked, stillToPopulate); } } // Get sub entities: FieldTest if ((toPopulate & EntityType.FieldTest) == EntityType.FieldTest) { // Grab the ones that actually need populating IEnumerable <Widget> toBePopulated = widgets.Where(entity => entity.FieldTestListUsingForeignKeyFieldNullablePopulated == false); // And load the sub entities for those ones. FieldTestList childFieldTestList = toBePopulated.Count() > 0 ? FieldTestRepo.GetByForeignKeyFieldNullable( toBePopulated.GetIdenties().Select(i => new Nullable <int>(i)).ToArray(), stillToPopulate) : new FieldTestList(); Dictionary <int?, List <FieldTest> > fieldTestListByForeignKeyFieldNullable = childFieldTestList.MapByForeignKeyFieldNullable; // Now go over all the entites. For ones that need popualting, populate collection // directly. For those already populated, make a check on sub entities to ensure // they are loaded to the required level FieldTestList toBeChecked = new FieldTestList(); foreach (Widget widget in widgets) { if (!widget.FieldTestListUsingForeignKeyFieldNullablePopulated) { var FieldTestListsForWidget = fieldTestListByForeignKeyFieldNullable.ContainsKey(widget.Identity) ? fieldTestListByForeignKeyFieldNullable[widget.Identity] : null; if (FieldTestListsForWidget != null) { FieldTestListsForWidget.ForEach(entity => entity.ForeignKeyFieldNullableWidget = widget); } widget.PopulateFieldTestListUsingForeignKeyFieldNullable(FieldTestListsForWidget); } else { toBeChecked.AddRange(widget.FieldTestListUsingForeignKeyFieldNullable); } } // If there's any "to be checked" (because they were already loaded) let the entities own // repo do whatever checks it needs to do if (toBeChecked.Count > 0) { FieldTestRepo.Populate(toBeChecked, stillToPopulate); } } // Get parent entities: Thing if ((toPopulate & EntityType.Thing) == EntityType.Thing) { // Grab the ids for the ones that actually need populating IEnumerable <Widget> toBePopulated = widgets.Where(entity => entity.ThingRequiresPopulation); int[] idsToLoad = toBePopulated.Select(entity => entity.ThingId).ToList().ToArray(); // And load the sub entities for those ones. ThingList parentThingList = toBePopulated.Count() > 0 ? ThingRepo.Get(idsToLoad, stillToPopulate) : new ThingList(); Dictionary <int, Thing> thingListById = parentThingList.MapById; // Now go over all the entites. For ones that need popualting, populate entities // directly. For those already populated, make a check on sub entities to ensure // they are loaded to the required level ThingList toBeChecked = new ThingList(); foreach (Widget widget in widgets) { if (widget.ThingRequiresPopulation) { widget.Thing = thingListById.ContainsKey(widget.ThingId) ? thingListById[widget.ThingId] : null; } else if (widget.ThingPopulated) { toBeChecked.Add(widget.Thing); } } // If there's any "to be checked" (because they were already loaded) let the entiies own // repo do whatever checks it needs to do if (toBeChecked.Count > 0) { ThingRepo.Populate(toBeChecked, stillToPopulate); } } }
public static void Save(FieldTestList fieldTestList) { _instance.Save(fieldTestList); }
public static void Populate(FieldTestList fieldTestList, Flags toPopulate) { _instance.Populate(fieldTestList, toPopulate); }
public static void Save(Flags toSave, FieldTestList fieldTestList) { _instance.Save(toSave, fieldTestList); }
public static void Refresh(FieldTestList fieldTestList, Flags toPopulate) { _instance.Refresh(fieldTestList, toPopulate); }