Reputation: 1
I want to make a class with functions like Add(), Delete() etc. to store users in a database.
So, what is a better naming convention and structure?
Both in one class:
**EXAMPLE**
User.Add("Username", "Password");
User user = User.GetUser();
class User
{
public User(string username, string password)
{
Username = username;
Password = password;
}
public string Username { get; set; }
public string Password { get; set; }
public static void Add(string username, string password)
{
//Add user to database
}
public static User GetUser()
{
//Get user from database
}
}
or separated in two classes and use the *Manager suffix...
**EXAMPLE**
UserManager.Add("", "");
User user = UserManager.GetUser();
class UserManager
{
public static void Add(string username, string password)
{
//Add user to database
}
public static User GetUser()
{
//Get user from database
}
}
class User
{
public User(string username, string password)
{
Username = username;
Password = password;
}
public string Username { get; set; }
public string Password { get; set; }
}
or are both examples bad and you have a better one?
Upvotes: 0
Views: 663
Reputation: 44605
I prefer the second way, with simple entities with no logic and Manager classes, in fact MS Entity Framework does more or less the same, entities only have properties and the DBContext generated allows you to query, add and edit entities.
Little note: do not make the methods of the UserManager static, class should be a normal class with instance methods so you can test easily and inject as/if needed.
little second note: you are not going to retrieve user's password in clear text or store it in clear text in the database right?!?! :D
Upvotes: 1
Reputation: 52280
Classes with name "manager" in it often point to some not well known design.
I would go with the classic Repository-pattern and dependency-injection.
That is basically:
Upvotes: 1