Serj Sagan
Serj Sagan

Reputation: 30208

Interface Extension method to Concrete implementations

I know that title is a bit vague, but it's the best I could come up with.

I have this property

public string SSN { get; set; }

I also have an extension method:

public static string FormatSSN(this string ssn)
{
    return ssn..... // Format logic
}

So now I can do:

@Model.SSN.FormatSSN()

Cool, but basic stuff. The problem is now I have a second client of my app who needs FormatSSN() to do different stuff.

How do I interface such an extension method so that I can inject different implementations of it based on who the client is?

Upvotes: 0

Views: 143

Answers (3)

Aly Elhaddad
Aly Elhaddad

Reputation: 1943

How about

public static string FormatSSN(this string ssn, string clientIdentity)
{
    switch (clientIdentity)
    {
        case "client1Identifier":
            return ssn..... // Format logic for client 1
            break;
        case "client2Identifier":
            return ssn..... // Format logic for client 2
            break;
        default:
            return ssn..... // Default Format logic
            break;
    }
}

And

@Model.SSN.FormatSSN(@Model.ClientIdentifier)

Upvotes: 1

profesor79
profesor79

Reputation: 9473

That can be obvious, but as long as you can store cliencode (let's in web.config) then you can add an if statement. As static functions aren't nice to decouple, the other solution is have a factory method which will provide exact function based on customer.

factroy example below:

namespace ConsoleApplication1
{
    public interface ISsnFormater
    {
        string Format(string input);
    }

    public class ClientOneFormater : ISsnFormater
    {
        public string Format(string input)
        {
            throw new NotImplementedException();
        }
    }

    public class ClientTwoFormater : ISsnFormater
    {
        public string Format(string input)
        {
            throw new NotImplementedException();
        }
    }

    public class FormaterFactory
    {
        public ISsnFormater GetFormaterFor(string customerName)
        {
            switch (customerName)
            {
                case "One":
                    return new ClientOneFormater();

                case "Two":
                    return new ClientTwoFormater();
            }

            throw  new IndexOutOfRangeException();
        }
    }

}

Upvotes: 0

kemiller2002
kemiller2002

Reputation: 115498

How do I interface such an extension method so that I can inject different implementations of it based on who the client is?

You can't have an extension method that an interface recognizes, because the extension method is really static and is tied to the class.

If you want to make it so you can inject things, pass in a Func or an Action with the appropriate parameters.

For example,

int DoSomethingToSSN(Func<string,string> myFormatSSNAction){
   var myssn = "115454564" 
   return myFormatSSNAction(myssn);
}


public static string FormatSSN(string ssn)
{
    return ssn..... // Format logic
}

Then you would call it like DoSomethingToSSN(FormatSSN)

Upvotes: 1

Related Questions