简体   繁体   English

ASP.NET中的静态方法

[英]Static Methods in ASP.NET

i am a little confused about static methods within asp.net-pages. 我对asp.net-pages中的静态方法有点困惑。 Eg what if i create a static Database-Method to get Userdata out of the database (something like UserDBHandler.getUser()) - is it safe to call that method from within web-pages? 例如,如果我创建一个静态数据库方法来从数据库中获取Userdata(类似于UserDBHandler.getUser()),那么从网页中调用该方法是否安全? Isnt a new thread created for every page-call? 是不是为每个页面调用创建了一个新线程? And does HttpContext.Current always return the current-users context, so is it safe to call that from static methods to get the current-users session?? 并且HttpContext.Current总是返回当前用户上下文,因此从静态方法调用它来获取当前用户会话是否安全?

thanks 谢谢

is it safe to call that method from within web-pages 从网页中调用该方法是否安全

Only if this method is reentrant . 仅当此方法是可重入的时 Example with sql: 使用sql的示例:

public static User GetUser(string username)
{
    using (var connection = new SqlConnection(ConnectionString))
    using (var command = connection.CreateCommand())
    {
        connection.Open();
        command.CommandText = "select name, username from users where username = @username";
        command.Parameters.AddWithValue("@username", username);
        using (var reader = command.ExecuteReader()) 
        {
            while (reader.Read())
            {
                return new User 
                {
                    Username = username,
                    Name = reader.GetString(0),
                }
            }
        }
        return null;
    }
}

And call in your ASPX page: 并在您的ASPX页面中调用:

var user = SomeClass.GetUser(Session["username"]);

And does HttpContext.Current always return the current-users context, so is it safe to call that from static methods to get the current-users session? 并且HttpContext.Current总是返回当前用户上下文,因此从静态方法调用它来获取当前用户会话是否安全?

Yes, HttpContext.Current can be safely used to get the current HTTP context. 是的,HttpContext.Current可以安全地用于获取当前的HTTP上下文。 But I would suggest you not calling HttpContext.Current in your DB access method. 但我建议你不要在数据库访问方法中调用HttpContext.Current。 Just pass what is needed as argument so that your ASPX page when calling the method will safely read the session and pass the needed parameters. 只需传递所需的参数作为参数,以便在调用方法时您的ASPX页面将安全地读取会话并传递所需的参数。

Remark and personal advice: don't use static methods for data access. 备注和个人建议: 不要使用静态方法进行数据访问。 Calling code using static methods is close to impossible to unit test. 使用静态方法调用代码几乎不可能进行单元测试。

is it safe to call that method from within web-pages 从网页中调用该方法是否安全

That really depends on what you do in the method. 这实际上取决于你在方法中做了什么。 This method should probably be a function without side-effects. 这种方法应该是没有副作用的功能。

Isnt a new thread created for every page-call 不是为每个页面调用创建的新线程

Yes. 是。

does HttpContext.Current always return the current-users context, so is it safe to call that from static methods to get the current-users session??

Yes. 是。

If you want to be a purist though, it is not advisable to rely on static methods since they make your code hard to test in isolation. 如果你想成为一个纯粹主义者,不建议依赖静态方法,因为它们使你的代码很难单独测试。 If class A calls a static method on class B, you will never be able to test class A without also testing / calling B. 如果A类在B类上调用静态方法,那么在没有测试/调用B的情况下,您将永远无法测试A类。

在单个会话的范围内,我相信您将在单个线程上运行,因此这不应该是一个问题。

It depends on how the method is written. 这取决于方法的编写方式。 If the method is written in a thread safe manner, then you should have no problems. 如果该方法是以线程安全的方式编写的,那么您应该没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM