views:

472

answers:

6

I recently had a discussion on another forum with another developer and the topic was Code Reuse in ASP.NET. The stated scenario was that he needs to update code frequently on Production servers during server uptimes, and this results in Session getting reset for all users. He is avoiding putting shared code or classes into the App_Code folder or precompiled DLL's into the Bin folder because any updates will also refresh the Session.

The solution he has come up with is to put his shared code into UserControls and reference them wherever required. This enables him to update only the UserControl files which would be recompiled dynamically on next request without forcing a Session restart. Note that the Usercontrols are not intended to have any UI, they probably only house some business logic.

I tried to convince him against this because it felt intrinsically wrong to me - but I could not provide any hard facts to support my claim that this was a very bad way of doing things. The only thing I could think of is that it violates the principle of separation of business logic from UI. Am I grossly mistaken or are there concrete reasons why this should not be done? Links or examples would be helpful.

Note: Using out-of-process Session state is not an option at present, nor have they been able to decide on scheduled downtimes. Also, since this is a site under active development, they don't seem to be using any sort of professional deployment model yet.

Thanks in advance.

Edit: Additionally, it would be helpful if someone could clarify exactly why the Session restarts in the above mentioned cases.

+2  A: 

It sounds like the main problem is that he's updating production code too frequently. Other than that, UserControls seem like a perfectly reasonable place to put business logic, especially if you have a good naming convention for them or can put them in a common folder.

Joel Coehoorn
Thanks for the answer, Joel, but then why *have* the App_Code folder at all?
Cerebrus
Because you can put things beside controls in the App_Code folder. I didn't say you'd always want to do this. Just that given the constraints it doesn't seem out of line.
Joel Coehoorn
+2  A: 

May i ask, why isn't out-of-process session state an option, really? Since this guy seems to put in so much effort to get around this "problem", wouldn't he be better off looking at better solutions? out-of-process session state is the only good solution.

baretta
I think it's a temporary phase, but still, given the conditions, do you think it the correct way of code reusability ?
Cerebrus
I would disagree with "the only good solution"
BlackTigerX
As far as code reusability goes, i would recommend a class library containing reusable controls and base pages, maybe HTTP modules etc.
baretta
BlackTigerX - your'e right, there are a few alternative good solutions, depending on the type of app and the context.
baretta
+2  A: 

It does seem like an unusual approach, and persistent session is the obvious answer. Assuming that reasons not to use persistent session are legitimate, sometime you just have to go with whatever works. I'd make a point of clearly documenting in the source files the unusual use of usercontrols and live with it.

To answer the why does session get reset edit. With in process session all the session data is in memory as part of your application. Various changes to the web site (e.g. web.config and others I don't recall off the top of my head) cause the application to restart wiping out all current state in your application. Persisting to SQL or the out of process session state server would allow the application to reset and lose any state without affecting the session data.

ScottS
+1  A: 

I'll agree with Dennis, there are really no issues moving from inproc to the state server. Not sure what your dev/deployment platforms are, but they should include a session state service - start that up, change your web.config, and the problem is solved.

chris
When using session state server, or sql, everything that you put into session needs to be serializable. It's usually easy to deal with, but not unusual to see it fail when first switching over an existing app.
ScottS
Yea, but still, i'd rather catch a few serialization problems, than addressing this the way your friend does.
baretta
+1  A: 

it's a clever (and ugly) solution to a common problem

The main problem is the architecture of such system; the code that needs to be updated can be put on a different service outside his web app, his code behind can then call these services, and the services can be updated when needed without affecting the web app

BlackTigerX
+1  A: 

Every base has been covered already, but I really hate bad practices like this. If the guy can't simply change to a state server to fix the problem that he has, then he doesn't really deserve the help. What would happen if he put his class in the root folder of the project and compiled it independently? Either way, I would think this guy is a bad developer for not thinking about scalability, and not planning for downtime. What I'm guessing is he doesn't have a development environment available. Tsk tsk tsk.

As an answer to your question, as stated by everyone else, put the code in a user control, and document well.

BBetances