Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
using DtoVsPoco.Domain;

namespace DtoVsPoco.DataStore
namespace DtoVsPoco.DataStore;

public static class FakeDataStore
{
public static class FakeDataStore
public static ICollection<Person> GetPersonDomainObjects()
{
public static ICollection<Person> GetPersonDomainObjects()
{
return new List<Person>
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)
},
];
}
}
46 changes: 24 additions & 22 deletions csharp-classes-struct-record/DtoVsPoco/DtoVsPoco/Domain/Person.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Mapster" Version="7.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Mapster" Version="10.0.14" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using DtoVsPoco.Dtos;

namespace DtoVsPoco.Services
namespace DtoVsPoco.Services;

public interface IPersonService
{
public interface IPersonService
{
Task<ICollection<PersonDetails>> GetPersonDetailsData();
}
Task<ICollection<PersonDetails>> GetPersonDetailsData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICollection<PersonDetails>> GetPersonDetailsData()
{
public async Task<ICollection<PersonDetails>> GetPersonDetailsData()
{
var people = FakeDataStore.GetPersonDomainObjects();
var personDetailsDtos = people.Adapt<ICollection<PersonDetails>>();
var people = FakeDataStore.GetPersonDomainObjects();
var personDetailsDtos = people.Adapt<ICollection<PersonDetails>>();

return await Task.FromResult(personDetailsDtos);
}
return Task.FromResult(personDetailsDtos);
}
}
49 changes: 38 additions & 11 deletions csharp-classes-struct-record/DtoVsPoco/Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -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<PersonDetails>);
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<PersonDetails>);
Assert.NotEmpty(personDetailsData);
}
Assert.True(person.IsAdult());
}
}
}
10 changes: 5 additions & 5 deletions csharp-classes-struct-record/DtoVsPoco/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading