Skip to content

Commit 071b7bc

Browse files
authored
Adding Vogen Value Objects to Sample (#855)
* Making ProjectName a value object and testing everything * Moved EF Core stuff to Infrastructure Project * Working on Vogen integration Some references: SteveDunn/Vogen#677 (comment) SteveDunn/Vogen#676 (comment) * Updating dependencies * WIP trying to get IDs working with Vogen and avoiding EF Core references in SharedKernel and/or Core * Fixing up build by keeping EF stuff out of Core and in Infrastructure for Vogen generated code. * updating sample deps * Everything builds but tests still fail with uninitialized value object errors * All tests pass! Thanks @SteveDunn * Fix usings
1 parent 195f698 commit 071b7bc

File tree

53 files changed

+297
-122
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+297
-122
lines changed

nuget.config

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<packageRestore>
44
<add key="enabled" value="True" />
@@ -8,6 +8,8 @@
88
<packageSources>
99
<clear />
1010
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
11+
<add key="LocalNuget" value="c:\LocalNuget" />
12+
1113
</packageSources>
1214

1315
<packageSourceMapping>

sample/Directory.Packages.props

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
<Project>
22
<ItemGroup>
3-
<PackageVersion Include="Ardalis.GuardClauses" Version="4.6.0" />
3+
<PackageVersion Include="Ardalis.GuardClauses" Version="5.0.0" />
44
<PackageVersion Include="Ardalis.HttpClientTestExtensions" Version="4.2.0" />
55
<PackageVersion Include="Ardalis.ListStartupServices" Version="1.1.4" />
66
<PackageVersion Include="Ardalis.Result" Version="10.0.0" />
77
<PackageVersion Include="Ardalis.Result.AspNetCore" Version="10.0.0" />
8-
<PackageVersion Include="Ardalis.SharedKernel" Version="1.6.0" />
9-
<PackageVersion Include="Ardalis.SmartEnum" Version="8.0.0" />
8+
<PackageVersion Include="Ardalis.SharedKernel" Version="2.1.1" />
9+
<PackageVersion Include="Ardalis.SmartEnum" Version="8.1.0" />
1010
<PackageVersion Include="Ardalis.Specification" Version="8.0.0" />
1111
<PackageVersion Include="Ardalis.Specification.EntityFrameworkCore" Version="8.0.0" />
1212
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
13-
<PackageVersion Include="FastEndpoints" Version="5.29.0" />
13+
<PackageVersion Include="FastEndpoints" Version="5.30.0" />
1414
<PackageVersion Include="FastEndpoints.ApiExplorer" Version="2.3.0" />
15-
<PackageVersion Include="FastEndpoints.Swagger" Version="5.29.0" />
15+
<PackageVersion Include="FastEndpoints.Swagger" Version="5.30.0" />
1616
<PackageVersion Include="FastEndpoints.Swagger.Swashbuckle" Version="2.3.0" />
1717
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
1818
<PackageVersion Include="MediatR" Version="12.4.1" />
1919
<PackageVersion Include="MediatR.Extensions.Autofac.DependencyInjection" Version="12.1.0" />
20-
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.8" />
21-
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.8" />
22-
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
23-
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.8" />
24-
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8" />
25-
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
26-
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
27-
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
20+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.10" />
21+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
22+
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.10" />
23+
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.10" />
24+
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10" />
25+
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10" />
26+
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
27+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
2828
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
2929
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
3030
<PackageVersion Include="NSubstitute" Version="5.1.0" />
31-
<PackageVersion Include="ReportGenerator" Version="5.3.9" />
32-
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.2" />
33-
<PackageVersion Include="Serilog.Sinks.ApplicationInsights" Version="4.0.1-dev-00040" />
31+
<PackageVersion Include="ReportGenerator" Version="5.3.11" />
32+
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.3" />
33+
<PackageVersion Include="Serilog.Sinks.ApplicationInsights" Version="4.0.1-dev-00043" />
3434
<PackageVersion Include="SQLite" Version="3.13.0" />
3535
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
3636
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
37-
<PackageVersion Include="Testcontainers" Version="3.9.0" />
38-
<PackageVersion Include="xunit" Version="2.9.1" />
37+
<PackageVersion Include="Testcontainers" Version="3.10.0" />
38+
<PackageVersion Include="Vogen" Version="5.0.3" />
39+
<PackageVersion Include="xunit" Version="2.9.2" />
3940
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
4041
</ItemGroup>
4142
</Project>

sample/src/NimblePros.SampleToDo.Core/Interfaces/IToDoItemSearchService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace NimblePros.SampleToDo.Core.Interfaces;
44

55
public interface IToDoItemSearchService
66
{
7-
Task<Result<ToDoItem>> GetNextIncompleteItemAsync(int projectId);
8-
Task<Result<List<ToDoItem>>> GetAllIncompleteItemsAsync(int projectId, string searchString);
7+
Task<Result<ToDoItem>> GetNextIncompleteItemAsync(ProjectId projectId);
8+
Task<Result<List<ToDoItem>>> GetAllIncompleteItemsAsync(ProjectId projectId, string searchString);
99
}

sample/src/NimblePros.SampleToDo.Core/NimblePros.SampleToDo.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PackageReference Include="Ardalis.Specification" />
1515
<PackageReference Include="MediatR" />
1616
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
17+
<PackageReference Include="Vogen" />
1718
</ItemGroup>
1819

1920
</Project>

sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/Priority.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Ardalis.SmartEnum;
2-
3-
namespace NimblePros.SampleToDo.Core.ProjectAggregate;
1+
namespace NimblePros.SampleToDo.Core.ProjectAggregate;
42

53
public class Priority : SmartEnum<Priority>
64
{

sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/Project.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
namespace NimblePros.SampleToDo.Core.ProjectAggregate;
44

5-
public class Project : EntityBase, IAggregateRoot
5+
public class Project : EntityBase<Project, ProjectId>, IAggregateRoot
66
{
7-
public string Name { get; private set; }
7+
public ProjectName Name { get; private set; }
88

99
private readonly List<ToDoItem> _items = new();
1010
public IEnumerable<ToDoItem> Items => _items.AsReadOnly();
1111
public ProjectStatus Status => _items.All(i => i.IsDone) ? ProjectStatus.Complete : ProjectStatus.InProgress;
1212

13-
// Note: Probably it makes more sense to prioritize items, not projects, but this is just an example
14-
public Priority Priority { get; }
15-
16-
public Project(string name, Priority priority)
13+
public Project(ProjectName name)
1714
{
18-
Name = Guard.Against.NullOrEmpty(name);
19-
Priority = priority;
15+
Name = name;
2016
}
2117

2218
public void AddItem(ToDoItem newItem)
@@ -28,8 +24,8 @@ public void AddItem(ToDoItem newItem)
2824
base.RegisterDomainEvent(newItemAddedEvent);
2925
}
3026

31-
public void UpdateName(string newName)
27+
public void UpdateName(ProjectName newName)
3228
{
33-
Name = Guard.Against.NullOrEmpty(newName);
29+
Name = newName;
3430
}
3531
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using Vogen;
2+
namespace NimblePros.SampleToDo.Core.ProjectAggregate;
3+
4+
[ValueObject<int>]
5+
public partial struct ProjectId;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Vogen;
2+
3+
[assembly: VogenDefaults(
4+
staticAbstractsGeneration: StaticAbstractsGeneration.MostCommon | StaticAbstractsGeneration.InstanceMethodsAndProperties)]
5+
6+
7+
namespace NimblePros.SampleToDo.Core.ProjectAggregate;
8+
9+
// NOTE: Structs do not require conversion to work with EF Core
10+
[ValueObject<string>(conversions: Conversions.SystemTextJson)]
11+
public partial struct ProjectName
12+
{
13+
private static Validation Validate(in string name) => String.IsNullOrEmpty(name) ?
14+
Validation.Invalid("Name cannot be empty") :
15+
Validation.Ok;
16+
}

sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/Specifications/ProjectByIdWithItemsSpec.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class ProjectByIdWithItemsSpec : Specification<Project>
44
{
5-
public ProjectByIdWithItemsSpec(int projectId)
5+
public ProjectByIdWithItemsSpec(ProjectId projectId)
66
{
77
Query
88
.Where(project => project.Id == projectId)

sample/src/NimblePros.SampleToDo.Core/ProjectAggregate/ToDoItem.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
using NimblePros.SampleToDo.Core.ProjectAggregate.Events;
2+
using Ardalis.SharedKernel;
23

34
namespace NimblePros.SampleToDo.Core.ProjectAggregate;
45

5-
public class ToDoItem : EntityBase
6+
public class ToDoItem : EntityBase<ToDoItem, ToDoItemId>
67
{
8+
public ToDoItem() : this(Priority.Backlog)
9+
{
10+
}
11+
12+
public ToDoItem(Priority priority)
13+
{
14+
Priority = priority;
15+
}
16+
717
public string Title { get; set; } = string.Empty;
818
public string Description { get; set; } = string.Empty;
919
public int? ContributorId { get; private set; } // tasks don't have anyone assigned when first created
1020
public bool IsDone { get; private set; }
1121

22+
public Priority Priority { get; private set; }
23+
24+
1225
public void MarkComplete()
1326
{
1427
if (!IsDone)

0 commit comments

Comments
 (0)