Lambda patterns in C#?


ANOOP MADHUSUDANAN

Vote on HN

I had few interesting discussions during the MVP Summit around using patterns from other programming languages in C#. Earlier, I’ve blogged about some of these scenarios.image

I’ve over heard Florian’s session this time during the summit, where he elaborated few interesting patterns from Java script that could be used in C#. He has a pretty good Code Project article regarding the same – Way To Lambda – and it is pretty good to see a formalized approach towards re-using these patterns in C#

One of my favorites is the C# version of Init-time branching (See Javascript Init time branching here)

   enum Mode
    {
        Http,
        WebSocket
    }

    class Messenger
    {

        public Func<string> Read { get; private set; }

        public Messenger(Mode m)
        {
            if (m == Mode.Http)
            {                
                Read = ()=>"Stub read logic using http"; 
            }
            else
            {
                Read = () => "Stub read logic using websocket";
            }
        }
    }

The above is a very simple version of Init time branching – but Instead of setting up the method (in this case the Read method) based on some configuration parameter, you can inject the same via the constructor or to the property – and in this way abstract out the branching as part the DI.

Convention Based Strategy Pattern *_*

Disclaimer: The remaining portion of the article is just not for regular use cases, and you can easily shoot yourself in the foot.

Now, let us use Init-Time branching to attain some kind of ‘Convention based’ strategy pattern. Branching will be done based on the caller’s semantics. Let us assemble a simple Serializer, that uses some convention based on the caller’s name. Note that we are using the Caller information attributes to extract the caller name.

class ConventionSerializer
    {
        public Func<string> Serialize { get; private set; }

        public ConventionSerializer([CallerMemberName] string memberName = "")
        {
            if (memberName.Contains("Xml"))
            {
                Serialize = () => 
                { /* serialize to xml */
                  return "xml"; 
                };
            }
            else
            {
                Serialize = () =>
                { /* serialize to json */
                    return "json";
                };
            }
        }

    }

And then, you may consume the same from the methods WriteXml and WriteJson.

       static void WriteXml()
        {
            var ser = new ConventionSerializer();
            Console.WriteLine(ser.Serialize()); //output xml
        }

        static void WriteJson()
        {
            var ser = new ConventionSerializer();
            Console.WriteLine(ser.Serialize()); //output json
        }

Was just bringing up an alternate perspective, requesting purists not to get offended. Happy Coding Smile

© 2012. All Rights Reserved. Amazedsaint.com