Beispiel #1
0
        private static MongoCollection GetCollecion <T>(string connectionName, string database, string collection)
        {
            MongoCollectionWarpper collectionWarpper = GetCollecionInternal(connectionName, database, collection);

            if (collectionWarpper != null && !collectionWarpper.IsCreateIndex && typeof(T).IsSubclassOf(typeof(MongoDocumentObject)))
            {
                lock (collectionWarpper)
                {
                    if (!collectionWarpper.IsCreateIndex)
                    {
                        var instance = (MongoDocumentObject)System.Activator.CreateInstance(typeof(T));
                        foreach (var indexkeys in instance.CreateIndex())
                        {
                            if (indexkeys != null && indexkeys.Item1.Length > 0 && !collectionWarpper.MongoDBCollection.IndexExists(indexkeys.Item1))
                            {
                                collectionWarpper.MongoDBCollection.CreateIndex(new MongoIndexKeysWarpper(indexkeys.Item1).MongoIndexKeys, IndexOptions.SetUnique(indexkeys.Item2).SetBackground(indexkeys.Item3));
                            }
                        }

                        collectionWarpper.IsCreateIndex = true;
                    }
                }
            }

            return(collectionWarpper == null ? null : collectionWarpper.MongoDBCollection);
        }
Beispiel #2
0
        private static MongoCollection GetCollecion(string connectionName, string database, string collection)
        {
            MongoCollectionWarpper collectionWarpper = GetCollecionInternal(connectionName, database, collection);


            return(collectionWarpper == null ? null : collectionWarpper.MongoDBCollection);
        }
Beispiel #3
0
        private static MongoCollectionWarpper GetCollecionInternal(string connectionName, string database, string collection)
        {
            string collectionkey = string.Format("{1}:{2}@{0}", connectionName, database, collection);

            MongoCollectionWarpper collectionWarpper = null;

            if (!CollectionDic.TryGetValue(collectionkey, out collectionWarpper))
            {
                lock (CollectionDic)
                {
                    if (!CollectionDic.TryGetValue(collectionkey, out collectionWarpper))
                    {
                        var mongocollection = CreateInstanceUseConfig(connectionName).GetDatabase(database).GetCollection(collection);
                        if (mongocollection != null)
                        {
                            collectionWarpper = new MongoCollectionWarpper(mongocollection);
                        }
                        CollectionDic.Add(collectionkey, collectionWarpper);
                    }
                }
            }

            return(collectionWarpper);
        }