Skip to content

Commit

Permalink
fix: immutable objects not working with latest mongodb.driver
Browse files Browse the repository at this point in the history
  • Loading branch information
alsami committed May 17, 2022
1 parent cae4544 commit 8551bce
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Covid19Api.AutoMapper;

// ReSharper disable once ClassNeverInstantiated.Global
public class VaccinationStatisticToDtoTypeConverter : ITypeConverter<VaccinationStatistic, VaccinationStatisticDto>
{
public VaccinationStatisticDto Convert(VaccinationStatistic source, VaccinationStatisticDto destination,
Expand Down
39 changes: 4 additions & 35 deletions src/Covid19Api.Domain/CountryStatistic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,12 @@

namespace Covid19Api.Domain;

public class CountryStatistic
public record CountryStatistic(string Country, string? CountryCode, int TotalCases, int NewCases, int TotalDeaths,
int NewDeaths,
int RecoveredCases, int ActiveCases, DateTime FetchedAt)
{
public Guid Id { get; private set; }

public string Country { get; private set; }

public string? CountryCode { get; private set; }

public int TotalCases { get; private set; }

public int NewCases { get; private set; }

public int TotalDeaths { get; private set; }

public int NewDeaths { get; private set; }

public int RecoveredCases { get; private set; }

public int ActiveCases { get; private set; }

public DateTime FetchedAt { get; private set; }

public CountryStatistic(string country, string? countryCode, int totalCases, int newCases, int totalDeaths,
int newDeaths,
int recoveredCases, int activeCases, DateTime fetchedAt)
{
this.Country = country;
this.CountryCode = countryCode;
this.TotalCases = totalCases;
this.NewCases = newCases;
this.TotalDeaths = totalDeaths;
this.NewDeaths = newDeaths;
this.RecoveredCases = recoveredCases;
this.ActiveCases = activeCases;
this.FetchedAt = fetchedAt;
this.Id = this.Generate();
}
public Guid Id => this.Generate();

public bool Empty()
{
Expand Down
44 changes: 4 additions & 40 deletions src/Covid19Api.Domain/CountryStatisticsAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,11 @@

namespace Covid19Api.Domain;

// ReSharper disable UnusedAutoPropertyAccessor.Local
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Local
// ReSharper disable UnusedAutoPropertyAccessor.Global
public class CountryStatisticsAggregate
public record CountryStatisticsAggregate(string Country, string? CountryCode, int Total, int New, int Deaths,
int NewDeaths,
int Recovered, int Active, int Month, int Year)
{
public CountryStatisticsAggregate(string country, string? countryCode, int total, int @new, int deaths,
int newDeaths,
int recovered, int active, int month, int year)
{
this.Country = country;
this.CountryCode = countryCode;
this.Total = total;
this.New = @new;
this.Deaths = deaths;
this.NewDeaths = newDeaths;
this.Recovered = recovered;
this.Active = active;
this.Month = month;
this.Year = year;
this.Id = this.Generate();
}

public Guid Id { get; private set; }

public string Country { get; private set; }

public string? CountryCode { get; private set; }

public int Total { get; private set; }

public int New { get; private set; }

public int Deaths { get; private set; }

public int NewDeaths { get; private set; }

public int Recovered { get; private set; }

public int Active { get; private set; }
public int Month { get; private set; }
public int Year { get; private set; }
public Guid Id => this.Generate();

private Guid Generate()
{
Expand Down
24 changes: 2 additions & 22 deletions src/Covid19Api.Domain/GlobalStatistics.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
using System.Security.Cryptography;
using System.Text;

// ReSharper disable AutoPropertyCanBeMadeGetOnly.Local
// ReSharper disable UnusedAutoPropertyAccessor.Global

namespace Covid19Api.Domain;

public class GlobalStatistics
public record GlobalStatistics(int Total, int Recovered, int Deaths, DateTime FetchedAt)
{
public Guid Id { get; private set; }

public int Total { get; private set; }

public int Recovered { get; private set; }

public int Deaths { get; private set; }

public DateTime FetchedAt { get; private set; }

public GlobalStatistics(int total, int recovered, int deaths, DateTime fetchedAt)
{
this.Total = total;
this.Recovered = recovered;
this.Deaths = deaths;
this.FetchedAt = fetchedAt;
this.Id = this.Generate();
}
public Guid Id => this.Generate();

private Guid Generate()
{
Expand Down
20 changes: 2 additions & 18 deletions src/Covid19Api.Domain/GlobalStatisticsAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,9 @@ namespace Covid19Api.Domain;

// ReSharper disable AutoPropertyCanBeMadeGetOnly.Local
// ReSharper disable UnusedAutoPropertyAccessor.Global
public class GlobalStatisticsAggregate
public record GlobalStatisticsAggregate(int Total, int Recovered, int Deaths, int Month, int Year)
{
public GlobalStatisticsAggregate(int total, int recovered, int deaths, int month, int year)
{
this.Total = total;
this.Recovered = recovered;
this.Deaths = deaths;
this.Month = month;
this.Year = year;
this.Id = this.Generate();
}

public Guid Id { get; private set; }
public int Total { get; private set; }
public int Recovered { get; private set; }
public int Deaths { get; private set; }
public int Month { get; private set; }
public int Year { get; private set; }

public Guid Id => this.Generate();
private Guid Generate()
{
using var hasher = MD5.Create();
Expand Down
16 changes: 1 addition & 15 deletions src/Covid19Api.Domain/VaccinationStatistic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,4 @@

namespace Covid19Api.Domain;

public class VaccinationStatistic
{
public VaccinationStatistic(string country, string countyCode, VaccinationStatisticValue[] values)
{
this.Country = country;
this.CountyCode = countyCode;
this.Values = values;
}

public string Country { get; private set; }

public string CountyCode { get; private set; }

public VaccinationStatisticValue[] Values { get; private set; }
}
public record VaccinationStatistic(string Country, string CountyCode, VaccinationStatisticValue[] Values);
43 changes: 10 additions & 33 deletions src/Covid19Api.Domain/VaccinationStatisticValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,13 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
namespace Covid19Api.Domain;

public class VaccinationStatisticValue
{
public VaccinationStatisticValue(DateTime loggedAt, ulong totalVaccinations, ulong peopleVaccinated, ulong peopleFullyVaccinated, double peopleFullyVaccinatedPerHundred, ulong dailyVaccinations, double totalVaccinationsPerHundred, double peopleVaccinatedPerHundred, uint dailyVaccinationsPerMillion)
{
this.LoggedAt = loggedAt;
this.TotalVaccinations = totalVaccinations;
this.PeopleVaccinated = peopleVaccinated;
this.PeopleFullyVaccinated = peopleFullyVaccinated;
this.PeopleFullyVaccinatedPerHundred = peopleFullyVaccinatedPerHundred;
this.DailyVaccinations = dailyVaccinations;
this.TotalVaccinationsPerHundred = totalVaccinationsPerHundred;
this.PeopleVaccinatedPerHundred = peopleVaccinatedPerHundred;
this.DailyVaccinationsPerMillion = dailyVaccinationsPerMillion;
}

public DateTime LoggedAt { get; private set; }

public ulong TotalVaccinations { get; private set; }

public ulong PeopleVaccinated { get; private set; }

public ulong PeopleFullyVaccinated { get; private set; }

public double PeopleFullyVaccinatedPerHundred { get; private set; }

public ulong DailyVaccinations { get; private set; }

public double TotalVaccinationsPerHundred { get; private set; }

public double PeopleVaccinatedPerHundred { get; private set; }

public uint DailyVaccinationsPerMillion { get; private set; }
}
public record VaccinationStatisticValue(
DateTime LoggedAt,
ulong TotalVaccinations,
ulong PeopleVaccinated,
ulong PeopleFullyVaccinated,
double PeopleFullyVaccinatedPerHundred,
ulong DailyVaccinations,
double TotalVaccinationsPerHundred,
double PeopleVaccinatedPerHundred,
uint DailyVaccinationsPerMillion);
16 changes: 9 additions & 7 deletions src/Covid19Api.Mongo/Conventions/MongoDbConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Covid19Api.Mongo.Conventions;

internal static class MongoDbConventions
{
private static readonly ConventionPack ConventionPack = new();

public static void UseGuidIdConvention(params Type[] typesToExclude)
{
ConventionRegistry.Register(nameof(GuidIdConvention), new ConventionPack
Expand Down Expand Up @@ -32,9 +34,11 @@ public static void UseImmutableConvention()

public static void UseCamelCaseConvention()
{
ConventionRegistry.Register(nameof(CamelCaseElementNameConvention),
new ConventionPack {new CamelCaseElementNameConvention()},
type => true);
ConventionPack.Add(new CamelCaseElementNameConvention());
ConventionRegistry.Register(
nameof(CamelCaseElementNameConvention),
ConventionPack,
_ => true);
}

public static void UseIgnoreNullValuesConvention()
Expand All @@ -45,9 +49,7 @@ public static void UseIgnoreNullValuesConvention()

public static void UseEnumStringRepresentation()
{
ConventionRegistry.Register(nameof(EnumRepresentationConvention), new ConventionPack
{
new EnumRepresentationConvention(BsonType.String)
}, _ => true);
ConventionPack.Add(new EnumRepresentationConvention(BsonType.String));
ConventionRegistry.Register(nameof(EnumRepresentationConvention), ConventionPack, _ => true);
}
}
5 changes: 2 additions & 3 deletions src/Covid19Api.Mongo/Covid19ApiDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ public class Covid19ApiDbContext
{
public Covid19ApiDbContext(Func<IMongoDatabase> databaseComposer)
{
this.Database = databaseComposer();
Configure();
this.Database = databaseComposer();
}

public IMongoDatabase Database { get; }

private static void Configure()
{
MongoDbConventions.UseGuidIdConvention(typeof(VaccinationStatistic));
MongoDbConventions.IgnoreNotMappedPropertiesConvention(typeof(VaccinationStatistic));
MongoDbConventions.UseImmutableConvention();
MongoDbConventions.IgnoreNotMappedPropertiesConvention(typeof(VaccinationStatistic), typeof(CountryStatistic));
MongoDbConventions.UseCamelCaseConvention();
MongoDbConventions.UseIgnoreNullValuesConvention();
MongoDbConventions.UseEnumStringRepresentation();
Expand Down

0 comments on commit 8551bce

Please sign in to comment.