public async Task <IList <int> > Execute(string[] args) { var results = new List <int>(); using (var fibonacciDataContext = new FibonacciDataContext()) { IList <Task <int> > tasks = new List <Task <int> >(); foreach (var arg in args) { var i = int.Parse(arg); var output = await _fibonacciDataContext.TFibonacci.Where(f => f.FibInput == i).Select(f => f.FibOutput).FirstOrDefaultAsync(); Task <int> task; if (output == default) { task = Task.Run(() => Fib(i)); } else { task = Task.FromResult((int)output); } tasks.Add(task); } ; foreach (var task in tasks) { var indexOf = tasks.IndexOf(task); var arg = args[indexOf]; results.Add(task.Result); _fibonacciDataContext.TFibonacci.Add(new TFibonacci() { FibInput = int.Parse(arg), FibOutput = task.Result, FibCreatedTimeStamp = DateTime.Now }); } await _fibonacciDataContext.SaveChangesAsync(); return(results); } }
public Compute(FibonacciDataContext fibonacciDataContext) { _fibonacciDataContext = fibonacciDataContext; }