开发者

Is it right to instantiate active classes outside of Page_Load?

a quick question for an ASP.NET expert. It is an arguable moment for us in the company at the moment...

We have built a nice (no bugs in there) CMS framework that we use for our sites...

It goes something like this:

MyCms.Content.Channels channels = new MyCms.Content.Channels();

where at the moment of instantiating the Channels class, it loads a bunch of XM开发者_运维百科L files and converts them to a List<MyCms.Content.Channels.Channel> that is held within the Channels class, and cached using System.Web.HttpRuntime.Cache (until there are any changes to the folder holding the XML files)

The Channels class is basically a hierarchical structure for web pages...

We normally use it like this in our ASP.NET pages (code behind):

public partial class Default : System.Web.UI.Page
{
    public MyCms.Content.Channels channels;
    public MyCms.Content.Images images;

    public MyCms.Content.Channels.Channel CurrentChannel;
    public List<MyCms.Content.Channels.Channel> latestItems;

    public MyCms.Content.GameVotes votes;
    public MyCms.Content.GameVotes.Vote vote;

    protected void Page_Load(object sender, EventArgs e)
    {
        channels = new MyCms.Content.Channels();
        images = new MyCms.Content.Images();
..
}

as you can see, the public variable 'channels' is instantiated at Page_Load()... where at the moment it has loaded a bunch of XML files either from a file system, or from cache...

Our colleague though, is sometimes instantiating this class outside of Page_Load() - right next to the public declaration of the 'channels' variable like this:

public partial class Default : System.Web.UI.Page
{
    public MyCms.Content.Channels channels = new MyCms.Content.Channels();

    protected void Page_Load(object sender, EventArgs e)
    {

... he does the same in various User Controls...

Now thing is.. I need your opinion on whether it's ok to instantiate a very active class like this - outside of Page_Load() event... ? The site that was built by our colleague was hanging the entire IIS from time to time, and I just suspect that this could be one reason... - what do you think, please ? :)

The same CMS Framework is being used on other sites, on other servers with no issues at all... So the only difference I could find between the good working sites and the one that is hanging - is this .. instantiation of 'channels' class outside the scope of Page_Load()...


It shouldn't make a difference. That's not to say it doesn't.

All that's happening is you're changing the point at which you create the object from load event, which happens about half way through the lifecycle to the constructor, right at the very start. At both points the cache should be available as it's part of the context, although you really ought to check that.

I would say though, instantiating such an important class should happen at a specific point, like page initialise or load, rather than at constructor.

Simon


The only difference I'm aware of is that if you initialize the objects outside of the Page_Load they will be created as soon as your page's class is created (i.e. before all the Page_XXX events), and if you initialize them inside the Page_Load they will be created only when the event is called.

That means if your application crashes, redirects or for whatever reason does not enter Page_Load, you have created the object uselessly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜