public ObjectDefinitionLookups GetLookups()
 {
     ObjectDefinitionLookups result = _CachedLookups;
     if (result == null)
     {
         result = new ObjectDefinitionLookups();
         IMongoDatabase database = GetDatabase(DATABASE_NAME, false);
         LoadObjectDefinition(database, result);
         _CachedLookups = result;
     }
     return result;
 }
 private void OnNotification(object sender, NotificationEventArgs e)
 {
     _CachedLookups = null;
 }
 private void LoadObjectDefinition(IMongoDatabase database, ObjectDefinitionLookups lookups)
 {
     IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("ObjectDefinition");
     IAsyncCursor<BsonDocument> mongoCursor = collection.FindSync(new BsonDocument());
     while (mongoCursor.MoveNext())
     {
         foreach (BsonDocument item in mongoCursor.Current)
         {
             ObjectDefinition objectDefinition = new ObjectDefinition();
             objectDefinition.ObjectDefinitionID = BsonHelper.GetGuid(item, "_id");
             objectDefinition.ObjectID = BsonHelper.GetString(item, "ObjectID");
             objectDefinition.OrganisationID = BsonHelper.GetInteger(item, "OrganisationID");
             if (objectDefinition.OrganisationID.HasValue && (objectDefinition.OrganisationID.Value == 0))
                 objectDefinition.OrganisationID = null;
             objectDefinition.Name = BsonHelper.GetString(item, "Name");
             objectDefinition.MIMEType = BsonHelper.GetString(item, "MIMEType");
             objectDefinition.Description = BsonHelper.GetString(item, "Description");
             objectDefinition.SerialisationName = BsonHelper.GetString(item, "SerialisationName");
             objectDefinition.Singleton = BsonHelper.GetBoolean(item, "Singleton");
             if (item.Contains("Properties"))
             {
                 BsonArray array = item["Properties"].AsBsonArray;
                 foreach (BsonValue arrayItem in array)
                 {
                     BsonDocument propertyItem = arrayItem.AsBsonDocument;
                     if (propertyItem != null)
                     {
                         if (objectDefinition.Properties == null)
                             objectDefinition.Properties = new List<PropertyDefinition>();
                         PropertyDefinition property = new PropertyDefinition();
                         property.PropertyDefinitionID = BsonHelper.GetGuid(propertyItem, "_id");
                         property.PropertyID = BsonHelper.GetString(propertyItem, "PropertyID");
                         property.Name = BsonHelper.GetString(propertyItem, "Name");
                         property.Description = BsonHelper.GetString(propertyItem, "Description");
                         property.DataType = (TPropertyDataType)propertyItem["DataType"].AsInt32;
                         if (propertyItem.Contains("DataTypeLength"))
                             property.DataTypeLength = propertyItem["DataTypeLength"].AsInt32;
                         property.MIMEType = BsonHelper.GetString(propertyItem, "MIMEType");
                         property.MinValue = BsonHelper.GetString(propertyItem, "MinValue");
                         property.MaxValue = BsonHelper.GetString(propertyItem, "MaxValue");
                         property.Units = BsonHelper.GetString(propertyItem, "Units");
                         property.IsCollection = BsonHelper.GetBoolean(propertyItem, "IsCollection");
                         property.IsMandatory = BsonHelper.GetBoolean(propertyItem, "IsMandatory");
                         property.Access = (TAccessRight)propertyItem["Access"].AsInt32;
                         if (propertyItem.Contains("SortOrder"))
                             property.SortOrder = propertyItem["SortOrder"].AsInt32;
                         property.SerialisationName = BsonHelper.GetString(propertyItem, "SerialisationName");
                         property.CollectionItemSerialisationName = BsonHelper.GetString(propertyItem, "CollectionItemSerialisationName");
                         objectDefinition.Properties.Add(property);
                     }
                 }
             }
             lookups.AddObjectDefinition(objectDefinition);
         }
     }
 }