我需要一个登录函数(登录只是一个示例,任何其他经常使用的方法都可以),它以电子邮件和密码为参数,并询问数据库是否有这样的用户。如果是的话,它必须返回customer_id(int),否则它将返回消息为什么登录不可能发生(例如:没有这样的电子邮件地址)。以最好的正确方法重用常用函数c#
我也不想每次都重写登录函数。我想在一个常用项目中写一次,我可以在每个项目中使用它并重用它。但我正试图找出最佳做法。到目前为止,我认为像下面这样的东西,但对我来说问题是,我不能返回客户ID,我会在我的项目(任何其他项目)中隐藏代码并使用它打开会话变量。我只能在下面的结构中返回字符串。我也认为返回一个DIC,但这也是错误的我猜,因为如果布尔(键)恰好是真的,customerID不是一个字符串(值)。你能帮我学习使用通用函数的正确方法,而不需要两次思考返回的消息和变量吗?非常感谢
public class UserFunctions
{
private enum Info
{
//thought of returning codes??
LoginSuccess = 401,
NoMatchPasswordEmail = 402,
InvalidEmail = 403,
};
public string TryLogin(string email, string password)
{
bool isValidEmail = Validation.ValidEmail(email);
if (isValidEmail == false)
{
return Result(Info.InvalidEmail);
// returning a message here
}
Customers customer = new Customers();
customer.email = email;
customer.password = password;
DataTable dtCustomer = customer.SelectExisting();
if (dtCustomer.Rows.Count > 0)
{
int customerID = int.Parse(dtCustomer.Rows[0]["CustomerID"].ToString());
return Result(Info.LoginSuccess);
// Here I cant return the customerID. I dont wanna open a session here because this function has no such a job. Its other projects button events job I guess
}
else
{
return Result(Info.NoMatchPasswordEmail);
}
}
private string Result(Info input)
{
switch (input)
{
case Info.NoMatchPasswordEmail:
return "Email ve şifre bilgisi uyuşmamaktadır";
case Info.InvalidEmail:
return "Geçerli bir email adresi girmelisiniz";
case Info.LoginSuccess:
return "Başarılı Login";
}
return "";
}
}
完全忘了这个选项。认为我在箱子外面思考的方式。 – Skintkingle 2012-03-27 20:23:51
然后对于每个功能组,您将不得不编写一个自定义类。例如,您想要一个通用篮子操作类和一个添加到购物车功能。你将不得不编写一个带有orderID的自定义类。 – 2012-03-27 20:34:12
对我来说,这听起来像是一个完全不同的函数,因此应该需要更多的代码。以上是通用“登录”的示例,它应该与“订单”分开。研究SOP,分离关注更多信息。 – Khan 2012-03-28 13:38:06