C
C#13mo ago
Kiel

❔ Automatic discovery of scoped services without a marker interface

In the past, I had an IService interface which defined a (singleton at the time) service and a contract for certain service-level init functionality etc. This was coupled with an extension method which used reflection to find all IService implementation types and register them with my service provider at startup. I'm trying to do the same thing with scoped services, but I'm struggling to think of ANYTHING to put on this prospective IScopedService interface, and I always feel like marker interfaces are...smelly. Any suggestions? Or should I just ignore my gut feeling and be happy with a marker interface for this extremely specific usecase?
6 Replies
Pobiega
Pobiega13mo ago
If you want to find them via reflection, you need either an interface, base class or an attribute to look for
Kiel
KielOP13mo ago
For sure, I'm not trying to avoid using an interface. Maybe an attribute would be better, as that fits the "marker" moniker better than an interface which does nothing Kinda forgot I could do that
Pobiega
Pobiega13mo ago
Yup
jcotton42
jcotton4213mo ago
this also opens the door to source gen usage wesmart
cumslvt13
cumslvt1313mo ago
Well technically you could use something like naming convention to register service. Like wolverine is doing for command handlers https://wolverine.netlify.app/tutorials/getting-started.html#snippet-sample_quickstart_issuecreatedhandler
Getting Started | Wolverine
Next Generation Command and Message Bus for .NET
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server