Recommendations - Extension Methods versus Partial Class

We discuss at work the best way to define methods for an entity class β€” as methods to extend or use partial classes. The types of methods that we are talking about do not change the state of an object; they are purely β€œhelper” methods that request state and return a value.

The main advantage of both approaches is to keep the entity class clean while maintaining support for client code.

I have no particular preference in any case, but I am curious to find out if others have a preference (or know documented recommendations) over this or that.

I started writing a list of merits for each approach that I could think of, but in the end, all I came up with was:

Partial classes

  • The method definition is inside the class (even if it is a different file), so support for the Visual Studio tool for the "search method" (for example, ALT- \ in resharper) will find the method

  • The existence of another file containing helper methods is obvious as soon as an entity class is opened due to the use of a partial keyword

Extension Methods

  • The naming of the file ("entityNameExtension") and its location in the project (in the "Extensions" subfolder) are intuitive and easy to search

Can anyone else add their opinion to this?

PS , , , , , , ​​ :

. , , , . - , , .

+4
2

, . -, , , , , , . -, , . , , , , , - , , , .

- , , LINQ Enumerable. , . - string.IsNullOrWhitespace , .

, , , . , , , , , , .

- . , , , . , , . , . , , , , , -.

, , . , . , , . , , , . , . , .NET- , , - .

, , , , .

+5

All Articles