Interface and partial classes
According to rule SA1201 in StyleCop elements in class must appear in correct order.
The order is following: Fields
Constructors
Finalizers (Destructors)
Delegates
Events
Enums
Interfaces
Properties
Indexers
Methods
Structs
Classes
Everything is ok, except of Interfaces part, because Interface can contain methods, events, properties etc...
If we want to be strict about this rule then we won't have all members of Interface in one place which is often very useful. According to StyleCop help this problem can be solved by splitting 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 oth开发者_StackOverflower good solutions to this problem?
I suppose (*) that explicit interface implementations should be grouped together (below events) and the rule applies only to implicit interface implementations. I think the rule makes some sense, because if you're implementing interface implicitly, it becomes a part of the public interface of the class, so the members of the interface are logically also a part of the class (and so you shouldn't separate them from the rest of the class).
On the other hand, if you're using explicit implementations, you're saying that you want to implement the interface, without making it a part of the class. In this case, it makes more sense to keep it separately from the rest of the members.
So, I guess the advice would be to use implicit interface implementations as often as possible (in the cases when it makes sense). For explicit implementations it may feel less inconvenient to use the suggested order.
That said, if you think it's better to group the members of an interface together, then I'd probably prefer just ignoring the StyleCop rule instead of using partial classes (which seems like crazy workaround to me). Afterall, StyleCop gives you just an advice...
(*) If my assumption is wrong, then I would probably just ignore the rule :-).
Personally, I don't see huge value in that type of rule, but that is just me. But it sounds like it is talking about nested types (it says "interfaces", not "interface implementations"), i.e.
class Foo {
//...
public interface IBar {...}
//...
}
(since the other comparison is enums)
in which case you can order it as it suggests. I just wouldn't ;-p (and nested interfaces is a rarity anyway)
If it means "explicit interface implementations", then you could just have them at this location and order them internally as already defined.
Other than partial classes or turning that rule off you could also write your own rule to replace that one.
精彩评论