Monday, April 25, 2011

Where to specify flag for Base class use

I've got a base class called Request.cs

In this class I have the following private fields:

protected string _requestURI = xxpConfig.xxEndpoint;
protected string _requestURIAuthBased = xxConfig.xxEndpoint + "?SessionID=";

endpoint is just a string such as "http://SomeThridPartyAPIURIEndPoint"

In this class I have a method called SendRequest.

Subclasses for example represent certain API calls:

UpdateCustomerRequest.cs (inherits Request.cs) DeleteCustomerRequest.cs (inherits Request.cs)

and so on.

the Request class has a method called SendRequest that takes the URI and does the work to send the API request.

Certain kinds of API calls require a sessionID to be passed and others don't. The only difference in the URI is an added param called sessionID.

So I'm trying to figure out the best and most efficient way to set the "flag" for each type of API call (subclass) as each may or may not require the auth formatted URI vs. the regular. So in my base class I could check whether I need to use the _requestURI vs. _requestURIAuthBased in my Request.SendMethod for the subclass that I'm going to be calling to make that API call.

From stackoverflow
  • If I follow what you ask, the best way I can think of is to follow the model of the EventArgs class. All event handlers take an EventArgs object, however, sometime a derived class is passed:

     public class RequestArgs { } 
     public class Request 
     {  // ...
         void virtual SendRequest(RequestArgs args);
     }
    
     // ----------------------------
    
     public class UpdateArgs : RequestArgs 
     { 
          public string SessionID {get; set;}
     }  
    
     class UpdateCustomerRequest : Request
     {  // ...
         void SendRequest(RequestArgs args)
         {
            UpdateArgs updateArgs = args as UpdateArgs ;
           // :
         }
     }
    
  • Make a property in the base class

    ...
    
    // base class
    abstract string RequestURI { get; };
    
    // class that needs auth
    override string RequestURI { get { return _requestURIAuthBased; } }
    
    // class that dosn't needs auth
    override string RequestURI { get { return _requestURI; } }
    
    ....
    
    : I'm not following you. I'm not asking how to check for it, I'm asking where best to set this useAuth. If I have a bunch of subclasses that implement API calls and half of them require an auth URI and half don't where's the best way to specify that for each subclass so that the base can check it?
    : If you make a property in the base class, that's for use within, but you have to get the useAuth somewhere to check that for each type of API call that your subclasses represent.
    BCS : I noticed I read the question wrong the first time around. I guess my edit wasn't clear enough. Will fix

0 comments:

Post a Comment