PageRenderTime 15ms CodeModel.GetById 10ms app.highlight 2ms RepoModel.GetById 1ms 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//===================================================================================
17using System;
18using System.Collections.Generic;
19using System.Globalization;
20using System.Linq;
21using System.Xml.Linq;
22using StockTraderRI.Infrastructure.Interfaces;
23using StockTraderRI.Infrastructure.Models;
24using StockTraderRI.Modules.News.Properties;
25using System.ComponentModel.Composition;
26
27namespace StockTraderRI.Modules.News.Services
28{
29    [Export(typeof(INewsFeedService))]
30    [PartCreationPolicy(CreationPolicy.Shared)]
31    public class NewsFeedService : INewsFeedService
32    {
33        readonly Dictionary<string, List<NewsArticle>> newsData = new Dictionary<string, List<NewsArticle>>();
34
35        public NewsFeedService()
36        {
37            var document = XDocument.Parse(Resources.News);
38            newsData = document.Descendants("NewsItem")
39                .GroupBy(x => x.Attribute("TickerSymbol").Value,
40                x => new NewsArticle
41                {
42                    PublishedDate = DateTime.Parse(x.Attribute("PublishedDate").Value, CultureInfo.InvariantCulture),
43                    Title = x.Element("Title").Value,
44                    Body = x.Element("Body").Value,
45                    IconUri = x.Attribute("IconUri") != null ? x.Attribute("IconUri").Value : null
46                })
47                .ToDictionary(group => group.Key, group => group.ToList());
48        }
49
50        #region INewsFeed Members
51
52        public IList<NewsArticle> GetNews(string tickerSymbol)
53        {
54            List<NewsArticle> articles = new List<NewsArticle>();
55            newsData.TryGetValue(tickerSymbol, out articles);
56            return articles;
57        }
58
59        public event EventHandler<NewsFeedEventArgs> Updated = delegate { };
60
61        public bool HasNews(string tickerSymbol)
62        {
63            return newsData.ContainsKey(tickerSymbol);
64        }
65
66        #endregion
67    }
68}