Note that there are some explanatory texts on larger screens.

plurals
  1. POGuidelines - extension methods vs partial class
    primarykey
    data
    text
    <p>We are debating at work the best way to define methods for an entity class - as extensions methods or using partial classes. The kind of methods we're talking about don't modify the state of the entity, they are purely "helper" methods that interrogate the state and return a value.</p> <p>The main benefit to both approaches is to keep the entity class clean, while still providing intellisense support to client code.</p> <p>I don't have a strong preference either way but am curious to know if others have a preference (or know of documented guidelines) towards one or the other.</p> <p>I started writing the list of merits for each approach that I could think of, but in the end all I've come up with is:</p> <p><strong>Partial Classes</strong></p> <ul> <li><p>The method definition resides within the class (even if it's another file) so Visual Studio tool support for "find method" (e.g. ALT-\ in resharper) will locate the method</p></li> <li><p>The existence of the other file containing helper methods is obvious as soon as the entity class is opened due to use of the partial keyword</p></li> </ul> <p><strong>Extension Methods</strong></p> <ul> <li>The naming of the file ("entityNameExtension") and its whereabouts in the project (in an "Extensions" sub-folder) are intuitive and easy to search for</li> </ul> <p>Can anyone else add their opinion to this?</p> <p>PS I don't feel this is a duplicate of the following question, as the asker of that question was content to mark a response which outlined the functional differences as the correct answer, which doesn't answer the question about which approach is best practice in this scenario: <a href="https://stackoverflow.com/questions/6017673/partial-class-vs-extension-method">Partial Class vs Extension Method</a></p> <p><strong>EDIT - I'm seeking people's preference towards one approach or the other, as there are no documented guidelines that we can find for this particular scenario. Both approaches are possible and neither violates any design principles, so it is a matter of preference and I'd like to know yours.</strong></p>
    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.
 

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