private void toolStripButtonAutoGen_Click(object sender, EventArgs e) { try { Cursor.Current = Cursors.WaitCursor; toolStripComboBoxRootClass.Items.Clear(); int sampleSize; if (!int.TryParse(toolStripTextBoxSampleSize.Text, out sampleSize)) { sampleSize = 1000; toolStripTextBoxSampleSize.Text = sampleSize.ToString(); } var doc = MongoCollectionSchemaStore.GetSchemaDocument(CollectionInfo.Database, CollectionInfo.Name, sampleSize) .SchemaDocument; var classes = new BsonDocumentConverter().ToCSharpClassDeclarations(doc, CollectionInfo.Name); scintillaCode.Text = string.Join("\r\n", classes.ToArray()).TrimEnd('\r', '\n').TrimStart('\r', '\n'); var types = new MongoDynamicCodeRunner().CompileModelCode(scintillaCode.Text, toolStripTextBoxNamespace.Text ?? "DEFAULT"); LoadRootClassDropDown(types.Select(x => x.Name).ToList(), new BsonDocumentConverter().GetDocName(CollectionInfo.Name) + "Model"); } finally { Cursor.Current = Cursors.Default; } }
private bool ValidateModel(string selectedRootClass, string @namespace, bool displayError) { string code = scintillaCode.Text; toolStripComboBoxRootClass.Items.Clear(); try { if (string.IsNullOrWhiteSpace(code)) { throw new Exception("Please paste your C# model code into the editor or use the Auto Generate option."); } if (string.IsNullOrWhiteSpace(@namespace)) { throw new Exception("Please enter a namespace."); } @namespace = @namespace.Trim(); foreach (var connInfo in Settings.Instance.Connections) { foreach (var dbInfo in connInfo.Databases) { foreach (var collectionInfo in dbInfo.Collections) { if (collectionInfo.Path != CollectionInfo.Path && collectionInfo.Namespace == @namespace) { throw new Exception("Namespace already used in another collection. Please select a new one."); } } } } var types = new MongoDynamicCodeRunner().CompileModelCode(code, @namespace); if (!types.Any()) { throw new Exception("No model classes are defined."); } LoadRootClassDropDown(types.Select(x => x.Name).ToList(), selectedRootClass); if (toolStripComboBoxRootClass.SelectedItem == null) { throw new Exception("Please select a root model class."); } } catch (Exception ex) { if (displayError) { MessageBox.Show(ex.Message, "Invalid Model", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(false); } return(true); }