-1

For example, I have a password generator in my user registration website. Where should I put the function of generating password? Put together with UserController?

What is the correct way to put these functions?

tereško
  • 58,060
  • 25
  • 98
  • 150
user2584307
  • 82
  • 1
  • 5
  • I would create a helper class with static methods which operate on the password generation/encryption etc... – Nilesh Aug 08 '13 at 16:36
  • It depends. Does it need to be reused? Does the implementation for how a password (hash I presume) is generated need to be swapped out i.e. change algorithms? – Russ Cam Aug 08 '13 at 16:37
  • It will be reused in other area. How to create helper class with static methods??? – user2584307 Aug 08 '13 at 16:40

2 Answers2

1

I would put it in my User Model.

Or you could create a Utility class and put it in there.

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
1

I would recommend putting it into a class of its own. For the sake of SRP, your UserModel should do things with a User and only a User. Your UserModel class should not be responsible for generating passwords for new users. Separate it into its own class and call a method on that class during the creation of your new user in your UserModel.

  • You suggest putting in Controller? – user2584307 Aug 08 '13 at 16:54
  • I agree with this comment. What if in the future you want a windows service to generate a password, why on would it use the `public class UserModel { }` instead of a `public class PasswordGenerator { }`? – Erik Philips Aug 08 '13 at 17:33
  • Well you're generating passwords for Users, so why wouldn't you put it in your User Model? – Jack Marchetti Aug 08 '13 at 20:27
  • Generating a password does not necessarily have anything to do with a user, so it's good practice to create [separate models for each concern](http://en.wikipedia.org/wiki/Separation_of_concerns). – Erik Philips Aug 12 '13 at 00:36