PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/StockTrader RI/Desktop/StockTraderRI.Modules.News/Services/NewsFeedService.cs

#
C# | 68 lines | 45 code | 7 blank | 16 comment | 1 complexity | e5a11cbdc14e000dec677bb8fa3794bb MD5 | raw file
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Globalization;
  20. using System.Linq;
  21. using System.Xml.Linq;
  22. using StockTraderRI.Infrastructure.Interfaces;
  23. using StockTraderRI.Infrastructure.Models;
  24. using StockTraderRI.Modules.News.Properties;
  25. using System.ComponentModel.Composition;
  26. namespace StockTraderRI.Modules.News.Services
  27. {
  28. [Export(typeof(INewsFeedService))]
  29. [PartCreationPolicy(CreationPolicy.Shared)]
  30. public class NewsFeedService : INewsFeedService
  31. {
  32. readonly Dictionary<string, List<NewsArticle>> newsData = new Dictionary<string, List<NewsArticle>>();
  33. public NewsFeedService()
  34. {
  35. var document = XDocument.Parse(Resources.News);
  36. newsData = document.Descendants("NewsItem")
  37. .GroupBy(x => x.Attribute("TickerSymbol").Value,
  38. x => new NewsArticle
  39. {
  40. PublishedDate = DateTime.Parse(x.Attribute("PublishedDate").Value, CultureInfo.InvariantCulture),
  41. Title = x.Element("Title").Value,
  42. Body = x.Element("Body").Value,
  43. IconUri = x.Attribute("IconUri") != null ? x.Attribute("IconUri").Value : null
  44. })
  45. .ToDictionary(group => group.Key, group => group.ToList());
  46. }
  47. #region INewsFeed Members
  48. public IList<NewsArticle> GetNews(string tickerSymbol)
  49. {
  50. List<NewsArticle> articles = new List<NewsArticle>();
  51. newsData.TryGetValue(tickerSymbol, out articles);
  52. return articles;
  53. }
  54. public event EventHandler<NewsFeedEventArgs> Updated = delegate { };
  55. public bool HasNews(string tickerSymbol)
  56. {
  57. return newsData.ContainsKey(tickerSymbol);
  58. }
  59. #endregion
  60. }
  61. }