Video: Creating an N-Layer ASP.NET Application (Updated)

In this video tutorial I walk through the fundamentals of creating an N-Layer ASP.NET application.  What's "N-Layer" you ask?  N-Layer can be interpreted many different ways, but I generally use the term to mean separating presentation, business and data code into individual code layers.  Doing this allows code to be re-used throughout an application and prevents unnecessary clutter in ASP.NET code-behind classes.  This video covers creating presentation, business and data layers and also covers another layer I normally add to projects that I refer to as "Model".  The model layer contains data entity classes that are used to pass data between the different layers. 

If you're currently embedding all of your code directly in ASP.NET pages, this video will help get you started on the road to recovery.  I'm kidding of course, but if you want to build more re-useable and maintainable applications you'll want to segregate your code into different layers at a minimum.  Other types of architectures can certainly be applied as well.

View the Video

 

My article in asp.netPRO Magazine that compliments the video can be viewed here.

 
Thanks to Orcsweb for hosting my website (http://www.xmlforasp.net) and for giving me space to host videos!  If you're looking for a host I highly recommend them.  You won't find better service anywhere.

Published Monday, February 5, 2007 9:00 PM by dwahlin

Comments

# re: Video: Creating an N-Layer ASP.NET Application

Tuesday, February 6, 2007 2:46 PM by Mike

Great video! 30 minutes covers everything. We need more videos like this, not just the Hello World video's using drag 'n drop. I have a couple of questions:

1. Why not store the schema in the Model folder?

2. Why bind the data tier to a webapp by using webconfigurationmanager and httpcontext?

3. Why bother with an array, wouldn't it be better to just pass the generic list?

Could you do a video where you have a totally different model from the database?

Thanks again! Adding blog to favorites....

# re: Video: Creating an N-Layer ASP.NET Application

Tuesday, February 6, 2007 6:04 PM by dwahlin

Glad you enjoyed the video.  As far as your questions, if I stick the schema in the app_code folder it'll automatically generate a strongly-typed DataSet for me.  While that's fine, it's not what I wanted to show here.  I prefer a pure streaming approach where possible with small objects being passed between layers to keep overhead down to a minimum.

As far as question 2, there are certainly different ways to handle that (such as the more generic ConfigurationManager...I probably should've used that actually) but, the goal was to keep it fairly simple.  Trace listeners or writing to the event log could certainly be used instead of directly calling Trace.Warn().  Since I knew the video would already be fairly long I went with the quick approach. :-)  I typically am working on Web apps or Web Service apps so HttpContext is always available...but to make it "truly" re-useable across maybe desktop apps then I'd certainly choose a different approach for logging errors.  Since this was specifically for ASP.NET I went the route shown in the video though.

For the List<Model.Customer>, that would be fine to return of course, but I prefer to keep the overhead of objects passed between the layers as minimal as possible.  Just a personal choice.  If I knew the collection would have to be increased potentially then I'd pass the List<Model.Customer>.

# re: Video: Creating an N-Layer ASP.NET Application

Tuesday, February 6, 2007 6:26 PM by Sunit Joshi

Nice..concise and to the point. Helpful for newbies.

# re: Video: Creating an N-Layer ASP.NET Application

Wednesday, February 7, 2007 6:51 AM by violetbase

thnks for you nice post.

it's really usefull.

# re: Video: Creating an N-Layer ASP.NET Application (Updated)

Monday, February 12, 2007 1:10 AM by Lixy

Why the video can't be downloaded?

# re: Video: Creating an N-Layer ASP.NET Application (Updated)

Monday, February 12, 2007 10:32 AM by dwahlin

You can download it.  Once on the video page, right-click on the view in media player link and select Save Target As.  That will let you download it to your machine.

# re: Video: Creating an N-Layer ASP.NET Application (Updated)

Monday, February 19, 2007 6:03 AM by Andreas

Thanks for a great video.

I'm starting off a small project with this methods right away.

A little question though:

When creating my .cs-file from the .xsd the .cs-file wants to be set as a partial class below the .xsd (unless I set the output directory). As far as i can see I have used the same parameters as you do in the video (apart from setting a fully qualified namespace which you pointed out). Is there any special setup or any occasions where this is supposed to happen?

# re: Video: Creating an N-Layer ASP.NET Application (Updated)

Friday, March 16, 2007 2:26 AM by Mazhar

Hi Dan,

This is the best Video I have ever seen in his category. Please post more video in some advance level.

Thanks,

Mazhar

# re: Video: Creating an N-Layer ASP.NET Application (Updated)

Monday, March 26, 2007 9:49 AM by John

Great video. One question. How would you bind the list of customers from the aspx codebehind to a gridview?

# re: Video: Creating an N-Layer ASP.NET Application (Updated)

Tuesday, March 27, 2007 9:38 AM by dwahlin

I would normally just use the ObjectDataSource to bind the list of customers returned from the business layer.  But, you could always write code to assign them to the GridView's DataSource property and then call DataBind().