c# - Implementing an interface whereby generics are based on the interface -


i refactoring code important classes implement interface (for unit testability). came across class implements icomparable (non-templated); like:

public myclass : icomparable {     public int compareto(object obj)     {         myclass cobj = obj myclass;         if (cobj == null) { throw new argumentexception(); }         // etc.     } } 

i'm wanting interface out, , use generics while i'm @ it; this:

public imyclass : icomparable<imyclass> {     // other methods here }  public myclass : imyclass {     public compareto<imyclass>(imyclass other)     {         ...     }     // other methods here } 

but then, ideally, myclass should implement icomparable<myclass> (and subclasses of myclass should implement icomparable<mysubclass>).

all of ask several questions:

what think of approach described? there better way of doing refactoring? there point in making myclass implement icomparable<myclass>, or pointless since implement icomparable<imyclass>? pro-tips or "best"-practices made aware of?

does make sense have several objects of different types comparable each other? language allows this, can count on 0 hands number of times i've had use it.

i'd recommend using iclass without being icomparable, , have derived classes implement icomparable.

p.s. i'm against adding interfaces "for unit testability". if program design calls factory pattern interface-only coupling, means code level of complexity. don't abuse design make tests easier; use moles instead.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -