Beispiel #1
0
        public Question Store(Question question)
        {
            Condition.Requires(question).IsNotNull();

            // First, validate the question.
            ValidateQuestion(question);

            if (question.IdAsANumber > 0)
            {
                var existingQuestion = DocumentSession.Load<Question>(question.Id);
                if (existingQuestion != null)
                {
                    existingQuestion.Subject = question.Subject;
                    existingQuestion.Content = question.Content;
                    existingQuestion.Tags = question.Tags;
                    existingQuestion.CreatedByUserId = question.CreatedByUserId;

                    question = existingQuestion;
                }
            }

            // Save.
            DocumentSession.Store(question);

            return question;
        }
        public void GivenAnQuestionWithInvalidData_Create_StoresAQuestion()
        {
            // Arrange.
            Question question = new Question(); // Empty, invalid question
            var documentSession = new Mock<IDocumentSession>();
            IQuestionService questionService = new QuestionService(documentSession.Object);

            // Act & Assert.
            Assert.Throws<ValidationException>(() => questionService.Create(question));
        }
        public Question Create(Question question, IDocumentSession documentSession)
        {
            Condition.Requires(question).IsNotNull();
            Condition.Requires(documentSession).IsNotNull();

            // First, validate the question.
            Validator.ValidateObject(question, new ValidationContext(question, null, null), true);
            Condition.Requires(question.Tags).IsNotNull().IsLongerThan(0);

            // Save.
            documentSession.Store(question);

            return question;
        }
Beispiel #4
0
        private static void ValidateQuestion(Question question)
        {
            Condition.Requires(question).IsNotNull();

            Condition.Requires(question.Subject)
                .IsNotNullOrEmpty()
                .Evaluate(x => x.Length >= 5, "A subject is required and needs to be at least 5 characters.");

            Condition.Requires(question.Content)
                .IsNotNullOrEmpty()
                .Evaluate(x => x.Length >= 5, "Some content is required and needs to be at least 5 characters.");

            Condition.Requires(question.Tags)
                .IsNotNull()
                .IsNotEmpty("At least one valid tag is required.");
        }