Exemplo n.º 1
0
        /// <summary>
        /// Generate the code and the code file.
        /// </summary>
        /// <param name="database">The existing instance of MongoDatabase.</param>
        /// <param name="directory">The directoty to store the code files.</param>
        /// <param name="globalNamespace">The code file namespace to use.</param>
        /// <returns>The number of code files created.</returns>
        public int Generate(MongoDatabase database, string directory, string globalNamespace)
        {
            // Make sure the page reference exists.
            if (database == null)
            {
                throw new ArgumentNullException("database");
            }
            if (String.IsNullOrEmpty(directory))
            {
                throw new ArgumentNullException("directory");
            }
            if (String.IsNullOrEmpty(globalNamespace))
            {
                throw new ArgumentNullException("globalNamespace");
            }

            int ret = 0;

            // Get the collections names within the database.
            IEnumerable <string> collections = database.GetCollectionNames();

            // Make sure collection exist.
            if (collections != null && collections.Count() > 0)
            {
                // For each collection in the database.
                foreach (string collectionName in collections)
                {
                    try
                    {
                        // Get the document collection.
                        MongoCollection <BsonDocument> collection = database.GetCollection(collectionName);
                        BsonDocument[] bsonDocuments = collection.FindAll().SetLimit(1).ToArray();

                        // At least one document should be returned.
                        if (bsonDocuments != null && bsonDocuments.Length > 0)
                        {
                            // Create the bson model container.
                            Nequeo.Data.MongoDb.CodeDom.BsonModelContainer model = new Nequeo.Data.MongoDb.CodeDom.BsonModelContainer();
                            model.ClassName    = collectionName;
                            model.Namespace    = globalNamespace;
                            model.BsonDocument = bsonDocuments[0];
                            model.AssignProperties();

                            // Generate the code files.
                            CodeCompileUnit unit = Generate(model);
                            CreateCodeFile(directory.TrimEnd('\\') + "\\" + collectionName + ".cs", unit);

                            // Increment by one.
                            ret++;
                        }
                    }
                    catch { }
                }
            }

            // Return the number of code files created.
            return(ret);
        }
Exemplo n.º 2
0
        private void button2_Click(object sender, EventArgs e)
        {
            Nequeo.Data.MongoDb.Connection conn   = new Nequeo.Data.MongoDb.Connection("nequeompc");
            Nequeo.Data.MongoDb.DataAccess access = new Nequeo.Data.MongoDb.DataAccess(conn);
            MongoDatabase database = access.GetDatabase("nequeo");
            MongoCollection <BsonDocument> collb = access.GetCollection(database, "User");
            BsonDocument document = access.FindAll(collb, limit: 1).ToArray().First();

            Nequeo.Data.MongoDb.CodeDom.BsonDocumentModel  bson  = new Nequeo.Data.MongoDb.CodeDom.BsonDocumentModel();
            Nequeo.Data.MongoDb.CodeDom.BsonModelContainer model = new Nequeo.Data.MongoDb.CodeDom.BsonModelContainer();
            model.ClassName    = "User";
            model.Namespace    = "Nequeo.MongoDb";
            model.BsonDocument = document;
            model.AssignProperties();

            System.CodeDom.CodeCompileUnit unit = bson.Generate(model);
            bson.CreateCodeFile(@"C:\Temp\BsonDocModel.cs", unit);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generate the code and the code file.
        /// </summary>
        /// <param name="database">The existing instance of MongoDatabase.</param>
        /// <param name="directory">The directoty to store the code files.</param>
        /// <param name="globalNamespace">The code file namespace to use.</param>
        /// <returns>The number of code files created.</returns>
        public async Task <int> Generate(IMongoDatabase database, string directory, string globalNamespace)
        {
            // Make sure the page reference exists.
            if (database == null)
            {
                throw new ArgumentNullException("database");
            }
            if (String.IsNullOrEmpty(directory))
            {
                throw new ArgumentNullException("directory");
            }
            if (String.IsNullOrEmpty(globalNamespace))
            {
                throw new ArgumentNullException("globalNamespace");
            }

            int ret = 0;

            // Get the collections names within the database.
            IAsyncCursor <MongoDB.Bson.BsonDocument> collections = await database.ListCollectionsAsync();

            // Make sure collection exist.
            if (collections != null)
            {
                // Move to the list.
                bool moved = await collections.MoveNextAsync();

                // Get the list.
                IEnumerable <BsonDocument> documentList = collections.Current;

                // Make sure collection exist.
                if (documentList != null && documentList.Count() > 0)
                {
                    // For each collection in the database.
                    foreach (BsonDocument document in documentList)
                    {
                        try
                        {
                            // Get the collection name.
                            BsonElement nameElement    = document.GetElement("name");
                            BsonValue   nameValue      = nameElement.Value;
                            string      collectionName = nameValue.AsString;

                            // Get the document collection.
                            IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>(collectionName);
                            BsonDocument[] bsonDocuments = (await collection.Find(new BsonDocument()).Limit(1).ToListAsync()).ToArray();

                            // At least one document should be returned.
                            if (bsonDocuments != null && bsonDocuments.Length > 0)
                            {
                                // Create the bson model container.
                                Nequeo.Data.MongoDb.CodeDom.BsonModelContainer model = new Nequeo.Data.MongoDb.CodeDom.BsonModelContainer();
                                model.ClassName    = collectionName;
                                model.Namespace    = globalNamespace;
                                model.BsonDocument = bsonDocuments[0];
                                model.AssignProperties();

                                // Generate the code files.
                                CodeCompileUnit unit = Generate(model);
                                CreateCodeFile(directory.TrimEnd('\\') + "\\" + collectionName + ".cs", unit);

                                // Increment by one.
                                ret++;
                            }
                        }
                        catch { }
                    }
                }
            }

            // Return the number of code files created.
            return(ret);
        }