protected string getOperatorNameByPrivateField <T>(UpdateDefinition <T> ud)
        {
            var operatorField = ud.GetType().GetField("_operatorName", BindingFlags.NonPublic | BindingFlags.Instance);

            var operatorName = operatorField.GetValue(ud) as string;

            return(operatorName);
        }
        protected string getOperatorNameByType <T>(UpdateDefinition <T> ud)
        {
            var operatorCompleteName = ud.GetType().Name;

            //avoid generic number in name: "List`1" -> "List"
            var operatorName = operatorCompleteName.Substring(0, operatorCompleteName.IndexOf('`'));

            return(operatorName);
        }
Пример #3
0
        private UpdateDefinition <TEntity> SetUpdatedTimeStamp(UpdateDefinition <TEntity> update)
        {
            var updates = new List <UpdateDefinition <TEntity> >()
            {
                update
            };

            if ((update.GetType().GenericTypeArguments[0]).GetTypeInfo().IsSubclassOf(typeof(BaseAuditedEntity)))
            {
                updates.Add(Builders <TEntity> .Update.Set("LastModifiedAt", DateTime.UtcNow));

                if (this.auditProvider != null && this.auditProvider.CurrentUserEmail != null)
                {
                    updates.Add(Builders <TEntity> .Update.Set("LastModifiedBy", this.auditProvider.CurrentUserEmail));
                }
            }

            return(Builders <TEntity> .Update.Combine(updates));
        }
        protected void CheckOperator <T>(
            UpdateDefinition <T> update,
            string privateFieldToCheck,
            Func <UpdateDefinition <T>, string> getOperatorName,
            string expectedOperatorName,
            int expectedCount)
        {
            var field = update.GetType().GetField(privateFieldToCheck, BindingFlags.NonPublic | BindingFlags.Instance);

            var updateDefinitions = field.GetValue(update) as IEnumerable <UpdateDefinition <T> >;

            int count = 0;

            foreach (var ud in updateDefinitions)
            {
                var operatorName = getOperatorName(ud);

                Assert.AreEqual(expectedOperatorName, operatorName);

                count++;
            }

            Assert.AreEqual(expectedCount, count);
        }