用最佳正确方法重用公共函数c#

本文关键字:函数 方法 最佳 | 更新日期: 2023-09-27 18:29:15

我需要一个Login函数(登录只是一个例子,任何其他常用的方法都可以),它以电子邮件和密码为参数,并询问DB是否有这样的用户。如果是,它必须返回customer_id(int),如果不是,它将返回无法登录的消息(例如:没有这样的电子邮件地址)。

我也不想每次都重写登录功能。我想在一个普通的项目中写一次,我可以在每个项目中使用它并重用它。但我正在努力寻找最佳实践。到目前为止,我想了如下的事情,但对我来说,问题是我不能返回customerID,我将在我的项目(任何其他项目)的codeehind中获得它,并用它打开一个会话变量。我只能返回下面结构中的字符串。我也想过返回Dic,但我想这也是错误的,因为如果bool(键)恰好为true,那么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 "";
    }
}

用最佳正确方法重用公共函数c#

您可能需要考虑返回自定义类的实例。

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;
    ...
}

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

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);
    }
}

我不会编译要返回的字符串,我会返回枚举结果,并让调用代码对结果为所欲为。读取枚举比解析返回的字符串快得多。

编辑:Typos和我错过了一个活动电话。两次