диплом целиком (1218792), страница 11
Текст из файла (страница 11)
public WeatherRecordNotFoundException() { }
public WeatherRecordNotFoundException(string message)
: base(message) { }
public WeatherRecordNotFoundException(string message, Exception innerException)
: base(message, innerException) { }
protected WeatherRecordNotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}
}
WeatherServiceException.cs
using System;
using System.Runtime.Serialization;
namespace WeatherAnalysis.Core.Exceptions
{
public class WeatherServiceException : WeatherAnalysisException
{
public WeatherServiceException() { }
public WeatherServiceException(string message) : base(message) { }
public WeatherServiceException(string message, Exception innerException) : base(message, innerException) { }
protected WeatherServiceException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}
WeatherAnalysis.Core.Logic
FireHazardReportBuilder.cs
using System;
using WeatherAnalysis.Core.Data;
using WeatherAnalysis.Core.Model;
namespace WeatherAnalysis.Core.Logic
{
public sealed class FireHazardReportBuilder
{
private readonly IWeatherRecordManager _weatherRecordManager;
public FireHazardReportBuilder(IWeatherRecordManager weatherRecordManager)
{
_weatherRecordManager = weatherRecordManager;
}
public FireHazardReport BuildReport(WeatherRecord record)
{
if (record == null) throw new ArgumentNullException("record");
if (record.Location == null) throw new ArgumentException("record.Location");
if (record.LocationId == null) throw new ArgumentException("record.LocationId");
var lastRainyWeather = _weatherRecordManager.GetLastRainyDay(record.LocationId.Value, record.Created.AddMonths(-2), record.Created.Date);
var daysWithoutRain = DaysWithoutRain(record, lastRainyWeather);
var report = new FireHazardReport
{
Created = DateTime.Now,
WeatherRecordId = record.Id,
Weather = record,
LocationId = record.LocationId,
Location = record.Location,
LastRainyDate = lastRainyWeather,
FireHazardCoefficient = CalculateFireHazardCoefficient(record, daysWithoutRain)
};
return report;
}
private static decimal CalculateFireHazardCoefficient(WeatherRecord record, int daysWithoutRain)
{
return daysWithoutRain * (record.Temperature - record.DewPoint) * record.Temperature;
}
private static int DaysWithoutRain(WeatherRecord currentWeather, DateTime lastRainyDay)
{
return Convert.ToInt32(Math.Ceiling((currentWeather.Created - lastRainyDay).TotalDays));
}
}
}
WeatherAnalysis.Core.Model
FireHazardReport.cs
using System;
using LinqToDB.Mapping;
namespace WeatherAnalysis.Core.Model
{
[Table(Name = "FireHazardReports")]
public sealed class FireHazardReport
{
[PrimaryKey, Identity]
public int? Id { get; set; }
[PrimaryKey, NotNull]
public int? WeatherRecordId { get; set; }
[Association(CanBeNull = false, ThisKey = "WeatherRecordId", OtherKey = "Id")]
public WeatherRecord Weather { get; set; }
[PrimaryKey, NotNull]
public int? LocationId { get; set; }
[Association(CanBeNull = false, ThisKey = "LocationId", OtherKey = "Id")]
public Location Location { get; set; }
[Column]
public DateTime Created { get; set; }
[Column]
public decimal FireHazardCoefficient { get; set; }
[Column]
public DateTime LastRainyDate { get; set; }
[Column]
public string SignedBy { get; set; }
}
}
Location.cs
using System;
using LinqToDB.Mapping;
namespace WeatherAnalysis.Core.Model
{
[Table(Name = "Locations")]
public sealed class Location
{
[PrimaryKey, Identity]
public int? Id { get; set; }
[Column, NotNull]
public string Name
{
get { return _name; }
set
{
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException();
_name = value;
}
}
[Column, NotNull]
public string SystemName
{
get { return _systemName; }
set
{
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException();
_systemName = value;
}
}
[NotColumn]
public int WeatherRecordsCount { get; set; }
[NotColumn]
public int FireHazardReportsCount { get; set; }
private string _name;
private string _systemName;
}
}
WeatherRecord.cs
using System;
using LinqToDB.Mapping;
namespace WeatherAnalysis.Core.Model
{
[Table(Name = "WeatherRecords")]
public sealed class WeatherRecord
{
[PrimaryKey, Identity]
public int? Id { get; set; }
[PrimaryKey, NotNull]
public int? LocationId { get; set; }
[Association(CanBeNull = false, ThisKey = "LocationId", OtherKey = "Id")]
public Location Location { get; set; }
[Column]
public DateTime Created { get; set; }
[Column]
public decimal Temperature { get; set; }
[Column]
public decimal Humidity
{
get
{
return _humidity;
}
set
{
if (value < 0.0M || value > 100.0M)
throw new ArgumentOutOfRangeException();
_humidity = value;
}
}
[NotColumn]
public decimal DewPoint
{
get
{
var tau = AlphaCoefficient*Temperature/(BetaCoefficient + Temperature);
if (Humidity > 0)
tau += (decimal)Math.Log((double)Humidity / 100);
var dewPoint = BetaCoefficient * tau / (AlphaCoefficient - tau);
return dewPoint;
}
}
[Column]
public decimal Precipitation
{
get { return _precipitation; }
set
{
if (value < 0) throw new ArgumentOutOfRangeException("Precipitation cannot be below 0.");
_precipitation = value;
}
}
[NotColumn]
public bool Rainy
{
get { return Precipitation >= 3; }
}
private decimal _humidity;
private decimal _precipitation;
private const decimal AlphaCoefficient = 17.27M;
private const decimal BetaCoefficient = 237.7M;
}
}
WeatherAnalysis.Core.Service
IWeatherService.cs
using System;
using System.Collections.Generic;
using WeatherAnalysis.Core.Model;
namespace WeatherAnalysis.Core.Service
{
public interface IWeatherService
{
IReadOnlyCollection<WeatherRecord> GetWeatherData(Location location, DateTime from, DateTime to);
}
}
WeatherAnalysis.Core.Data.Sql
DbManager.cs
using System.Data.Common;
using System.Linq;
using LinqToDB;
using LinqToDB.Data;
using WeatherAnalysis.Core.Model;
namespace WeatherAnalysis.Core.Data.Sql
{
public sealed class DbManager : IDbManager
{
private readonly string _configurationString;
public DbManager(string configurationString)
{
_configurationString = configurationString;
}
public bool HasValidSchema()
{
using (var db = new DataConnection(_configurationString))
{
try
{
db.GetTable<Location>().FirstOrDefault();
db.GetTable<WeatherRecord>().FirstOrDefault();
db.GetTable<FireHazardReport>().FirstOrDefault();
return true;
}
catch (DbException)
{
return false;
}
}
}
public void CreateSchema()
{
using (var db = new DataConnection(_configurationString))
{
CreateIfNotExists<Location>(db);
CreateIfNotExists<WeatherRecord>(db);
CreateIfNotExists<FireHazardReport>(db);
}
}
public void CleanUp()
{
using (var db = new DataConnection(_configurationString))
{
DropIfExists<Location>(db);
DropIfExists<WeatherRecord>(db);
DropIfExists<FireHazardReport>(db);
}
}
private static void CreateIfNotExists<T>(DataConnection db) where T : class
{
if (!TableExists<T>(db))
db.CreateTable<T>();
}
private static void DropIfExists<T>(DataConnection db) where T : class
{
if (TableExists<T>(db))
db.DropTable<T>();
}
private static bool TableExists<T>(DataConnection db) where T : class
{
try
{
db.GetTable<T>().Any();
return true;
}
catch (DbException)
{
return false;
}
}
}
}
FireHazardReportManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using LinqToDB;
using LinqToDB.Data;
using WeatherAnalysis.Core.Model;
namespace WeatherAnalysis.Core.Data.Sql
{
public class FireHazardReportManager : IFireHazardReportManager
{
private readonly string _configurationString;
public FireHazardReportManager(string configurationString)
{
_configurationString = configurationString;
}
public IReadOnlyCollection<FireHazardReport> Get(int locationId, DateTime from, DateTime to)
{
using (var db = new DataConnection(_configurationString))
{
var reports = db.GetTable<FireHazardReport>()
.LoadWith(r => r.Location)
.LoadWith(r => r.Weather)
.Select(r => r)
.Where(r => r.Location.Id == locationId)
.Where(r => r.Created >= from)
.Where(r => r.Created <= to);
return reports.OrderByDescending(r => r.Created).ToArray();
}
}
public void Save(FireHazardReport report)
{
using (var db = new DataConnection(_configurationString))
{
if (report.Id.HasValue)
{
db.Update(report);
}
else
{
report.Id = Convert.ToInt32(db.InsertWithIdentity(report));
}
}
}
public void Delete(FireHazardReport report)
{
using (var db = new DataConnection(_configurationString))
{
db.Delete(report);
}
}
}
}
LocationManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using LinqToDB;
using LinqToDB.Data;
using WeatherAnalysis.Core.Exceptions;
using WeatherAnalysis.Core.Model;
namespace WeatherAnalysis.Core.Data.Sql
{
public sealed class LocationManager : ILocationManager
{
private readonly string _configurationString;
public LocationManager(string configurationString)
{
_configurationString = configurationString;
}
public IReadOnlyCollection<Location> GetAll()
{
using (var db = new DataConnection(_configurationString))
{
var locations = CreateQuery(db);
return locations.ToArray();
}
}
public IReadOnlyCollection<Location> Get(string namePart)
{
if (string.IsNullOrWhiteSpace(namePart)) return Enumerable.Empty<Location>().ToArray();
using (var db = new DataConnection(_configurationString))
{
var locations = CreateQuery(db).Where(location => location.Name.Contains(namePart));
return locations.ToArray();
}
}
private static IQueryable<Location> CreateQuery(DataConnection db)
{
var locations =
from location in db.GetTable<Location>()
join report in db.GetTable<FireHazardReport>() on location.Id equals report.LocationId into reports
join record in db.GetTable<WeatherRecord>() on location.Id equals record.LocationId into records
select new Location
{
Id = location.Id,
Name = location.Name,
SystemName = location.SystemName,
FireHazardReportsCount = reports.Count(),
WeatherRecordsCount = records.Count()
};
return locations;
}
public void Save(Location location)
{
var exist = Get(location.Name).FirstOrDefault();
if (exist != null) throw new WeatherAnalysisException("Населенный пункт с таким именем уже существует.");
using (var db = new DataConnection(_configurationString))
{
if (location.Id.HasValue)
















