Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to limit extension methods in a class to be of specific type?

This requirement is driven by grief that peer developers mix extension method on different types into one xxxExt class. It still works because compiler takes care of the resolution by looking at he imported namespaces. But it is annoying and not easy to maintain when it comes to overlapping types.

Is it at all possible to constraint the type extensions that could be written in a specific xxExt class? Circulating manual rules does not work well... possibly something like a static code analysis if not compiler level restriction?

Code (which I want to restrict in this class is IsActiveRisk method).

public static class TradeDataExt{
     public static bool IsActiveTrade(this TradeData tradeData){...}

     public static bool IsActiveRisk(this RiskData riskData) {...}


}

This is more of a "desired" feature, not sure if at all possible. Comments/suggestions would be helpful.

like image 631
Manish Basantani Avatar asked Nov 02 '22 21:11

Manish Basantani


1 Answers

First of all, it seems to me that you wrote your example and forgot the more important keyword of the extension methods, you missed the keyword "this" right before your parameter! :)

i'm guessing you meant:

public static class TradeDataExt
{
     public static bool IsActiveTrade(this TradeData tradeData){...}
     public static bool IsActiveRisk(this RiskData riskData) {...}
}

Ok, first point said, now let's take a look at your question:

This requirement is driven by grief that peer developers mix extension method on different types into one xxxExt class.It still works because compiler takes care of the resolution by looking at the imported namespaces.

In fact, what you are saying is the exact opposite of the truth of how extensions methods work!

When you declare a static class with static methods, the methods will be automatically available when declaring using "namespace", and not of the name of the class! (unless you are in scenario that you have a project with several .cs files that are partial classes of the same static class..... what i think does not make sense)

Take a look at this example:

namespace Gabriel.Extensions
{
    public static class ClassWithSameName
    {
         public static bool IsActiveTrade(this TradeData tradeData){...}
    }
}

namespace John.Extensions
{
    public static class ClassWithSameName
    {
         public static bool IsAGoodDeal(this TradeData tradeData){...}
    }
}

If you look at the example, both classes have the same name, but since they are in different namespaces, they will only extend the TradeData class when explicitly declaring the "using" of each namespace. So, i would say this is the way to go for you:

You should use the namespaces to control the types extensions being created, so you can have the namespace like XXXX.Extensions.Validation, XXXXX.Extensions.Calculation, XXXXX.Extensions.ServicesProvider and so on... instead of using all at the same namespace (because things can get complicated...add hundreds of extensions methods in the same namespace, is not a best practice at all.

Your code should look like this:

namespace TradeDataExtensions.Validation
{
    public static class ClassWithSameName
    {
         public static bool IsActiveTrade(this TradeData tradeData){...}
    }
}

namespace TradeDataExtensions.Analytics
{
    public static class ClassWithSameName
    {
         public static decimal ExpectedReturn(this TradeData tradeData){...}
    }
}
like image 64
Gabriel Vonlanten C. Lopes Avatar answered Nov 15 '22 04:11

Gabriel Vonlanten C. Lopes