Wednesday, October 26, 2005

ASP style of development?

A lot of my co-workers complained about my ASP style in ASP.Net development. The only reason for such a complaint is that I am using data-binding statements on ASPX pages and user controls, like this: <%# DataBinder.Eval(Container.DataItem, "City")%>. I never use any server-side scripting code on the page itself, all server-side code goes to code classes. However I do like to use data-binding statements to achieve better separation of presentation from business logic. Indeed, what is the alternative? The alternative is to handle OnDataBound() events, find the controls within each row and manually set properties on them. This is not ASP style, this is VB6 style of developing Windows apps. Is it better? No, you still have your presentation mixed up with the business logic, the methods become very long, there is indeed a lot of code written to do something which I can easily accomplish by using data-binding statements.

I would actually go to the extreme by trying to separate business logic from presentation. I would not use any code-behhind. I'll have all pages and user controls inherit from pages, but I would prevent Visual Studio from coupling these classes to the pages. This way if I add a control to the page it will not automatically appear in the class. All events should be registered by using control's attributes, like "onclick". The protected control variables will appear in the base classes only when I need to read properties of the controls. This way we can have the same base class for multiple pages or controls, avoiding duplication of the business logic, while being less strict about presentation (where some duplication is always allowed).

No comments: