Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用最佳正确方法c重用公共函数#_C#_Asp.net_Reusability - Fatal编程技术网

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

C# 使用最佳正确方法c重用公共函数#,c#,asp.net,reusability,C#,Asp.net,Reusability,我需要一个登录功能(登录只是一个例子,任何其他常用的方法都可以),它以电子邮件和密码为参数,询问DB是否有这样的用户。如果是,它必须返回customer_id(int),如果不是,它将返回为什么无法登录的消息(例如:没有这样的电子邮件地址) 我也不想每次都重写登录函数。我想在一个普通项目中编写一次,我可以在我的每个项目中使用它并重用它。但我正试图找出这方面的最佳做法。到目前为止,我的想法如下,但我的问题是,我无法返回customerID,我将在我的项目(任何其他项目)中的codebehind中获

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

我也不想每次都重写登录函数。我想在一个普通项目中编写一次,我可以在我的每个项目中使用它并重用它。但我正试图找出这方面的最佳做法。到目前为止,我的想法如下,但我的问题是,我无法返回customerID,我将在我的项目(任何其他项目)中的codebehind中获得该ID并用它打开会话变量。我只能返回以下结构中的字符串。我还想返回一个Dic,但我想这也是错误的,因为如果bool(key)恰好为true,customerID不是字符串(value)。你能帮我学习使用普通函数的正确方法吗?不需要反复考虑返回的消息和变量?非常感谢

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

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

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);
    }
}
我不会编译要返回的字符串,我会返回枚举结果,并让调用代码对结果执行它喜欢的操作。读取枚举比解析返回的字符串快得多


编辑:输入错误,我错过了一个活动电话。两次

您可能想考虑返回一个自定义类的实例。
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;
    ...
}

完全忘记了这个选项。我想我的想法太离题了。那么对于每个函数组,你都必须编写一个自定义类。例如,您需要一个通用的basket操作类和一个add-to-cart函数。这次您将不得不编写一个具有orderID的自定义类。对我来说,这听起来像是一个完全不同的函数,因此需要更多的代码。上面是一个通用的“登录”示例,它应该与“订单”分开。研究SOP,分离关注点以获取更多信息。