Interface that is implemented to multiple classes (different types of objects)
Method in class1:
Method in class2:
How could I create interface that would be used for both of them..
Any idea/advice?
6 Replies
That's exactly what an interface is for.
You create one that is implemented by multiple classes, and then you specify that that method takes in that interface.
@reeeeeee
Yes but how would I specify the input parameter, if each service method expects different type
If you expect a different implementation, then why bother with an interface?
An interface is when you don't care about the implementation, as long as it meets some criteria.
I have some kind of a background worker, which uses two kinds of APIs. You specify type of API in the appsettings. The whole processing of the data is completely the same, except some models/DTO.. would be better to always check if type1 -> use service1, elee use service2?
Without seeing the code itself it's hard to say, but yes, you could either do a type switch for that, or explore generics / generic registrations in your DI.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View