There are two different services. Those services have different processes but give us same result. A helper method needs to use that services and service responses. User is going to choose the which service will using. So, I am going to create a helper method and it takes chosen service and its parameters. The problem is that services need different parameters with different size.(a few of them is common but not most of them)
I create an abstract class and then two class inherited from it. Abstract class contains an standart method. But inherited class methods needs different type and size parameters. Because of that, I planning to use dynamic class object as a parameter. Is that proper solution?
public abstract class ServiceBase
{
public abstract int StandartMethod(dynamic parameters);
}
public class ServiceForA : ServiceBase
{
public override int StandartMethod(dynamic parameters)
{
//Service Request is send to ServiceA with spesific parameters
//(parameters.Ticket, parameter.myType1, string p1 , int n1, ..)
//----
//Service Response received and processed
return requiredInfo;
}
}
public class ServiceForB : ServiceBase
{
public override int StandartMethod(dynamic parameters)
{
//Service Request is send to ServiceB with spesific parameters
//(parameters.UserName,parameters.Password, parameters.myType1,int beginRange, ..)
//----
//Service Response received and processed
return requiredInfo;
}
}
public static int HelperMethod(ServiceBase service, ServiceParameters parameters)
{
//Do required stuff
return service.StandartMethod(parameters);
}
static void Main(string[] args)
{
ServiceBase serviceA = new ServiceForA();
dynamic obj1 = new ServiceParameters();
obj1.Ticket = GetTicket(UserName,Pass);
obj1.p1 = "Parameter1";
obj1.n1 = 10;
var result = HelperMethod(serviceA, obj1);
Console.WriteLine("RESULT 1: " + result);
ServiceBase serviceB = new ServiceForB();
dynamic obj2 = new ServiceParameters();
obj2.UserName = "slyn";
obj2.Password = "****";
...
obj2.beginRange = 1;
var result2 = HelperMethod(serviceB, obj2);
Console.WriteLine("RESULT2: " + result2);
}
Service object type will be ServiceForA or ServiceForB. It doesn't matter, StandartMethod is implemented both classes. And ServiceParameters class is inherited from System.Dynamic.DynamicObject
It seems solve the problem, but is it proper solution? Any suggestion?
dynamic parametersfrom method definition, there will not be abstract class anymore (overrided abstract methods needs same parameters). I need to write actual parameters in method definition.(In our example, ServiceForA and ServiceForB are not inheried from ServiceBase and StandartsMethods take params in ServiceFor* classes.) After that why do I need cast class and modify parameters?