DEV Community

Discussion on: What every ASP.NET Core Web API project needs - Part 6 - IServiceCollection Extension

Collapse
 
mwrpwr profile image
Joseph Maurer

I remember learning about Extension methods and thinking that they were amazing, and in practice I find them to do more harm than good. It's the definition of syntactical sugar and gives you the clean appearance that everyone wants, but the problem with it is that it breaks encapsulation. I understand why you used it in this instance, but I think a basic static helper class would be a cleaner implementation. Good Post 👍🏻

Collapse
 
moesmp profile image
Mohsen Esmailpour • Edited

A big drawback of a static method is testability. Mocking static method or extension method is hard, so most of the time developers avoid spreading the business logic into static method or extension method and also there is no much difference between static helper method and extension method:

var a = customer.CreateDate.ToACustomFormat();
var b = DateTimeHelper.ToACustomFormat(customer.CreateDate);
Enter fullscreen mode Exit fullscreen mode

Here is some useful link about extension method:
Extension Methods Guidelines in C# .NET
Extension Methods General Guidelines