I have a situation in a project that I am currently working on at work that has left my brain uneasy for the whole weekend. First, I need to explain my scenario and the possible solutions that I have considered.
I am writing a composite WCF service that will aggregate a large number of external APIs. These APIs are arbitrary, and their existence is all that is needed for this explanation.
These services can be added and removed throughout the development period. My WCF service should be able to consume services using several methods (REST, SOAP, etc.). In this example, I focus on communicating with external APIS by manually creating queries in code.
For example, we can have two APIs ServiceX and ServiceY .
ServiceX is consumed by a POST with a web request with specific data in the request body.
ServiceY is consumed by a POST with a web request with data appended to the url (Yes ... I know it must be GET, but I did not write an external API, so don't read me this.)
To avoid code duplication, I wrapped web requests using a command template and use factory to create requests.
For ServiceX, the data should be encoded and placed in the body of the request, as opposed to ServiceY, where the data should be overwritten and placed on the Post line.
I have a class structure such as:
public abstract class PostCommandFactory
{
public ICommand CreateCommand();
}
public class UrlPostCommandFactory:PostCommandFactory
{
public ICommand CreateCommand()
{
}
}
public class BodyPostCommandFactory:PostCommandFactory
{
public ICommand CreateCommand()
{
}
}
public interface ICommand
{
string Invoke();
}
public class UrlPostCommand:ICommand
{
public string Invoke()
{
}
}
public class BodyPostCommand:ICommand
{
public string Invoke()
{
}
}
, , , , , GET. , . , , . :
public class RequestBodyPostStrategy:IPostStrategy
{
public string Invoke()
{
}
}
public class UrlPostStrategy:IPostStrategy
{
public string Invoke()
{
}
}
public interface IPostStrategy
{
string Invoke();
}
public class PostContext
{
pubic List<IPostStrategy> _strategies;
public IPostStrategy _strategy;
public PostContext()
{
_strategies = new List<IPostStrategy>();
}
public void AddStrategy(IPostStrategy strategy)
{
_strategies.Add(strategy);
}
public void SetStrategy(IPostStrategy strategy)
{
_strategy = strategy;
}
public void Execute()
{
_strategy.Invoke();
}
}
, .
?