C
C#2y ago
reeeeeee

Interface that is implemented to multiple classes (different types of objects)

Method in class1:
public Employee CreateEmployee(EmployeeCreateModel1 model) { .... }
public Employee CreateEmployee(EmployeeCreateModel1 model) { .... }
Method in class2:
public Employee CreateEmployee(EmployeeCreateModel2 model) { .... }
public Employee CreateEmployee(EmployeeCreateModel2 model) { .... }
How could I create interface that would be used for both of them..
public interface EmployeeService {
Employee CreateEmployee(??? createmodel);
}
public interface EmployeeService {
Employee CreateEmployee(??? createmodel);
}
Any idea/advice?
6 Replies
Yawnder
Yawnder2y ago
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
reeeeeee
reeeeeee2y ago
Yes but how would I specify the input parameter, if each service method expects different type
Yawnder
Yawnder2y ago
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.
reeeeeee
reeeeeee2y ago
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?
Yawnder
Yawnder2y ago
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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View