Static / Shared Helper Functions vs Built-In Methods

Posted by Nathan on Programmers See other posts from Programmers or by Nathan
Published on 2011-07-07T14:07:19Z Indexed on 2014/06/05 3:38 UTC
Read the original article Hit count: 225

Filed under:
|

This is a simple question but a design consideration that I often run across in my day to day development work. Lets say that you have a class that represents some kinds of collection.

Public Class ModifiedCustomerOrders
     Public Property Orders as List(Of ModifiedOrders)
End Class

Within this class you do all kinds of important work, such as combining many different information sources and, eventually, build the Modified Customer Orders.

Now, you have different processes that consume this class, each of which needs a slightly different slice of the ModifiedCustomerOrders items. To enable this, you want to add filtering functionality. How do you go about this? Do you:

  1. Add Filtering calls to the ModifiedCustomerOrders class so that you can say:

    MyOrdersClass.RemoveCanceledOrders()

  2. Create a Static / Shared "tooling" class that allows you to call:

    OrdersFilters.RemoveCanceledOrders(MyOrders)

  3. Create an extension method to accomplish the same feat as #2 but with less typing:

    MyOrders.RemoveCanceledOrders()

  4. Create a "Service" method that handles the getting of Orders as appropriate to the calling function, while using one of the previous approaches "under the hood".

    OrdersService.GetOrdersForProcessA()

  5. Others?

I tend to prefer the tooling / extension method approaches as they make testing a little bit simpler. Although I dependency inject all my sourcing data into the ModifiedCustomerOrders, having it as part of the class makes it a little bit more complicated to test. Typically, I choose to use extension methods where I am doing parameterless transformations / filters. As they get more complex, I will move it into a static class instead.

Thoughts on this approach? How would you approach it?

© Programmers or respective owner

Related posts about design

Related posts about vb.net