Exemple #1
0
        static void Main()
        {
            var sensor = new TemperatureSensor();

            using (var db = new TemperatureContext())
            {
                // Clean database before importing data (optional, just to make testing easier)
                db.CleanupTargetDatbaseAsync().Wait();

                while (!Console.KeyAvailable)
                {
                    var temperature = sensor.GetCurrentTemperatureAsync().Result;
                    db.StoreTemperatureAsync(temperature).Wait();
                    Thread.Sleep(1000);
                }
            }
        }
        public static async Task StoreTemperatureAsync(this TemperatureContext db, double temperature)
        {
            var reading = new TemperatureReading
            {
                MeasureDateTime = DateTime.UtcNow,
                Temperature     = temperature
            };

            // Required exercise: Add record to database
            await db.TemperatureReadings.AddAsync(reading);

            // Optional exercise: Add alert to database
            var message = temperature <-10d ? "Too low" : temperature> 25d ? "Too high" : null;

            if (message != null)
            {
                await db.Alerts.AddAsync(new Alert { Message = message, TemperatureReading = reading });
            }

            await db.SaveChangesAsync();
        }
        // The following method was not requested in the test. I just added it
        // to make testing a little bit easier.
        public static async Task CleanupTargetDatbaseAsync(this TemperatureContext db)
        {
            await db.Database.ExecuteSqlCommandAsync("DELETE FROM dbo.TemperatureReadings");

            await db.Database.ExecuteSqlCommandAsync("DELETE FROM dbo.Alerts");
        }