private static ExplorerEFElement GetNewOrExisting( EditingContext context, EFElement efElement, ExplorerEFElement parent, Type viewModelType, bool mustNotExist) { var xref = GetModelToBrowserModelXRef(context); var result = xref.GetExisting(efElement); if (result != null) { if (mustNotExist) { Debug.Fail( string.Format( CultureInfo.CurrentCulture, Resources.BadInsertChildAlreadyExists, efElement.GetType().FullName, parent.GetType().FullName)); return(null); // TODO: we need to provide a general exception-handling mechanism and replace the above Assert() // by e.g. the excepiton below // throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.BadInsertChildAlreadyExists, efElement.GetType().FullName, parent.GetType().FullName)); } else { result.Parent = parent; } } else { if (viewModelType != null) { if (!xref.IsDisplayedInExplorerProtected(efElement)) { Debug.Fail( "Attempting to create an ExplorerEFElement of type " + viewModelType.FullName + " based on an EFElement which is not displayed in the Explorer " + efElement.ToPrettyString()); return(null); } result = Activator.CreateInstance(viewModelType, context, efElement, parent) as ExplorerEFElement; xref.Add(efElement, result); } } return(result); }