Wei Ma
Wei Ma

Reputation: 3155

Assign function to an extension method

I am writing extension methods as

public static class MyExtension{
   private const String key1 = "key1";
   private const String key2 = "key2";


   public static String GetCalculatedValue1(this MyClass src, MyClass2 para){
        ... do things with src and para and key 1
        return "CalculatedValue1";
   }

   public static String GetCalculatedValue2(this MyClass src, MyClass2 para){
        ... do things with src and para and key 2
        return "CalculatedValue2";
   }
}

Since the logic inside GetCalculatedValue1 and GetCalculatedValue2 are very similar except they are using different keys. I am thinking of extract them into something like

delegate String GetValue(MyClass src, MyClass2 para);
private static GetValue GetValueFunc(String key){
   return (s,p) => {
      ... logic with s, p, and key
   };
} 

public static String GetCalculatedValue1(this MyClass src, MyClass2 para){
   return GetValueFunc(key1)(src,para);
}

public static String GetCalculatedValue2(this MyClass src, MyClass2 para){
   return GetValueFunc(key2)(src,para);
}

Then I found the return GetValueFunc... statements are somewhat wasteful. I am wondering if we could do

public static String GetCalculatedValue1(this MyClass src, MyClass2 para) = GetValueFunc(key1);
public static String GetCalculatedValue1(this MyClass src, MyClass2 para) = GetValueFunc(key2);

but of cause The C# syntax won't allow me do it. I understand that if I didn't need the functions to be extension methods, I could write

 public static readonly GetValue GetCalculatedValue1 = GetValueFunc(key1);
 public static readonly GetValue GetCalculatedValue2 = GetValueFunc(key2);

I am wondering if there is any way to do this kind of "function assignment" for extension methods as well.

Edit: To answer @philologon's question: I am writing these extension methods for HttpContext, which I have no control. All I am trying to do is to eliminate the calls for context.Items["key"]. Accessing this dictionary using magic strings are pretty error prone. so I am thinking about creating something like context.GetIceCream context.GetBeer instead of calling context.Items["icecream"] context.Items["beer"], also I would like this calls to be strongly typed, for example GetIceCream will return an object of class "IceCream". So my code will be var iceCream = context.GetIceCream() instead of var iceCream = (IceCream)context.items["icecream"] I am trying to utilize the functional approach with traditional OO concept. After all, C# is a functional language as well :)

Why is C# a functional programmming language?

Upvotes: 0

Views: 149

Answers (1)

Enigmativity
Enigmativity

Reputation: 117047

I would just do this and avoid delegate entirely.

public static class MyExtension
{
    private const String key1 = "key1";
    private const String key2 = "key2";

    private static String GetCalculatedValue(this MyClass src, MyClass2 para, string key)
    {
            ... do things with src and para and key
            return "CalculatedValue";
    }

    public static String GetCalculatedValue1(this MyClass src, MyClass2 para)
    {
            return src.GetCalculatedValue(para, key1);
    }

    public static String GetCalculatedValue2(this MyClass src, MyClass2 para)
    {
            return src.GetCalculatedValue(para, key1);
    }
}

If you want you could still use a delegate, but it makes things more complicated.

Upvotes: 1

Related Questions