Пример #1
0
        private static void CheckRelation(object Sender, MongoRelation Relation, bool FromUp)
        {
            Dictionary <string, object> fieldValues = new Dictionary <string, object>();

            for (int index = 0; index < Relation.CurrentFieldNames.Length; index++)
            {
                string currentFieldName  = Relation.CurrentFieldNames[index];
                string relationFieldName = Relation.RelationFieldNames[index];
                object v = ReflectionUtility.GetPropertyValue(Sender, currentFieldName);
                fieldValues.Add(relationFieldName, v);
            }

            if (fieldValues.All(V => V.Value == null))
            {
                return;
            }


            var filters = fieldValues.Select(CurrentFieldvalue => MongoQuery <BsonDocument> .Eq(Relation.RelationObjectName, CurrentFieldvalue.Key, CurrentFieldvalue.Value)).ToList();

            var relationCollection = CollectionsManager.GetPrimaryCollection <BsonDocument>(Relation.RelationObjectName);

            var documentCount = relationCollection.CountAsync(Builders <BsonDocument> .Filter.And(filters)).Result;

            bool okRelation = FromUp ? documentCount != 0 : documentCount == 0;

            if (!okRelation)
            {
                if (FromUp)
                {
                    throw new ValidateUpRelationException(string.Join(",", filters.FilterToJson()));
                }

                throw new ValidateDownRelationException(string.Join(",", filters.FilterToJson()));
            }
        }
 private IMongoCollection <T> GetCollection()
 {
     return(FromPrimary
         ? CollectionsManager.GetPrimaryCollection <T>(typeof(T).Name)
         : CollectionsManager.GetCollection <T>(typeof(T).Name));
 }