0

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?

Community
  • 1
  • 1
Wei Ma
  • 3,125
  • 6
  • 44
  • 72
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackoverflow.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 08 '14 at 00:14
  • @JohnSaunders - I think you're going a little too far with removing "c#" from the title here. This question is specifically about c# syntax. This isn't a general purpose programming question were the answer would be valid for other languages. I think this is a case where "they are organic to the conversational tone of the title". Perhaps rewording the title to make it more conversational rather than removing the tag would be been better? – Enigmativity May 08 '14 at 00:23
  • That's why C# is in the tags. – John Saunders May 08 '14 at 00:46
  • 1
    This may depend on why you require the use of extension methods. If you are writing myClass too (i.e., you have control over it and can add to it), then just add these methods as member methods of the class. If you do not have control of myClass (like maybe it is in an closed-source API), then you would need extension methods, but then how do you know how to map functionality differently based on a key? I am sensing that there be a better, more o-o way to accomplish this without needing extension methods at all, which would make for simpler and more maintainable code. Please advise. – philologon May 08 '14 at 06:45
  • I have answered the comment in the main question. Thanks for asking. – Wei Ma May 08 '14 at 17:02
  • @philologon You never *need* extension methods. They just provide an alternate syntax for calling static methods declared and implemented in external classes where the first parameter is of a given type. – Tom Blodget May 08 '14 at 23:50
  • @TomBlodget I'm do not claim to be the expert, but as I understand it, Extension Methods are necessary for Linq to work. – philologon May 09 '14 at 01:24
  • I agree with @TomBlodget, you never "need" extension methods, they are just syntactic sugar to make programmers' life easier. Without them, Linq could be come very cumbersome and not usable by regular joes. – Wei Ma May 09 '14 at 02:29

1 Answers1

1

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.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Yes, this will work. But this is not what I want. Imagine that I need to write 50 similar functions, writing these simple 4-liner become tedious. I would like to replace them with 1-liners. – Wei Ma May 08 '14 at 00:15
  • Then move all of the code to one line. There is no way to use delegates and retain the extension method functionality as simple assignments. – Enigmativity May 08 '14 at 00:25