2012-03-27 47 views
0

我需要一个登录函数(登录只是一个示例,任何其他经常使用的方法都可以),它以电子邮件和密码为参数,并询问数据库是否有这样的用户。如果是的话,它必须返回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 ""; 
    } 
} 

回答

0

您可以尝试创建一个事件,然后调用代码可以在尝试登录之前注册到该事件。 例如:

public class UserFunctions 
{ 
    private enum Info 
    { 
     LoginSuccess = 401, 
     NoMatchPasswordEmail = 402, 
     InvalidEmail = 403,    
    }; 

    public delegate void LoginAttemptArgs(object sender, Info result, int CustomerID);//Define the delegate paramters to pass to the objects registered to the event. 
    public event LoginAttemptArgs LoginAttempt;//The event name and what delegate to use. 

    public void TryLogin(string email, string password) 
    { 
     bool isValidEmail = Validation.ValidEmail(email); 
     if (isValidEmail == false) 
     {     
      OnLoginAttempt(Info.InvalidEmail, -1); 
     }   

     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()); 
      OnLoginAttempt(Info.LoginSuccess, customerID); 
     } 
     else 
     {      
      OnLoginAttempt(Info.NoMatchPasswordEmail, -1); 
     } 
    } 
    private void OnLoginAttempt(Info info, int CustomerID) 
    { 
     if (LoginAttempt != null)//If something has registered to this event 
      LoginAttempt(this, info, CustomerID); 
    } 
} 

我不会编一个字符串返回,我将返回枚举结果,并让调用代码做什么,结果喜欢。读取枚举比解析返回的字符串快得多。

编辑:错别字,我错过了事件调用... .Twice

1

你可能要考虑返回一个自定义的类的实例。

public class LoginResult 
{ 
    public Info Result { get; set; } 
    public int CustomerId { get; set;} 
} 

修改您TryLogin方法返回的LoginResult一个实例。

基础上的结果你的应用程序流:

var loginResult = TryLogin(..., ...); 

switch (loginResult.Result) 
{ 
    case Info.LoginSuccess: 
     var customerId = loginResult.CustomerId; 
     //do your duty 
     break; 
    case Info.NoMatchPasswordEmail: 
     //Yell at them 
     break; 
    ... 
} 
+0

完全忘了这个选项。认为我在箱子外面思考的方式。 – Skintkingle 2012-03-27 20:23:51

+0

然后对于每个功能组,您将不得不编写一个自定义类。例如,您想要一个通用篮子操作类和一个添加到购物车功能。你将不得不编写一个带有orderID的自定义类。 – 2012-03-27 20:34:12

+0

对我来说,这听起来像是一个完全不同的函数,因此应该需要更多的代码。以上是通用“登录”的示例,它应该与“订单”分开。研究SOP,分离关注更多信息。 – Khan 2012-03-28 13:38:06