mercredi 2 novembre 2016

c# func parameter not being used

I have a legacy app that logs the input / output of services. Currently, every method has the same lines to log the request and response objects. I would like to use AOP, but without adding any extra tool (Postsharp, Castle, etc), or wrap every service class into another class (ServiceWrapper).

In order to do that, I'm trying to create a Generic class that knows that it should log the request and response objects. Here's what I'm trying:

using System;

namespace ProxyTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var request = "request";
            var fooService = new FooService();

            ServiceProxy.Invoke(r => fooService.DoFoo(request), "abc");

            Console.Read();
        }
    }

    class ServiceProxy
    {
        public static void Invoke(Func<object, object> service, object request)
        {
            Console.WriteLine("input:" + request);

            var response = service(request);

            Console.WriteLine("output:" + response);
        }
    }

    class FooService
    {
        public string DoFoo(object a)
        {
            return a +  ": returning: Do Foo";
        }
    }

}

Although it's working, the "abc" string is just to compile the application, but it's not being used as the request parameter. If I remove that, the code does not compile. Am I missing something?

Aucun commentaire:

Enregistrer un commentaire