IRequest / IResponse Pattern
Posted
by traderde
on Stack Overflow
See other posts from Stack Overflow
or by traderde
Published on 2010-05-28T00:35:47Z
Indexed on
2010/05/28
0:41 UTC
Read the original article
Hit count: 463
I am trying to create an Interface-based Request/Response pattern for Web API requests to allow for asynchronous consumer/producer processing, but not sure how I would know what the underlying IResponse class is.
public void Run()
{
List<IRequest> requests = new List<IRequest>();
List<IResponse> responses = new List<IResponse();
requests.Add(AmazonWebRequest); //should be object, trying to keep it simple
requests.Add(EBayWebRequest); //should be object, trying to keep it simple
foreach (IRequest req in requests)
{
responses.Add(req.GetResponse());
}
foreach (IResponse resp in response)
{
typeof resp????
}
}
interface IRequest
{
IResponse GetResponse();
}
interface IResponse
{
}
public class AmazonWebServiceRequest : IRequest
{
public AmazonWebServiceRequest()
{
//get data;
}
public IResponse GetResponse()
{
AmazonWebServiceRequest request = new AmazonWebServiceRequest();
return (IResponse)request;
}
}
public class AmazonWebServiceResponse : IResponse
{
XmlDocument _xml;
public AmazonWebServiceResponse(XmlDocument xml)
{
_xml = xml;
_parseXml();
}
private void _parseXml()
{
//parse Xml into object;
}
}
public class EBayWebRequest : IRequest
{
public EBayWebRequest ()
{
//get data;
}
public IResponse GetResponse()
{
EBayWebRequest request = new EBayWebRequest();
return (IResponse)request;
}
}
public class EBayWebResponse : IResponse
{
XmlDocument _xml;
public EBayWebResponse(XmlDocument xml)
{
_xml = xml;
_parseXml();
}
private void _parseXml()
{
//parse Xml into object;
}
}
© Stack Overflow or respective owner