从PageMethods调用业务逻辑层方法
我在 Web 表单应用程序中有一个静态页面方法,我想从中调用私有类级别变量的方法,如下所示。我正在使用 jQuery 来调用页面方法。
private readonly ICatalogBLL _catalogBLL = new CatalogBLL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_catalogBLL.GetSomething();
}
}
[WebMethod]
public static UpdateSomething(int i)
{
//Want to do as below. But can't call it from a static method.
_catalogBLL.UpdateSomething();
}
更新
如果我按照 John Saunders 的说法调用它,它不会像在静态方法中那样对来自不同用户的请求使用相同的实例吗?
I've a static page method in web form application and I want to call method on private class level variable from it as shown below. I'm using jQuery to call the page method.
private readonly ICatalogBLL _catalogBLL = new CatalogBLL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_catalogBLL.GetSomething();
}
}
[WebMethod]
public static UpdateSomething(int i)
{
//Want to do as below. But can't call it from a static method.
_catalogBLL.UpdateSomething();
}
UPDATE
If I call it as said by John Saunders, won't it use the same instance for requests from different users as it is within a static method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。页面方法是静态的。您的
_catalogBLL
是实例成员。但是,既然您在每个请求上都创建了一个新的
CatalogBLL
实例,为什么不再这样做一次呢?You can't. The page method is static. Your
_catalogBLL
is an instance member.However, since you create a new instance of
CatalogBLL
on every request, why not do so once more?您无法调用,因为页面方法是静态的...
静态方法只是与其包含类的任何实例解除关联的方法。更常见的替代方案是实例方法,该方法的结果取决于其所属类的特定实例的状态。
看看约翰桑德的回答..
You can't call because pagemethods are static...
A static method is simply one that is disassociated from any instance of its containing class. The more common alternative is an instance method, which is a method whose result is dependent on the state of a particular instance of the class it belongs to.
Look at John saunder's answer..