• 0

Trying to Create a Method Signature I'm Happy With


Question

I'm trying to create a base interface / class that I am happy with that can accept a class type, request, and response. I can't quite get it structured the way I want it to be. I essentially want to have a helper assembly that will wrap any call on a given interface and perform things like timing, exception logging, etc. I want something similar to this:


var response = ServiceCaller.MakeServiceCall<Response, Request)>(SomeServiceClass.MyMethod(request));

return response;

 

I can't seem to quite get the code to be structured this way though. The closest I can get is:


public interface IWebServiceCaller<T>
        where T : class
    {
        R MakeServiceCall<R, C>(Expression<Func<T, Func<C, R>>> expression);
    }

 

public class WebServiceCaller<T>

 where T : class

{

         public R MakeServiceCall3<R, C>(Expression<Func<T, R>> expression, C request)
        {
            var exp = expression.Compile();
            ValidateRequest(request);
            var response = exp.Invoke(ServiceClass);

            return response;
        }

}

 

You have to pass the request object to be able to access it outside the expression it seems with this method though. I haven't used expressions that much I must admit. Is there something I'm missing here? I wish I could remove that extra C request parameter and still access the instance being passed in. If I change Expression<Func<T, R>> to Expression<Func<T, C, R>>, it gets even more awkward it seems.

 

Part of me is thinking I don't need the T, since my class doesn't really care about what type is being called. That could change method on the interface to just R MakeServiceCall3<R, C>, but then I still would need to pass the method to be called and an extra parameter for type C (like so: MakeServiceCall3<R, C>(serviceFuncDelegate, C request);

 

Maybe I'm trying to force this structure too much. I like the general idea of it, just can't get the code structured the way I would like.

1 answer to this question

Recommended Posts

  • 0

What's wrong with:

TReturn WrapRequest<TReturn>(Func<TReturn> f) {
     try {
         // start timing
         return f();
     }
    catch // etc.
    finally // log timing, etc.
}

Called as such:

 

WrapRequest(() => SomeServiceClass.MyRequest(request));

It's not clear to me what your requirements are, if this isn't satisfactory could you explain why?

 

If you need access to the request parameter then just pass it separately:

TReturn WrapRequest<TReturn, TParam>(Func<TParam, TReturn> f, TParam p) {
      // try-catch, time, log, do whatever you want with p, etc.
      return f(p);
}

WrapRequest(SomeServiceClass.MyRequest, request);

This is less flexible because the number of parameters is hardcoded; you'd need a separate overload for each different number of parameters.

This topic is now closed to further replies.
  • Posts

    • As much as I love owning my own hardware, it's hard to argue with the value. I'm not a huge gamer, I'd actually be interested in a cheaper plan with limited monthly hours, or even a pay-by-the-hour plan.
    • Well, they (LibreOffice/The Document Foundation) are bitchy and whiny, yes, but they're right, at least this time. It doesn't make sense to market something as "free and open source to thwart dependency on foreign companies' software" but at the same time, using the format of said companies (Microsoft) by default. That way, you are changing nothing, essentially, you're just using another UI. I'm not saying they should drop other formats altogether, but they shouldn't default to the thing they're trying to run away from in the first place. If you're gonna do something, just go all the way in, don't stop in the middle, IMO.
    • Words cannot express how much garbage this app is.
    • Vivaldi 8.0.4033.46 by Razvan Serea Vivaldi is a cross-platform web browser built for – and with – the web. A browser based on the Blink engine (same in Chrome and Chromium) that is fast, but also a browser that is rich in functionality, highly flexible and puts the user first. A browser that is made for you. Vivaldi is produced with love by a founding team of browser pioneers, including former CEO Jon Stephenson von Tetzchner, who co-founded and led Opera Software. Vivaldi’s interface is very customizable. Vivaldi combines simplicity and fashion to create a basic, highly customizable interface that provides everything a internet user could need. The browser allows users to customize the appearance of UI elements such as background color, overall theme, address bar and tab positioning, and start pages. Vivaldi features the ability to "stack" and "tile" tabs, annotate web pages, add notes to bookmarks and much more. Vivaldi 8.0.4033.46 fixes: [Chromium] Update to 148.0.7778.263 ESR (includes security fixes from 149.0.7827.102/103) Download: Vivaldi 64-bit | 139.0 MB (Freeware) Download: Vivaldi 32-bit | ARM64 View: Vivaldi Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I'm surprised they haven't found a way to fix that. As much as I don't like software wasting memory, a file manager is the kind of thing that makes sense to keep running in active memory for super-fast recall. I suspect that is why MS makes their File Explorer part of the main explorer.exe shell, so that it is guaranteed to always be running.
  • Recent Achievements

    • Week One Done
      FBSPL earned a badge
      Week One Done
    • One Year In
      Jim Dugan earned a badge
      One Year In
    • One Month Later
      Tommi118 earned a badge
      One Month Later
    • One Month Later
      sjbousquet earned a badge
      One Month Later
    • Week One Done
      sjbousquet earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      484
    2. 2
      PsYcHoKiLLa
      195
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      83
    5. 5
      ATLien_0
      69
  • Tell a friend

    Love Neowin? Tell a friend!