使用最佳正确方法重用常用函数c#

时间:2012-03-27 20:07:42

标签: c# asp.net reusability

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

我也不想每次都重写登录功能。我想在一个公共项目中编写一次,我可以在我的每个项目中使用它并重用它。但我正在努力寻找最佳实践。到目前为止,我想到了类似下面的内容,但问题是我无法返回customerID,我将在我的项目(任何其他项目)中获得代码隐藏,并用它打开一个会话变量。我只能在下面的结构中返回字符串。我还想过返回一个Dic,但这也是错误的我猜是因为如果bool(key)碰巧是真的,那么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 "";
    }
}

2 个答案:

答案 0 :(得分: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;
    ...
}

答案 1 :(得分: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