从PageMethods调用业务逻辑层方法

发布于 2024-09-09 19:04:38 字数 524 浏览 9 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牛↙奶布丁 2024-09-16 19:04:38

你不能。页面方法是静态的。您的 _catalogBLL 是实例成员。

但是,既然您在每个请求上都创建了一个新的 CatalogBLL 实例,为什么不再这样做一次呢?

[WebMethod]
public static UpdateSomething(int i)
{
   CatalogBLL catalogBLL = new CatalogBLL();
   catalogBLL.UpdateSomething();
}

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?

[WebMethod]
public static UpdateSomething(int i)
{
   CatalogBLL catalogBLL = new CatalogBLL();
   catalogBLL.UpdateSomething();
}
追风人 2024-09-16 19:04:38

您无法调用,因为页面方法是静态的...

静态方法只是与其包含类的任何实例解除关联的方法。更常见的替代方案是实例方法,该方法的结果取决于其所属类的特定实例的状态。

看看约翰桑德的回答..

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文