Home > database >  Trying to get rid of many if else with factory method
Trying to get rid of many if else with factory method

Time:09-22

    if (ftpType == "YouTube")
    {
        var digitalServiceResponse = JsonConvert.DeserializeObject<YoutubeMetadataResponse>(metadataJson);

        var fileRequest = new AddDeliveryFileRequest
        {
            //some code
        };

        ftpDeliveryQueue.AddFile(fileRequest);
    }
    else if (ftpType == "Flashtalking")
    {
        var metadata = JsonConvert.DeserializeObject<FlashtalkingMetadataResponse>(metadataJson);

        foreach (var file in metadata.FileList)
        {
            var fileRequest = new AddDeliveryFileRequest
            {
                //some code
            };

            ftpDeliveryQueue.AddFile(fileRequest);
        }
    }
    else if (ftpType == "Innovid")
    {
        var metadata = JsonConvert.DeserializeObject<InnovidMetadataResponse>(metadataJson);

        foreach (var file in metadata.FileList)
        {
            var fileRequest = new AddDeliveryFileRequest
            {
                //some code
            };

            ftpDeliveryQueue.AddFile(fileRequest);
        }
    }
    else
    {
        var digitalServiceResponse = JsonConvert.DeserializeObject<SizmekMetadataResponse>(metadataJson);

        foreach (var file in digitalServiceResponse.Metadata.FileList)
        {
            var fileRequest = new AddDeliveryFileRequest
            {
                //some code
            };
            ftpDeliveryQueue.AddFile(fileRequest);
        }
    }

How do I get rid of these if and else if block with the help of factory method?

CodePudding user response:

You could use a Dictionary where the key is the string and the value is an Action.

For example:

static void Main( string[ ] args )
{ 
    // Obviously replace the body of these lambdas with your 
    // custom logic or have them call a private member method.
    var dict = new Dictionary<string, Action>
    {
        { "YouTube", ( ) => { Console.WriteLine( "YouTube" ); } },
        { "Flashtalking", ( ) => { Console.WriteLine( "Flashtalking" ); } },
        { "Innvoid", ( ) => { Console.WriteLine( "innvoid" ); } },
    };
    
    // This is what this would look like if you wanted to accept
    // parameters. In this case, each `Action` would have to accept
    // the same number of parameters and parameter types.
    var dict2 = new Dictionary<string, Action<int, string>>
    {
        { "YouTube", ( num, str ) => { Console.WriteLine( "YouTube" ); } },
        { "Flashtalking", ( num, str ) => { Console.WriteLine( "Flashtalking" ); } },
        { "Innvoid", ( num, str ) => { Console.WriteLine( "innvoid" ); } },
    };
    
    // If you want to pass an arbitrary number of parameters
    // and types you could pass an array of objects and just
    // cast the elements to their known types.
    var dict3 = new Dictionary<string, Action<object[ ]>>
    {
        { "YouTube", ( objects ) => { Console.WriteLine( $"YouTube: {( int )objects[ 0 ]}" ); } },
        { "Flashtalking", ( objects ) => { Console.WriteLine( $"Flashtalking: {objects[ 1 ]}" ); } },
        { "Innvoid", ( _ ) => { Console.WriteLine( "innvoid" ); } },
    };

    // Then you can use it like this.
    var key = "YouTube";
    if ( dict.ContainsKey( key ) )
        dict[ key ]( );

    // Or to pass parameters.
    if ( dict2.ContainsKey( key ) )
        dict2[ key ]( 42, "Some string" );

    // Or to pass an arbitrary number of parameters and types of
    // of parameters.
    if ( dict3.ContainsKey( key ) )
        dict3[ key ]( new object[ ] { 42, "Some string" } );

}

You would want to make your Dictionary a readonly field of the encapsulating class of course.

  • Related