Tutorial :Organizing c# project helper or utility classes



Question:

What are some best practices for where you should have helper classes in a .NET project? Referring to classes separate from business layer stuff, but presentation and app stuff like appSetting config managers and other code that would sometimes be module specific or sometimes be used throughout the app.


Solution:1

I always allow things like this to be pretty fluid. That said:

  1. I test "helper" classes the same as any other class. This makes them tend to not be static.
  2. I may start by creating these helpers as individual methods when needed. As I find they are needed in more than one class, I'll move them into their own class or a "Utilities" class in the same project.
  3. If I find they are needed in more than one project, then I move them higher up in the "hierarchy": from project to solution, from solution to subsystem, from subsystem to application, from application to library or framework, etc.


Solution:2

I almost always have a MyProject.Core class library in my solution where I put things like that.

Edit: I might have answered a "bigger" question.

In a single project it all depends on the size of the project. The Microsoft Design Guidelines talks about that you shouldn't create a namespace if you have less then five(correct me if I'm wrong about this number) types within it.


Solution:3

I tend to do a combination of what Randolpho and Ben do: I use static helper classes in a "Utilities" folder in a Utilities namespace. Better file organization, keeps the rest of the application namespace clear.


Solution:4

Most of us just throw them in a "Helpers" folder.

Depending on the helper, you might want to mark the methods virtual so you can mock it if necessary. That or bind to an interface it implements, but if you only have one concrete per interface it might be overkill.

Unless the helper methods are pure calculation with no external dependences, under no circumstances should they be static.

Even then, reconsider.


Solution:5

I tend to put them in a utils namespace. Either in the mainproject namespace if they are pretty general e.g. MyProject.Utils.MyHelperClass, or if they are more specific then a sub namespace MyProject.CRM.Utils.MyCRMHelperClass.


Solution:6

We put such classes in an assembly called Common for example intended to be referenced by all projects which need it except of cases when helper need to use some buisness objects or core objects.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »