Whats 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.
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.
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
.
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.
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.
I always allow things like this to be pretty fluid. That said:
- I test "helper" classes the same as any other class. This makes them tend to not be static.
- 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.
- 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.
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.