In my first post I said I wanted Vitreous to cover more of the setup a clean architecture project needs. Version 0.6.0 does that with two commands: one installs the templates, the other creates a layered solution that builds with no warnings and comes with 34 passing tests.
Underneath, dispatch got faster too. Sending a query takes about 90 ns and allocates nothing, whether its handler is the first or the 200th.
Two commands
I wanted to give people an easier way into Vitreous and into clean architecture, especially to try them in a new project:
dotnet new install Vitreous.Templates
dotnet new vitreous-clean -n ShopThat creates four projects and their tests:
src/
Shop.Domain/
Shop.Application/
Shop.Infrastructure/
Shop.Api/
tests/
Shop.Domain.Tests/
Shop.Application.Tests/
Shop.Architecture.Tests/
Shop.Api.Tests/The domain has an Order aggregate with its value objects and events. Application has the commands,
queries and handlers, Infrastructure has EF Core, and the API has the endpoints, OpenAPI and the
developer dashboard.
Run dotnet test and all 34 pass. Nothing to configure first: the API's database is a SQLite file
it creates on the first run, and the handler tests use SQLite in memory.
The rule of clean architecture is that dependencies point inwards, towards the domain. In
Shop.Architecture.Tests that rule is a test, so it can't erode quietly:
[Fact]
public void Domain_uses_nothing_but_Vitreous() =>
AssertUsesOnly(Domain, "Vitreous");It looks at the assemblies the compiled layer references. Use an EF Core type in the domain and the test fails.
There are no repositories. A DbContext already is a unit of work, and each DbSet a repository,
so handlers use EF Core through a small IAppDbContext interface that the Application layer owns.
When an order is saved, its domain events are published too.
Slices or layers
Clean architecture isn't always the right start, so it's one of four project templates:
| Template | What you get |
|---|---|
vitreous-api |
A minimal API in vertical slices |
vitreous-console |
A command-line app in vertical slices |
vitreous-worker |
A background service in vertical slices |
vitreous-clean |
The layered solution, with --host api, --host console or --host worker |
In a vertical slice, a feature keeps its command, validator, handler and endpoint in one file. You read it, change it or delete it in one place. Start there, and pick the layers when you have business rules worth protecting from the database and the app around them.
They all target .NET 8 to 11, and options like --validation, --caching redis or --aot add or
remove pieces where they apply.
There are smaller templates for the everyday work too. From a feature's folder:
cd src/Shop.Application/Orders
dotnet new vitreous-command -n CancelOrder --result GuidYou get CancelOrder.cs with the command and an empty handler to fill in, in the namespace of that
folder. The source generator registers it on the next build.
A template is code nobody compiles
Template files are full of #if blocks and placeholder names, and they reference packages that
aren't published yet. My repository can't build them, so a broken template would look fine until
somebody ran it.
So CI does what a user does. It packs the templates, installs the package, creates 37 projects in different combinations of template, framework and options, builds each one with warnings as errors and runs its tests if it was created with them.
Then it runs some of them. It checks the console app's output and exit codes, stops the worker with SIGTERM and expects a clean exit, and publishes the Native AOT variant of the API and calls it over HTTP.
Some of the details that only matter in a generated project:
- The console host is named
Shop.Cli. Inside aShop.Consolenamespace,Console.WriteLinedoesn't compile, becauseConsoleis now the namespace. - The console app doesn't pass its arguments to the .NET host. The host would read
add a=bas configuration. - On .NET 8 the solution is a
.sln, since that SDK can't read.slnx. template.jsoncan't contain a backslash. MSBuild turns\into/on Linux when it rewrites the file at pack time, so packing fails if there is one.
One lookup instead of a chain
The first versions of the generated dispatcher were a chain of type checks, one per message:
if (query is GetOrderQuery q0)
return (Result<TResult>)(object)await ExecuteQuery<GetOrderQuery, OrderDto>(q0, ct);
if (query is ListOrdersQuery q1)
return (Result<TResult>)(object)await ExecuteQuery<ListOrdersQuery, OrderList>(q1, ct);
// ...one per queryIt's simple, but every new handler makes the chain longer, and each send also allocated a delegate for the handler, even with no behaviors to run. Since 0.2.0 the generator writes a frozen dictionary instead, from the message type to a typed function:
static readonly FrozenDictionary<Type, Delegate> s_queries =
new Dictionary<Type, Delegate>
{
[typeof(GetOrderQuery)] = (QueryInvoker<OrderDto>)
((sp, q, ct) => ExecuteQuery<GetOrderQuery, OrderDto>(
sp, (GetOrderQuery)q, ct)),
// ...one entry per query
}.ToFrozenDictionary();
public ValueTask<Result<TResult>> Send<TResult>(
IQuery<TResult> query, CancellationToken ct = default)
{
if (s_queries.TryGetValue(query.GetType(), out var found)
&& found is QueryInvoker<TResult> invoke)
return invoke(_services, query, ct);
throw NoHandler(query);
}QueryInvoker<TResult> stands in for a long Func<...> type here.
The trick is that type check. Send<TResult> already knows the result type, so it can turn the
Delegate back into the exact function, with no cast through object and no async. And when a
message has no behaviors, its handler is called directly, so there is no delegate to allocate.
I ran both versions on the same machine with 200 extra queries registered, and sent the ones at different places in the chain:
| Query | 0.1.1 | 0.6.0 |
|---|---|---|
| 1st | 169.0 ns / 64 B | 85.8 ns / 0 B |
| 50th | 199.5 ns / 64 B | 90.3 ns / 0 B |
| 100th | 270.4 ns / 64 B | 83.8 ns / 0 B |
| 200th | 455.1 ns / 64 B | 82.3 ns / 0 B |
And 0.6.0 next to MediatR 12.4.1:
| Scenario | MediatR | Vitreous 0.6.0 |
|---|---|---|
| Send a query | 159.7 ns / 200 B | 92.7 ns / 0 B |
| Send a query, last of 200 | 158.6 ns / 200 B | 92.8 ns / 0 B |
| Send a query through one behavior | 269.7 ns / 384 B | 129.5 ns / 64 B |
| Send a command with no result | 146.9 ns / 128 B | 82.3 ns / 0 B |
This is .NET 10 on a Xeon Silver 4114, a much slower CPU than in my first post, so don't compare the two. MediatR runs with its default transient handlers and Vitreous with singletons, while its own default is scoped, so your numbers will differ. The zero is for a send with no behaviors: the templates add logging and validation, and those have a cost, as the third row shows.
It's still nanoseconds, and your database won't notice. The point is that the cost stays flat as the app grows.
What's next
More hosts and more databases for the templates, so a new project is closer to the one you'd ship.
The templates are on NuGet, every option is in the docs and the code is on GitHub. Try one on your next project and tell me where it got in your way.