Generic static factories
Hello. I have the following interface:
This works well but I want to have a specific type of strongly typed id's of type
AggregateId
which must have an underlying backing type of Guid
, especially a version 7 guid. So basically how can I have default implementations for abstract records?
I tried this but infinite recursion.
10 Replies
Hello!
The problem you have is that in the abstract record AggregateId<TId>, the implementation of the From method calls itself, resulting in infinite recursion. Specifically, TSelf.From(value) actually calls the From method of the current class instead of the concrete subclass implementation, which results in recursive calls and eventually throws a stack overflow.
Yes
so avoid to invoke From, you can:
or , if you want use reflection.
An overridable method cannot be static so this will not work.
oh, I see. sorry
if it must be static, you can derived it.
Yeah I figured you could use reflection, but wondering if it is possible without really.
avoid reflection, you can use factory method.
derived it:
Thanks for your help but I'm not really a fan of either of these but I appreciate it ❤️
QwQ,You're welcome
What do you need this to be an interface for?