private static T CreateChildElement <T>(ModelElement parent, bool raiseInstantiateEvents) where T : ModelElement { var childClass = parent.Partition.DomainDataDirectory.DomainClasses .FirstOrDefault(dci => typeof(T).IsAssignableFrom(dci.ImplementationClass) && !dci.ImplementationClass.IsAbstract); var elementOperations = new ElementOperations(parent.Store, parent.Partition); if (elementOperations != null) { var elementGroupPrototype = new ElementGroupPrototype(parent.Partition, childClass.Id); var partition = elementOperations.ChooseMergeTarget(parent, elementGroupPrototype).Partition; var element = (T)partition.ElementFactory.CreateElement(childClass); var elementGroup = new ElementGroup(partition); elementGroup.Add(element); elementGroup.MarkAsRoot(element); elementOperations.MergeElementGroup(parent, elementGroup); // Flag the element in the state so that the ProductStore class sees it and doesn't // raise the instantiation event. if (!raiseInstantiateEvents) { element.Store.PropertyBag.Add(element, null); } return(element); } return(default(T)); }