EF Core can't store a union directly, so you have two choices:
JSON column (via a value converter): quick to set up, but System.Text.Json writes no type discriminator by default. Cases with the same shape can get mixed up when read back, and you can't easily query by state.
Discriminator column plus mapping, which I think is the better choice. store a StateKind column with the payload in nullable columns or JSON, and convert to and from the union in one place. You can query and index the state, and adding a new state flags your mapping code at compile time.
Keep the union in your domain code, and give the database a plain, queryable shape.
The parsing example raises an interesting tension: ParseError carries only a string Message, so a caller can display the reason but cannot branch on why parsing failed β distinguishing 'not a number' from 'out of range', for example, would mean parsing the message text again. Would adding a machine-readable code to ParseError, or splitting it into separate cases for each failure kind, keep the match exhaustive while letting the UI react differently to each failure? The same question applies to AccessDenied carrying a Reason string in the API example, where a 403 response might need different handling depending on the underlying reason.
Senior full-stack developer in Shiraz. Since 2016 I've built ASP.NET Core and Angular platforms for healthcare, real estate and e-commerce, with admin panels the teams run themselves.
The API results section is the one I'd reach for first. I keep ending up with small hand-written result wrappers in command handlers, and a union would make the success and failure cases explicit without the extra class hierarchy. Do you return the union straight from a minimal API endpoint, or map it to HTTP responses in a thin layer first? I wrote up how I split handlers in ASP.NET Core here, in case it sits well next to this series: dev.to/mohammad_khalifeh/clean-arc...
Senior full-stack developer in Shiraz. Since 2016 I've built ASP.NET Core and Angular platforms for healthcare, real estate and e-commerce, with admin panels the teams run themselves.
The UnionsWebApi minimal API example mapping each service union case straight to an HTTP response is where unions really earn their keepβmuch cleaner than juggling custom exceptions or nullable tuples just to separate 404 from validation errors.
The state machine example clicked for me. Enum plus nullable fields that only make sense in some states, I've seen that mess a lot.
How would you store ConnectionState with EF Core though? JSON column?
That's my favorite example as well.
As for your question:
EF Core can't store a union directly, so you have two choices:
JSON column (via a value converter): quick to set up, but System.Text.Json writes no type discriminator by default. Cases with the same shape can get mixed up when read back, and you can't easily query by state.
Discriminator column plus mapping, which I think is the better choice. store a StateKind column with the payload in nullable columns or JSON, and convert to and from the union in one place. You can query and index the state, and adding a new state flags your mapping code at compile time.
Keep the union in your domain code, and give the database a plain, queryable shape.
Makes sense, the discriminator route is what I'd pick too. Being able to index the state matters more than saving a few columns.
And I'd keep StateKind as varchar with IsUnicode(false). Learned that one the hard way.
The parsing example raises an interesting tension: ParseError carries only a string Message, so a caller can display the reason but cannot branch on why parsing failed β distinguishing 'not a number' from 'out of range', for example, would mean parsing the message text again. Would adding a machine-readable code to ParseError, or splitting it into separate cases for each failure kind, keep the match exhaustive while letting the UI react differently to each failure? The same question applies to AccessDenied carrying a Reason string in the API example, where a 403 response might need different handling depending on the underlying reason.
Thanks for the feedback!
The API results section is the one I'd reach for first. I keep ending up with small hand-written result wrappers in command handlers, and a union would make the success and failure cases explicit without the extra class hierarchy. Do you return the union straight from a minimal API endpoint, or map it to HTTP responses in a thin layer first? I wrote up how I split handlers in ASP.NET Core here, in case it sits well next to this series: dev.to/mohammad_khalifeh/clean-arc...
You can do both - return the union or have a middleware that transforms it.
thank you i will consider it!
The UnionsWebApi minimal API example mapping each service union case straight to an HTTP response is where unions really earn their keepβmuch cleaner than juggling custom exceptions or nullable tuples just to separate 404 from validation errors.
Thanks for the feedback!
ParseError with only a string Message leaves the caller guessing which check failed. A code next to the message fixes that.
This is just a simple example to show what's possible with unions. Of course you can add more custom types to your union, including status code.
A status code in the union settles the error case cleanly. Add that one first.
adding one case to a public union being a breaking change is going to catch a lot of library authors out. that's a major version bump for one line
Good point, thatβs why using unions in libraries should be done very carefully.
I think the state machine example is the clearest use case here.
Agreed, thatβs also my favorite one.