Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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! :)</p> <p>i'm guessing you meant:</p> <pre><code>public static class TradeDataExt { public static bool IsActiveTrade(this TradeData tradeData){...} public static bool IsActiveRisk(this RiskData riskData) {...} } </code></pre> <p>Ok, first point said, now let's take a look at your question:</p> <blockquote> <p>This requirement is driven by grief that peer developers mix extension method on different types <strong>into one xxxExt class</strong>.It still works because compiler takes care of the resolution <strong>by looking at the imported namespaces.</strong></p> </blockquote> <p>In fact, what you are saying is the exact opposite of the truth of how extensions methods work! </p> <p>When you declare a static class with static methods, the methods will be automatically available when declaring <strong>using</strong> "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)</p> <p>Take a look at this example:</p> <pre><code>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){...} } } </code></pre> <p>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:</p> <p>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...<strong>add hundreds of extensions methods in the same namespace, is not a best practice at all</strong>.</p> <p>Your code should look like this:</p> <pre><code>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){...} } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload