Interface and Partial Classes

According to the SA1201 rule in StyleCop, elements in a class must appear in the correct order.
Order:

 Fields  
 Constructors  
 Finalizers (Destructors)  
 Delegates  
 Events  
 Enums  
 Interfaces  
 Properties  
 Indexers  
 Methods  
 Structs  
 Classes 

      

Everything is fine except for the interface part, because an interface can contain methods, events, properties, etc.

If we want to be strict about this rule, then we will not have all the members of the interface in one place, which is often very useful. According to StyleCop, this problem can be solved by dividing the class into partial classes.

Example:

/// <summary>
/// Represents a customer of the system.
/// </summary>
public partial class Customer
{
    // Contains the main functionality of the class.
}

/// <content>
/// Implements the ICollection class.
/// </content>
public partial class Customer : ICollection
{
    public int Count 
    { 
        get { return this.count; }
    }

    public bool IsSynchronized 
    { 
        get { return false; }
    }

    public object SyncRoot 
    { 
        get { return null; }
    }

    public void CopyTo(Array array, int index)
    {
        throw new NotImplementedException();
    }
}

      

Are there any other good solutions to this problem?

+2


source to share


3 answers


I believe (*) that explicit interface implementations should be grouped together (below events) and this rule only applies to implicit interface implementations. I think the rule makes sense, because if you implement an interface implicitly, it becomes part of the public interface of the class, so the members of the interface are logically part of the class as well (and therefore you shouldn't separate them from the rest of the class).

On the other hand, if you use explicit implementations, you are saying that you want to implement the interface without making it part of the class. In this case, it makes sense to keep it separate from the rest of the members.

So, I think the advice would be to use implicit interface implementations as often as possible (where it makes sense). For explicit implementations, it may seem less awkward to use the suggested ordering.



That said, if you think it's better to bundle the interface members together, then I would rather just ignore the StyleCop rule instead of using partial classes (which seems like a crazy workaround to me). After all, StyleCop only gives you advice ...

(*) If my guess is wrong, I would probably just ignore the rule: -).

+3


source


Personally, I don't see much value in this type of rule, but that's just me. But it looks like it's about nested types (he says "interfaces", not "interface implementations"), i.e.

class Foo {
    //...
    public interface IBar {...}
    //...
}

      

(since the other comparison is an enum)



in which case you can order it as he suggests. I just didn't want: -p (and nested interfaces are rare anyway)

If it means "explicit interface implementations", you can simply have them in that place and order them internally as already defined.

+1


source


Besides partial classes or disabling this rule, you can also write your own rule to replace it.

0


source







All Articles