I'm working in a web service that sends SMS messages using N providers. Each provider receives the message in a different format at his own web service.
In order to create a pattern in my web service, I created an abstract class with many methods to be implemented for each of the providers.
Some of the methods need to receive an object that can be of different types, depending on the child class (provider) that inherits it. One of these methods returns an object that can be of at least two different types.
I achieved this working with the "object" keyword, which allows me to pass or return any object:
public abstract class Provider {
protected HttpClient _Client;
protected SMSManagerAPIContext _Context;
public abstract DeliveryResponse SendSMS(object Sms);
protected abstract DeliveryResponse ParseResponse(HttpResponseMessage Response);
public abstract object PrepareMessage(SMS Sms);
protected abstract void SaveResponse(object Sms, HttpResponseMessage Response);
}
The object Sms
received in the SendSMS
method is the return of the PrepareMessage
method. The problem is that PrepareMessage
could return an object of different types.
(For example, one of my providers accept requests to send one message or multiple messages, but the object is different for each one. Then I return the correct object in the PrepareMessage
method, and the SendSMS
method send it as a JSON object to the provider.)
It works but causes some conversion problems in the child classes during the development, and I was wondering if there is a better way to do this. I have read some questions about it:
Generic method that can return different types
I think that overload methods using Generics would be better, but I have no experience with C#.
Any ideas?