Do Extension Methods Hide Dependencies?

后端 未结 4 611
执念已碎
执念已碎 2020-12-17 14:19

All,

Wanted to get a few thoughts on this. Lately I am becoming more and more of a subscriber of \"purist\" DI/IOC principles when designing/developing. Part of thi

4条回答
  •  死守一世寂寞
    2020-12-17 14:50

    I see where you are coming from, however, if you are trying to mock out the functionality of an extension method, I believe you are using them incorrectly. Extension methods should be used to perform a task that would simply be inconvenient syntactically without them. Your TruncateToLength is a good example.

    Testing TruncateToLength would not involve mocking it out, it would simply involve the creation of a few strings and testing that the method actually returned the proper value.

    On the other hand, if you have code in your data layer contained in extension methods that is accessing your data store, then yes, you have a problem and testing is going to become an issue.

    I typically only use extension methods in order to provide syntactic sugar for small, simple operations.

提交回复
热议问题