diff --git a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/DataStore/FakeDataStore.cs b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/DataStore/FakeDataStore.cs index bab0c2c190..215cbdccb5 100644 --- a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/DataStore/FakeDataStore.cs +++ b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/DataStore/FakeDataStore.cs @@ -1,38 +1,37 @@ using DtoVsPoco.Domain; -namespace DtoVsPoco.DataStore +namespace DtoVsPoco.DataStore; + +public static class FakeDataStore { - public static class FakeDataStore + public static ICollection GetPersonDomainObjects() { - public static ICollection GetPersonDomainObjects() - { - return new List + return + [ + new Person(1) + { + FirstName = "Bruce", + LastName = "Wayne", + Address = "DC Street 22", + JobTitle = "Batman", + DateOfBirth = new DateTime(1972, 7, 8) + }, + new Person(2) + { + FirstName = "Tony", + LastName = "Stark", + Address = "Marvel Street 9", + JobTitle = "Iron-Man", + DateOfBirth = new DateTime(1968, 5, 5) + }, + new Person(3) { - new Person(1) - { - FirstName = "Bruce", - LastName = "Wayne", - Address = "DC Street 22", - JobTitle = "Batman", - DateOfBirth = new DateTime(1972, 7, 8) - }, - new Person(2) - { - FirstName = "Tony", - LastName = "Stark", - Address = "Marvel Street 9", - JobTitle = "Iron-Man", - DateOfBirth = new DateTime(1968, 5, 5) - }, - new Person(3) - { - FirstName = "Peter", - LastName = "Parker", - Address = "Marvel Street 87", - JobTitle = "Spider-Man", - DateOfBirth = new DateTime(1992, 2, 4) - }, - }; - } + FirstName = "Peter", + LastName = "Parker", + Address = "Marvel Street 87", + JobTitle = "Spider-Man", + DateOfBirth = new DateTime(1992, 2, 4) + }, + ]; } } diff --git a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Domain/Person.cs b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Domain/Person.cs index ac3bcc3568..3ac8877b3e 100644 --- a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Domain/Person.cs +++ b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Domain/Person.cs @@ -1,30 +1,32 @@ -namespace DtoVsPoco.Domain +namespace DtoVsPoco.Domain; + +public class Person { - public class Person + public Person(int id) { - public Person(int id) - { - Id = id; - } + Id = id; + } + + public int Id { get; private init; } + public string? FirstName { get; set; } + public string? LastName { get; set; } + public string? Address { get; set; } + public DateTime DateOfBirth { get; set; } + public string? JobTitle { get; set; } - public int Id { get; private init; } - public string? FirstName { get; set; } - public string? LastName { get; set; } - public string? Address { get; set; } - public DateTime DateOfBirth { get; set; } - public string? JobTitle { get; set; } + public string DoWork() + { + return $"{JobTitle} doing his work!"; + } - public string DoWork() - { - return $"{JobTitle} doing his work!"; - } + public bool IsAdult() + { + var today = DateTime.Today; + var age = today.Year - DateOfBirth.Year; - public bool IsAdult() - { - var currentYear = DateTime.Now.Year; - var age = currentYear - DateOfBirth.Year; + if (DateOfBirth.Date > today.AddYears(-age)) + age--; - return age >= 21; - } + return age >= 21; } } diff --git a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/DtoVsPoco.csproj b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/DtoVsPoco.csproj index 3061c754e7..3bd49b4b51 100644 --- a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/DtoVsPoco.csproj +++ b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/DtoVsPoco.csproj @@ -1,14 +1,13 @@ - net6.0 + net11.0 enable enable - - + diff --git a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Dtos/PersonDetails.cs b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Dtos/PersonDetails.cs index 9cd280ef21..0b625160a0 100644 --- a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Dtos/PersonDetails.cs +++ b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Dtos/PersonDetails.cs @@ -1,4 +1,3 @@ -namespace DtoVsPoco.Dtos -{ - public record PersonDetails(string FirstName, string LastName, string Address, DateTime DateOfBirth); -} +namespace DtoVsPoco.Dtos; + +public record PersonDetails(string FirstName, string LastName, string Address, DateTime DateOfBirth); diff --git a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Properties/launchSettings.json b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Properties/launchSettings.json index 431d5d8cbb..9d56f4a0b0 100644 --- a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Properties/launchSettings.json +++ b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Properties/launchSettings.json @@ -1,30 +1,14 @@ { - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:52797", - "sslPort": 44379 - } - }, + "$schema": "https://json.schemastore.org/launchsettings.json", "profiles": { "DtoVsPoco": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, - "launchUrl": "swagger", + "launchBrowser": false, "applicationUrl": "https://localhost:5000;http://localhost:5001", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "swagger", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } } } } diff --git a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Services/IPersonService.cs b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Services/IPersonService.cs index 2206c7f1dd..67ef9557e6 100644 --- a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Services/IPersonService.cs +++ b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Services/IPersonService.cs @@ -1,9 +1,8 @@ using DtoVsPoco.Dtos; -namespace DtoVsPoco.Services +namespace DtoVsPoco.Services; + +public interface IPersonService { - public interface IPersonService - { - Task> GetPersonDetailsData(); - } + Task> GetPersonDetailsData(); } diff --git a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Services/PersonService.cs b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Services/PersonService.cs index 63c7fbb76f..4ac85593f3 100644 --- a/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Services/PersonService.cs +++ b/csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Services/PersonService.cs @@ -2,16 +2,15 @@ using DtoVsPoco.Dtos; using Mapster; -namespace DtoVsPoco.Services +namespace DtoVsPoco.Services; + +public class PersonService : IPersonService { - public class PersonService: IPersonService + public Task> GetPersonDetailsData() { - public async Task> GetPersonDetailsData() - { - var people = FakeDataStore.GetPersonDomainObjects(); - var personDetailsDtos = people.Adapt>(); + var people = FakeDataStore.GetPersonDomainObjects(); + var personDetailsDtos = people.Adapt>(); - return await Task.FromResult(personDetailsDtos); - } + return Task.FromResult(personDetailsDtos); } } diff --git a/csharp-classes-struct-record/DtoVsPoco/Tests/Tests.cs b/csharp-classes-struct-record/DtoVsPoco/Tests/Tests.cs index a26943980e..133dd01866 100644 --- a/csharp-classes-struct-record/DtoVsPoco/Tests/Tests.cs +++ b/csharp-classes-struct-record/DtoVsPoco/Tests/Tests.cs @@ -1,22 +1,49 @@ +using DtoVsPoco.Domain; using DtoVsPoco.Dtos; using DtoVsPoco.Services; +using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Xunit; -namespace Tests +namespace Tests; + +public class Tests { - public class Tests + private readonly IPersonService _personService = new PersonService(); + + [Fact] + public async Task WhenGettingPersonDetailsData_ThenReturnNotEmptyCollectionOfPersonDetailsDtos() { - private readonly IPersonService _personService = new PersonService(); + var personDetailsData = await _personService.GetPersonDetailsData(); + + Assert.True(personDetailsData is ICollection); + Assert.NotEmpty(personDetailsData); + } - [Fact] - public async Task WhenGettingPersonDetailsData_ThenReturnNotEmptyCollectionOfPersonDetailsDtos() - { - var personDetailsData = await _personService.GetPersonDetailsData(); + [Fact] + public async Task WhenGettingPersonDetailsData_ThenPocoValuesAreCopiedToDtos() + { + var personDetailsData = await _personService.GetPersonDetailsData(); + var first = personDetailsData.First(); + + Assert.Equal(new PersonDetails("Bruce", "Wayne", "DC Street 22", new DateTime(1972, 7, 8)), first); + } + + [Fact] + public void GivenPersonWhoTurns21Tomorrow_WhenCheckingIsAdult_ThenReturnFalse() + { + var person = new Person(4) { DateOfBirth = DateTime.Today.AddYears(-21).AddDays(1) }; + + Assert.False(person.IsAdult()); + } + + [Fact] + public void GivenPersonWhoTurns21Today_WhenCheckingIsAdult_ThenReturnTrue() + { + var person = new Person(5) { DateOfBirth = DateTime.Today.AddYears(-21) }; - Assert.True(personDetailsData is ICollection); - Assert.NotEmpty(personDetailsData); - } + Assert.True(person.IsAdult()); } -} \ No newline at end of file +} diff --git a/csharp-classes-struct-record/DtoVsPoco/Tests/Tests.csproj b/csharp-classes-struct-record/DtoVsPoco/Tests/Tests.csproj index 7998b95a41..e5f937e2c7 100644 --- a/csharp-classes-struct-record/DtoVsPoco/Tests/Tests.csproj +++ b/csharp-classes-struct-record/DtoVsPoco/Tests/Tests.csproj @@ -1,20 +1,20 @@ - net6.0 + net11.0 enable false - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all