I use inline functions to avoid garbage collection pressure specially when dealing with longer running methods. Say one would like to get 2 years or market data for a given ticker symbol. Also, one can pack a lot of functionality and business logic if one needs to.
what one does is open a socket connection to the server and loop over the data binding an event to a event. One can think of it the same way as a class is designed, only one is not writing helper methods all over the place that are really only working for one pice of functionality. below is some sample of how this might look like, please note that i am using variables and the "helper" methods are below the finally. In the Finally I nicely remove the event handlers, if my Exchange class would be external/injected i would not have any pending event handler registrated
void List<HistoricalData> RequestData(Ticker ticker, TimeSpan timeout)
{
var socket= new Exchange(ticker);
bool done=false;
socket.OnData += _onData;
socket.OnDone += _onDone;
var request= NextRequestNr();
var result = new List<HistoricalData>();
var start= DateTime.Now;
socket.RequestHistoricalData(requestId:request:days:1);
try
{
while(!done)
{ //stop when take to long….
if((DateTime.Now-start)>timeout)
break;
}
return result;
}finally
{
socket.OnData-=_onData;
socket.OnDone-= _onDone;
}
void _OnData(object sender, HistoricalData data)
{
_result.Add(data);
}
void _onDone(object sender, EndEventArgs args)
{
if(args.ReqId==request )
done=true;
}
}
You can see the advantages as mentioned below, here you can see a sample implementation. Hope that helps explaining the benefits.