-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add persistence layer (MSSQL) to WeatherForecast example
- Loading branch information
1 parent
78cf9a3
commit 4dab255
Showing
16 changed files
with
269 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
# Autodetect text files | ||
* text=auto | ||
# Definitively text files | ||
# Definitively text files | ||
*.cs text | ||
*.cake text | ||
|
||
# Git lfs | ||
*.crt filter=lfs diff=lfs merge=lfs -text | ||
*.png filter=lfs diff=lfs merge=lfs -text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project> | ||
<ItemGroup> | ||
<PackageReference Update="JetBrains.Annotations" Version="2022.1.0" /> | ||
<PackageReference Update="Microsoft.Fast.Components.FluentUI" Version="1.5.3" /> | ||
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
<PackageReference Update="Selenium.WebDriver.ChromeDriver" Version="106.0.5249.6100" /> | ||
<PackageReference Update="Selenium.WebDriver" Version="4.5.1" /> | ||
<PackageReference Update="xunit.runner.visualstudio" Version="2.4.5" /> | ||
<PackageReference Update="xunit" Version="2.4.2" /> | ||
<PackageReference Update="JetBrains.Annotations" Version="2022.1.0"/> | ||
<PackageReference Update="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.10"/> | ||
<PackageReference Update="Microsoft.EntityFrameworkCore" Version="6.0.10"/> | ||
<PackageReference Update="Microsoft.Fast.Components.FluentUI" Version="1.5.3"/> | ||
<PackageReference Update="System.ComponentModel.Annotations" Version="5.0.0"/> | ||
<PackageReference Update="System.Text.Json" Version="6.0.6"/> | ||
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.3.2"/> | ||
<PackageReference Update="Selenium.WebDriver.ChromeDriver" Version="106.0.5249.6100"/> | ||
<PackageReference Update="Selenium.WebDriver" Version="4.5.1"/> | ||
<PackageReference Update="xunit.runner.visualstudio" Version="2.4.5"/> | ||
<PackageReference Update="xunit" Version="2.4.2"/> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
examples/WeatherForecast/src/WeatherForecast.Contexts/WeatherDataContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Linq; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore; | ||
using WeatherForecast.Entities; | ||
|
||
namespace WeatherForecast.Contexts; | ||
|
||
[PublicAPI] | ||
public sealed class WeatherDataContext : DbContext | ||
{ | ||
public WeatherDataContext(DbContextOptions<WeatherDataContext> options) : base(options) | ||
{ | ||
} | ||
|
||
public DbSet<WeatherData> WeatherData { get; set; } = null!; | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
// Map all read-only properties. There is not [Mapped] attribute like [NotMapped]. | ||
modelBuilder.Entity<WeatherData>().Property(weatherData => weatherData.Id); | ||
modelBuilder.Entity<WeatherData>().Property(weatherData => weatherData.Date); | ||
modelBuilder.Entity<Temperature>().Property(temperature => temperature.Id); | ||
modelBuilder.Entity<Temperature>().Property(temperature => temperature.UnitName); | ||
modelBuilder.Entity<Temperature>().Property(temperature => temperature.UnitSymbol); | ||
modelBuilder.Entity<Temperature>().Property(temperature => temperature.Value); | ||
modelBuilder.Entity<Temperature>().Property(temperature => temperature.Measured); | ||
modelBuilder.Entity<WeatherData>().HasMany(weatherData => weatherData.Temperatures).WithOne().HasForeignKey(temperature => temperature.BelongsTo); | ||
|
||
var weatherDataSeed = Enumerable.Range(0, 30).Select(_ => Guid.NewGuid()).Select((id, day) => new WeatherData(id, DateTime.Today.AddDays(day))).ToList(); | ||
var temperatureSeed = weatherDataSeed.SelectMany(data => Enumerable.Range(0, 23).Select(hour => Temperature.Celsius(data.Id, Random.Shared.Next(-10, 30), data.Date.AddHours(hour)))).ToList(); | ||
|
||
modelBuilder.Entity<Temperature>().HasData(temperatureSeed); | ||
modelBuilder.Entity<WeatherData>().HasData(weatherDataSeed); | ||
base.OnModelCreating(modelBuilder); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
examples/WeatherForecast/src/WeatherForecast.Contexts/WeatherDataReadOnlyContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore; | ||
using WeatherForecast.Entities; | ||
using WeatherForecast.Repositories; | ||
|
||
namespace WeatherForecast.Contexts; | ||
|
||
[PublicAPI] | ||
public sealed class WeatherDataReadOnlyContext : IWeatherDataReadOnlyRepository | ||
{ | ||
private readonly WeatherDataContext _context; | ||
|
||
public WeatherDataReadOnlyContext(WeatherDataContext context) | ||
{ | ||
_context = context; | ||
_context.Database.EnsureCreated(); | ||
} | ||
|
||
public Task<IEnumerable<WeatherData>> GetAllAsync() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<IEnumerable<WeatherData>> GetAllAsync(string latitude, string longitude, DateTime from, DateTime to) | ||
{ | ||
return Task.FromResult<IEnumerable<WeatherData>>(_context.WeatherData.Include(property => property.Temperatures).OrderBy(weatherData => weatherData.Date).Take(to.Subtract(from).Days)); | ||
} | ||
|
||
public Task<WeatherData> GetAsync(Guid id) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
examples/WeatherForecast/src/WeatherForecast.Contexts/WeatherDataWriteOnlyContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
using WeatherForecast.Entities; | ||
using WeatherForecast.Repositories; | ||
|
||
namespace WeatherForecast.Contexts; | ||
|
||
[PublicAPI] | ||
public sealed class WeatherDataWriteOnlyContext : IWeatherDataWriteOnlyRepository | ||
{ | ||
public WeatherDataWriteOnlyContext(WeatherDataContext context) | ||
{ | ||
_ = context; | ||
} | ||
|
||
public Task CreateAsync(WeatherData weatherData) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task UpdateAsync(WeatherData weatherData) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task DeleteAsync(WeatherData weatherData) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/WeatherForecast/src/WeatherForecast.Contexts/WeatherForecast.Contexts.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Sdk Name="Microsoft.Build.CentralPackageVersions" Version="2.1.3"/> | ||
<PropertyGroup> | ||
<TargetFrameworks>net6.0</TargetFrameworks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="JetBrains.Annotations"/> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)src/WeatherForecast.Repositories/WeatherForecast.Repositories.csproj"/> | ||
</ItemGroup> | ||
</Project> |
20 changes: 20 additions & 0 deletions
20
examples/WeatherForecast/src/WeatherForecast.Entities/HasId.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Text.Json.Serialization; | ||
using JetBrains.Annotations; | ||
|
||
namespace WeatherForecast.Entities; | ||
|
||
[PublicAPI] | ||
public abstract class HasId | ||
{ | ||
[JsonConstructor] | ||
public HasId(Guid id) | ||
{ | ||
Id = id; | ||
} | ||
|
||
[Key] | ||
[JsonPropertyName("id")] | ||
public Guid Id { get; } | ||
} |
42 changes: 27 additions & 15 deletions
42
examples/WeatherForecast/src/WeatherForecast.Entities/Temperature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,51 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
using JetBrains.Annotations; | ||
|
||
namespace WeatherForecast.Entities; | ||
|
||
[PublicAPI] | ||
public readonly struct Temperature | ||
public sealed class Temperature : HasId | ||
{ | ||
private Temperature(string unitName, string unitSymbol, double value, DateTime measured) | ||
[JsonConstructor] | ||
public Temperature(Guid id, Guid belongsTo, string unitName, string unitSymbol, double value, DateTime measured) : base(id) | ||
{ | ||
BelongsTo = belongsTo; | ||
UnitName = unitName; | ||
UnitSymbol = unitSymbol; | ||
Value = value; | ||
Measured = measured; | ||
} | ||
|
||
public static Temperature Kelvin(double value, DateTime measured) | ||
{ | ||
return new Temperature("Kelvin", "K", value, measured); | ||
} | ||
|
||
public static Temperature Celsius(double value, DateTime measured) | ||
{ | ||
return new Temperature("degree Celsius", "°C", value, measured); | ||
} | ||
public static Temperature AbsoluteZero { get; } = Temperature.Kelvin(Guid.Empty, 0, DateTime.MinValue); | ||
|
||
public static Temperature Fahrenheit(double value, DateTime measured) | ||
{ | ||
return new Temperature("degree Fahrenheit", "°F", value, measured); | ||
} | ||
[JsonPropertyName("belongsTo")] | ||
public Guid BelongsTo { get; } | ||
|
||
[JsonPropertyName("unitName")] | ||
public string UnitName { get; } | ||
|
||
[JsonPropertyName("unitSymbol")] | ||
public string UnitSymbol { get; } | ||
|
||
[JsonPropertyName("value")] | ||
public double Value { get; } | ||
|
||
[JsonPropertyName("measured")] | ||
public DateTime Measured { get; } | ||
|
||
public static Temperature Kelvin(Guid belongsTo, double value, DateTime measured) | ||
{ | ||
return new Temperature(Guid.NewGuid(), belongsTo, "Kelvin", "K", value, measured); | ||
} | ||
|
||
public static Temperature Celsius(Guid belongsTo, double value, DateTime measured) | ||
{ | ||
return new Temperature(Guid.NewGuid(), belongsTo, "degree Celsius", "°C", value, measured); | ||
} | ||
|
||
public static Temperature Fahrenheit(Guid belongsTo, double value, DateTime measured) | ||
{ | ||
return new Temperature(Guid.NewGuid(), belongsTo, "degree Fahrenheit", "°F", value, measured); | ||
} | ||
} |
22 changes: 16 additions & 6 deletions
22
examples/WeatherForecast/src/WeatherForecast.Entities/WeatherData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json.Serialization; | ||
using JetBrains.Annotations; | ||
|
||
namespace WeatherForecast.Entities; | ||
|
||
[PublicAPI] | ||
public readonly struct WeatherData | ||
public sealed class WeatherData : HasId | ||
{ | ||
public WeatherData(DateTime date, IEnumerable<Temperature> measurements) | ||
public WeatherData(Guid id, DateTime date) : this(id, date, new List<Temperature>()) | ||
{ | ||
// Entity Framework constructor. | ||
} | ||
|
||
[JsonConstructor] | ||
public WeatherData(Guid id, DateTime date, IList<Temperature> temperatures) : base(id) | ||
{ | ||
IReadOnlyCollection<Temperature> temperatures = measurements.ToList(); | ||
Date = date; | ||
Minimum = temperatures.OrderBy(temperature => temperature.Value).FirstOrDefault(); | ||
Maximum = temperatures.OrderBy(temperature => temperature.Value).LastOrDefault(); | ||
Minimum = temperatures.OrderBy(temperature => temperature.Value).DefaultIfEmpty(Temperature.AbsoluteZero).First(); | ||
Maximum = temperatures.OrderBy(temperature => temperature.Value).DefaultIfEmpty(Temperature.AbsoluteZero).Last(); | ||
Temperatures = temperatures; | ||
} | ||
|
||
[JsonPropertyName("date")] | ||
public DateTime Date { get; } | ||
|
||
[JsonIgnore] | ||
public Temperature Minimum { get; } | ||
|
||
[JsonIgnore] | ||
public Temperature Maximum { get; } | ||
|
||
public IEnumerable<Temperature> Temperatures { get; } | ||
[JsonPropertyName("temperatures")] | ||
public IList<Temperature> Temperatures { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.