Per-request Code Execution in ASP.NET July 12, 2006
Posted by Siva in .NET, ASP.NET, Design.trackback
I have seen some ASP.NET applications doing business logic checks in each and every page with repeated code; the code is literally copied and pasted in all the pages, aiming to do the work for every ‘request’. The code does various business rule checks based on the data stored in the session state. E.g.: give or deny access to a feature based on the current user’s role in the session state. I believe this pattern is largely inherited from the classic ASP world. To reduce the ‘perceived’ code repetition, some developers wrap the repeating code blocks inside a separate class and call methods on it from various pages. But, the net effect is the same: duplicate code. What if a dev forgets to include the repeating code in new pages included later?
For such scenarios, the best bet is using HTTP modules: wrap all per-request validation rules inside a HTTP module and hook it into the ASP.NET request pipeline via web.config. This way, your business rules are executed and appropriate action is taken even before the control comes to the page level. If you want to pass the result of the business rule execution down to page levels and use it further, you can use Session state as the communication channel.
This approach promotes a clean application design and improves the overall application performance.
What do you think?
Comments»
No comments yet — be the first.