与实体合作时,建议使用以下哪项?(以下代码在 UI 层。UserManager
在业务层)
1-
protected void btnReturn_Click(object sender, EventArgs e)
{
var user = new User();
user.Name = txtName.Text;
user.Address = txtAddress.Text;
...
new UserManager().AddUser(User);
}
在用户管理器中:
public void AddUser(User user)
{
_repository.Add(user);
_repository.SaveChanges();
}
public void DeleteUser(User user)
{
_repository.Delete(user);
_repository.SaveChanges();
}
2-
protected void btnReturn_Click(object sender, EventArgs e)
{
new UserManager().AddUser(txtName.Text, txtAddress.Text, ...);
}
在用户管理器中:
public void AddUser(string name, string address,...)
{
var user = new User();
user.Name = name;
user.Address = address;
context.Users.Add(user);
context.SaveChanges();
}
public void DeleteUser(int userID)
{
var user = rep.First(u => u.UserID = userID)
_repository.Delete(user);
_repository.SaveChanges();
}