Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Styles of ASP.NET design

Hey guys,

I have recently been working on an ASP.NET website. The site was partly done in PHP, but since I want to learn ASP.NET I decided to re-write it (I know re-writes are bad, but it's a personal, not-for-profit site). I plan to use the ASP.NET skills I acquire in this project in a larger commercial application in a couple of months.

However, I am finding that writing the code in ASP.NET is slower than doing it in PHP. I have a feeling this is through my own fault, and not a fault with the technology.

I think the problem lies in my coding habbits.

With PHP, I had created an 'include' folder, with a file called 'include.php'. In that PHP file was a list of functions that get used quite frequently, such as validating users, writing the title and menus, etc.

In ASP.NET, I created a C# source file just using a regular text editor. Inside the C# source file was a single class that inherited from System.Web.Page. Using a batch file, I called the C# compiler and created a DLL from the source and have it copied to my \Bin folder on the web server. This DLL serves as my 'include.php', and has all the same functions include.php used to have.

Then, I began work on my ASPX files. Every page is just an .aspx file - no .aspc or the rest of that stuff. The files all begin with this:

<%@ Page Language="C#" Inherits="BHeard.Page" Debug="True" %>

Where BHeard.Page is the class in the DLL I had created. Everything works fine. I can use all the Request and Response properties without hassle.

But it feels like I'm just doing ASP or PHP coding but with ASP.NET syntax. I never use any <script runat... or <asp:... tags, just render tags.

My questions:
By coding the way I am, does it seem I am missing out on a great deal of the functionality ASP.NET has to offer?

I've never been a fan of WYSIWYG editors, and I do prefer to hand code everything. But would it be much better in this case if I was to use the WebMatrix or Visual Studio .NET editors (I have them both installed, just never use them)?

And lastly, if I made my projects using the ASP.NET WebMatrix or VS.NET editors, with code behind and the .aspc's and all that other crap, would my pages still work on all the current browsers? One reason I have liked coding the way I have been is that as all the output is created by me, and theres not a single ASP.NET web control used, is I can be sure it will look the same in all browsers. But do the web controls work in all the common browsers? Am I just being pedantic?

Thankyou very much in advance!

PS: Any links to tutorials (not the ones at www.asp.net) that might help me would be awesome.

PHP at heart :(
Sunday, April 25, 2004

"By coding the way I am, does it seem I am missing out on a great deal of the functionality ASP.NET has to offer?"

Absolutely. The server control architecture, althought it's overly complex, is without question the biggest addition to the system. By side-stepping it, you're missing out on a lot of time savings.

Personally, I never use .aspx pages at all. I use pure code only. I find myself most productive this way.

"I've never been a fan of WYSIWYG editors, and I do prefer to hand code everything. But would it be much better in this case if I was to use the WebMatrix or Visual Studio .NET editors (I have them both installed, just never use them)?"

I don't like them either, but it's a decent way to start, to learn how the system is intended to be used. When you start dragging controls onto forms, and then hooking up server side events to do your work, then things will become clearer (and you won't need to drag and drop anything any more).

"And lastly, if I made my projects using the ASP.NET WebMatrix or VS.NET editors, with code behind and the .aspc's and all that other crap, would my pages still work on all the current browsers?"

Yes. You can force the system to generate HTML 4.0 all the time, rather than browser sniff.

If you have no Web.config file, create one, and put this in it:

<configuration>
  <system.web>
    <browserCaps>
      TagWriter=System.Web.UI.HtmlTextWriter
    </browserCaps>
  </system.web>
</configuration>

Brad Wilson (dotnetguy.techieswithcats.com)
Sunday, April 25, 2004

I felt exactly the same. I spent a lot of time trying to fine tune a good framework for web application design in ASP .NET.  Not too much options for structuring my application, and I always feel that .NET tries to force me to design following some hidden pattern (the IDE way, I guess).

But I had some other needs:

- First, I considered crucial that scripts shouldn't need to be compiled and no code-behind should be used (to allow
easy editing by non-programmers).

- Second, I wanted to use some abstraction layer for rendering html. Basically, some functions to render the header and the footer of the pages, which also included and initialized the main systems of the pages (referer control, user data, keywords/title loading...).

- Third, I wanted to benefit from an abstraction for form rendering and processing... ASP .NET does a lot on this but not in the way I would prefer (using server controls and little control over the javascript used for validation, difficult to add a lot of scripting on controls).

So I came up with a model similar to yours, but with some changes:

- A lot of classes inside some namespaces where designed, to take care of the business logic and the framework (own Page class (which inherits Page), User class, DateTime small wrapper, Data Access facilities... all of this developed inside the IDE and compiled into assemblies.

- Some other classes and systems in .aspx files (which had to be included with ugly syntax, different from the nice Import syntax).

- A presentation system: I though that I should write some Controls, but had problems with this.

- Scripts: I had a lot of problems with those.

I wanted pages that looked like this:

1) Includes/References
2) System Init: Page object, User object...
3) Script Process (UI Logic)
4) Rendering (Header / Body / Footer)
5) Cleaning up

So, I can include the files I need without problem . Then the problems start:

2) The page object is the page itself, no need to create any new object. But I have to create a User object, that should be global because some systems will need to use this. I need some initializing code for every site... Page constructor was not good place because I would need to recompile the assembly :-(. So I need a function... I came up with a 'FrameworkManager' class, not compiled, website dependant to have a place for this code. But now I have to create it and call this method, passing the Page object to it so this object can do it work. I finally ended up passing a lot of parameters to workaround this.

3) Script process: the problem here is with forms. I like to create forms by code (using some classes). So what I have to do is _not writing_ any web control tag, but a placeholder. Then I can use my own form class that reads the form definition and creates the web controls by code in that placeholder. Big problem: is very difficult to combine my form loader and some tags and html to make up the presentation. So I though that maybe I should ignore _all_ the form processing in .NET... I didn't like this.

4) Rendering: a new problem. I use to have some presentation functions: 'ShowHeader, ShowFooter, ShowBlockOpening, ShowBlockClosing, ShowTitle...' which I found very useful in the past. I though that this should become user controls in .NET. But I have to declare User Controls in separate files, and its really difficult to do things like 'passing some attributes with icons and title to a box control'. The code to read attributes from user control tags is more complicated than it should, I think. The real problem came when I wanted to create server controls inside this user controls and do things with their events. Very difficult.

So I got stuck. Of course I should give it more time (I'm a newbie in .NET), but this is what I felt when I first approached the website designing in .NET. Overall, there is no easy way to give things a different use that the one they were created for.

J
Sunday, April 25, 2004

Wow, a lot of the problems you have describe mine perfectly.

Last night I was up till 6 AM creating a User class, which just acts as an abstraction from all the old SQL queries I used to have. My user class has properties (Username, Password, etc.), and originally contained methods (Load(string ByUsername), Load(float ByID), and Save()).

But unfortunately, my Load and Save functions need access to the database. But the database lies on the web server, and uses Server.MapPath() to construct the data connection string. And Server.* is only avaliable to my Page class, not the User class, and I didn't want to have to pass the connection string to every new instance.

So I moved my Load and Save functions out of User and into my Page class, and they became LoadUser() and SaveUser(User userToSave).

It works but it feels bad. I think the problem is my design... is there a way I can act as if there will always be just one instance of Page, and treat its members as if they were global?

I had a look at the W3Schools tutorial on ASP.NET using server controls... and WOW! I re-wrote my registration page last night using the new controls, my own CustomValidators, and my new User() class, and everything works well and seems to work much better. I'm amazed at how useful these server controls are now. And it solves my problems with switching between HTML and ASP.NET with render tags all the time.

Any ideas on what to do with my global problem?

PHP at heart :(
Sunday, April 25, 2004

I initially had a lot of problems adapting to the ASP.Net way of doing things also. It took quite a while to get rid of some old habits and learn to work with ASP.Net and not against it.

I know you specifically said to not recommend the Tutorials at www.asp.net, but I would highly recommend that you take a look at the Source Projects and Starter Kits available for download on the www.asp.net site. Dowload three or four of them and get them up and running on your machine within the Visual Studio .Net IDE and you can see how ASP.Net was meant to be used most effectively. Make sure you also check out the Beta version of the 2nd generation of the forums (http://forums.asp.net). Somewhat overkill for most apps, but they cover all sorts of advanced ways of doing things. Especially nice is the fact that version of the forums is being developed with heavy input from some of the MS guys responsible for developing ASP.Net. Many of the solutions to issues they encountered in this rewrite, they incorporated into the next generation of ASP.Net (unfortunately not due until next year (maybe)).

FYI - There is a Principal (-le?) object is used for managing the logged in user's information and security.

Regards,
Matt

Matt
Sunday, April 25, 2004

Wow, I must say I'm so impressed, I had already read a chunk of the ASP.NET tutorials and downloaded one of the sample apps and had a good look at it, and I was already planning to download those forums, all before I even read your post!

I'm so into ASP.NET now - I fixed the problems I described above using an entry in web.config.

I'm planning to take these forums and modify them to suit my site (integrate their member system with the rest of my sites, etc.), which is a pretty hefty undertaking but should be a great learning experience.

Thankyou all so much for your replies - for the last few months programming hasn't been so much fun, but last night I was up 'till 6 AM and I couldn't sleep because of this new ASP.NET stuff. You've made programming fun again!

PHP at heart :D
Monday, April 26, 2004

"Any ideas on what to do with my global problem?"

Use HttpContext.Current. It returns the current HttpContext for the request, which includes some of the things you're used to seeing hang off of page; namely, Request, Response, Server, etc.

Use:

HttpContext.Current.Server.MapPath(...)

Brad Wilson (dotnetguy.techieswithcats.com)
Monday, April 26, 2004

Wow, thanks Brad Wilson, thats awesome! I should really learn to read more of the framework before jumping to conclusions!

I'm trying to download the source from http://forums.asp.net/Builds/Source/forums_latest_source.cab, but when I get to 20% it stops and the server disconnected it. It's been doing this since yesterday. If anyone has the source, or knows of an alternative mirror, could you please give me a link?

PHP at heart :D
Tuesday, April 27, 2004

*  Recent Topics

*  Fog Creek Home