annotate Stocks/Stocks.Service/StocksService.cs @ 110:0185b15e04ba

Test added for multi property validation
author stevenhollidge <stevenhollidge@hotmail.com>
date Sun, 06 May 2012 20:43:37 +0100
parents 399398841fd0
children
rev   line source
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
1 using System;
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
2 using System.Collections.Generic;
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
3 using System.Diagnostics;
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
4 using System.Linq;
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
5 using System.Threading.Tasks;
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
6 using NLog;
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
7 using Stocks.Common;
6
c812bca7b1ac "Restore packages on build" enabled by Nuget 1.6
stevenh7776 stevenhollidge@hotmail.com
parents: 4
diff changeset
8 using Stocks.Common.Core;
2
29ed98d659e9 Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents: 0
diff changeset
9 using Stocks.Common.Events;
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
10 using Stocks.Common.Models;
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
11
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
12 namespace Stocks.Service
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
13 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
14 public class StocksService : IStocksService
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
15 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
16 #region Delegates
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
17
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
18 public delegate void PriceChangedEventHandler(object sender, PriceChangedEventArgs e);
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
19
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
20 #endregion
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
21
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
22 private static readonly Logger Log = LogManager.GetCurrentClassLogger();
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
23
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
24 private readonly IConfigurationService _configurationService;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
25 private readonly IWebClientShim _webClient;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
26 private IList<Company> _companies;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
27 private List<Price> _currentPrices;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
28 private string _serviceUrl;
2
29ed98d659e9 Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents: 0
diff changeset
29
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
30 public StocksService(
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
31 IConfigurationService configurationService,
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
32 IWebClientShim webClientShim)
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
33 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
34 new AssemblyInit();
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
35
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
36 _webClient = webClientShim;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
37 _configurationService = configurationService;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
38 Stats = new SummaryStats();
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
39
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
40 GetCompanyList();
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
41 }
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
42
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
43 public SummaryStats Stats { get; private set; }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
44
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
45 #region IStocksService Members
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
46
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
47 public bool IsActive { get; private set; }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
48
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
49 public void Start()
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
50 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
51 PrepareForServiceStarting();
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
52 Task.Factory.StartNew(DownloadPrices);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
53 }
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
54
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
55 public void Stop()
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
56 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
57 IsActive = false;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
58 Log.Debug("StockService stopped");
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
59 }
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
60
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
61 #endregion
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
62
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
63 private void GetCompanyList()
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
64 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
65 _companies = _configurationService.GetCompanies();
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
66
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
67 string symbolsCsv = _companies.Select(
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
68 c => c.Symbol).Aggregate((c, d) => c + "," + d);
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
69
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
70 _serviceUrl = _configurationService.GetServiceUrl(symbolsCsv);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
71 }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
72
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
73 public event PriceChangedEventHandler PriceChanged;
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
74
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
75 protected virtual void OnPriceChanged(Price price)
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
76 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
77 Stats.PriceChangeEvents++;
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
78
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
79 if (PriceChanged != null)
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
80 PriceChanged(this, new PriceChangedEventArgs {Price = price});
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
81 }
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
82
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
83 private void PrepareForServiceStarting()
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
84 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
85 Log.Debug("StockService starting");
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
86 IsActive = true;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
87 Stats.Reset();
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
88 _currentPrices = new List<Price>(_companies.Count);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
89 }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
90
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
91 private void DownloadPrices()
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
92 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
93 try
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
94 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
95 while (IsActive)
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
96 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
97 Stopwatch timeToDownload;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
98 string webResponse = TimedDelegates.Execute(
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
99 _webClient.DownloadString,
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
100 _serviceUrl,
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
101 out timeToDownload);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
102
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
103 PopulatePricesFromWebResponse(webResponse);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
104 UpdateStats(timeToDownload, webResponse);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
105 }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
106 }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
107 catch (Exception e)
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
108 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
109 Log.Error("Exception during DownloadPrices()");
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
110 Log.Error("Stack Trace {0}: /r/nException Message: {1}", e.StackTrace, e.Message);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
111 Stop();
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
112 }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
113 }
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
114
23
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
115 private void PopulatePricesFromWebResponse(string webResponse)
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
116 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
117 string[] webPrices = webResponse.Split(
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
118 new[] {"\n", "\r\n"},
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
119 StringSplitOptions.RemoveEmptyEntries);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
120
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
121 foreach (string webPriceData in webPrices)
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
122 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
123 Price webPrice = Factory.CreatePrice(webPriceData);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
124 Price localPrice = _currentPrices.Find(x => x.Symbol == webPrice.Symbol);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
125
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
126 if (localPrice == null)
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
127 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
128 _currentPrices.Add(new Price(webPrice.Symbol, webPrice.CurrentPrice, webPrice.PreviousPrice));
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
129 continue;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
130 }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
131
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
132 if (!localPrice.Equals(webPrice))
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
133 UpdateLocalPrice(webPrice, localPrice);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
134 }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
135 }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
136
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
137 private void UpdateLocalPrice(Price webPrice, Price localPrice)
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
138 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
139 localPrice.PreviousPrice = localPrice.CurrentPrice;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
140 localPrice.CurrentPrice = webPrice.CurrentPrice;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
141 OnPriceChanged(localPrice);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
142 }
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
143
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
144 private void UpdateStats(Stopwatch timeToDownload, string webResponse)
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
145 {
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
146 Stats.LastWebRequest.Duration = (int) timeToDownload.ElapsedMilliseconds;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
147 Stats.LastWebRequest.PricesDownloaded = _currentPrices.Count;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
148 Stats.LastWebRequest.Response = webResponse;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
149 Stats.LastWebRequest.Request = _serviceUrl;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
150 Stats.LastWebRequest.SymbolCount = _companies.Count;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
151 Stats.NumberOfRequests++;
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
152 Log.Trace(Stats);
399398841fd0 Working version for Stocks (including loosely coupled components
adminsh@apollo
parents: 8
diff changeset
153 }
2
29ed98d659e9 Adding WebClientShim files
stevenh7776 stevenhollidge@hotmail.com
parents: 0
diff changeset
154 }
0
e5d46bb6cdb0 Initial commit
adminSH stevenhollidge@hotmail.com
parents:
diff changeset
155 }