コード例 #1
0
        public Quote GetRandomQuote(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return(DefaultQuoteGenerator.GetDefaultQuote());
            }

            string text, author;

            int randomIndex;

            try
            {
                XDocument doc = _ixDocumentLoader.LoadXDocument(filePath);

                randomIndex = RandomNumberGenerator.GetRandomNumber(1, doc.Descendants("quote").Count());

                var xElement = doc.Element("quotes");

                List <XElement> quoteDescendants = xElement.Descendants("quote").ToList();

                text = quoteDescendants.ElementAt(randomIndex).Element("text").Value;

                author = quoteDescendants.ElementAt(randomIndex).Element("author").Value;
            }
            catch (NullReferenceException)
            {
                return(DefaultQuoteGenerator.GetDefaultQuote());
            }

            var quote = new Quote(randomIndex + 1, text, author);

            return(quote);
        }
コード例 #2
0
        public QuoteRepository(IXDocumentLoader ixDocumentLoader)
        {
            _ixDocumentLoader = ixDocumentLoader;

            RandomNumberGenerator = new RandomNumberGenerator();
            DefaultQuoteGenerator = new DefaultQuoteGenerator();
        }