Extension interface patterns

后端 未结 11 1995
失恋的感觉
失恋的感觉 2020-12-14 16:56

The new extensions in .Net 3.5 allow functionality to be split out from interfaces.

For instance in .Net 2.0

public interface IHaveChildren {
    str         


        
相关标签:
11条回答
  • 2020-12-14 17:03

    There is nothing wrong with extending interfaces, in fact that is how LINQ works to add the extension methods to the collection classes.

    That being said, you really should only do this in the case where you need to provide the same functionality across all classes that implement that interface and that functionality is not (and probably should not be) part of the "official" implementation of any derived classes. Extending an interface is also good if it is just impractical to write an extension method for every possible derived type that requires the new functionality.

    0 讨论(0)
  • 2020-12-14 17:03

    Ouch. Please don't extend Interfaces.
    An interface is a clean contract that a class should implement, and your usage of said classes must be restricted to what is in the core Interface for this to work correctly.

    That is why you always declare the interface as the type instead of the actual class.

    IInterface variable = new ImplementingClass();
    

    Right?

    If you really need a contract with some added functionality, abstract classes are your friends.

    0 讨论(0)
  • 2020-12-14 17:06

    I think the best thing that extension methods replace are all those utility classes that you find in every project.

    At least for now, I feel that any other use of Extension methods would cause confusion in the workplace.

    My two bits.

    0 讨论(0)
  • 2020-12-14 17:08

    Extension methods should be used as just that: extensions. Any crucial structure/design related code or non-trivial operation should be put in an object that is composed into/inherited from a class or interface.

    Once another object tries to use the extended one, they won't see the extensions and might have to reimplement/re-reference them again.

    The traditional wisdom is that Extension methods should only be used for:

    • utility classes, as Vaibhav mentioned
    • extending sealed 3rd party APIs
    0 讨论(0)
  • 2020-12-14 17:08

    One problem I can see is that, in a large company, this pattern could allow the code to become difficult (if not impossible) for anyone to understand and use. If multiple developers are constantly adding their own methods to existing classes, separate from those classes (and, God help us all, to BCL classes even), I could see a code base spinning out of control rather quickly.

    Even at my own job, I could see this happening, with my PM's desire to add every bit of code that we work on to either the UI or the data access layer, I could totally see him insisting on 20 or 30 methods being added to System.String that are only tangentially-related to string handling.

    0 讨论(0)
  • 2020-12-14 17:09

    I needed to solve something similar: I wanted to have a List<IIDable> passed to the extensions function where IIDable is an interface that has a long getId() function. I tried using GetIds(this List<IIDable> bla) but the compiler didn't allow me to do so. I used templates instead and then type casted inside the function to the interface type. I needed this function for some linq to sql generated classes.

    I hope this helps :)

        public static List<long> GetIds<T>(this List<T> original){
            List<long> ret = new List<long>();
            if (original == null)
                return ret;
    
            try
            {
                foreach (T t in original)
                {
                    IIDable idable = (IIDable)t;
                    ret.Add(idable.getId());
                }
                return ret;
            }
            catch (Exception)
            {
                throw new Exception("Class calling this extension must implement IIDable interface");
            }
    
    0 讨论(0)
提交回复
热议问题