开发者

Static functions in asp.net

I have an asp.net webforms application where the aspx pages get their data from a hierarchy of plain .net objects.

Currently the hierarchy is fully dynamic, being instantiated and stored once for each usersession, but I would like to use a function in one of开发者_StackOverflow the classes without creating the full hierarchy. (I want to avoid repeating the function that is in this class, but I need to use it outside the context of the class).

As an illustrative example, consider a webshop-type application with a shopping cart class. Inside the shopping cart class there is a function that applies discounts like say "buy 3 of this get 1 free". I want to be able to use this discount-function without creating a whole shopping cart and without repeating the code somewhere else.

Considering that this is a web-based application running under IIS7, which is the best approach?

Should I:

  • Break out this function and put it in a separate class (containing only static helper functions)?
  • Just make this one function static in the current class?
  • Use some other approach to avoid the duplication?


It sounds to me like an Utility method. If it is, just create an Utils class. Look at : Creating An Utilities Class?


Without seeing your code I'm not exactly sure the best approach. Would you be able to post a simplified version of your code?

It might make sense to create a base class that contains the function you need that all the other classes inherit from.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜