Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to perform direct database access in the code-behind of an ASP.NET page?

Tags:

c#

asp.net

I am an experienced developer but I am new to web application development. Now I am in charge of developing a new web application and I could really use some input from experienced web developers out there.

I'd like to understand exactly what experienced web developers do in the code-behind pages. At first I thought it was best to have a rule that all the database access and business logic should be performed in classes external to the code-behind pages. My thought was that only logic necessary for the web form would be performed in the code-behind. I still think that all the business logic should be performed in other classes but I'm beginning to think it would be alright if the code-behind had access to the database to query it directly rather than having to call other classes to receive a dataset or collection back.

Any input would be appreciated.

like image 387
user356687 Avatar asked Sep 02 '25 04:09

user356687


1 Answers

If you go strictly with asp.net (and don't use MVC models), then you are on the right track considering N-tier development and separation.

Your code-behind should be related to presentation/UI, and should rely on middle tier layers for business logic etc.

In general the easiest way to split up the app, is to have multiple projects in a VS solution, such as:

  • ASP.NET Web application project (Presentation/UI)
  • C# class library - Business Logic Layer
  • C# class library - Data Layer
  • Database (SQL or otherwise)

You can of course have many other libraries and layers as needed, but essentially the n-tier approach that works in non-web environments applies well using the same principles here.

like image 101
KP. Avatar answered Sep 04 '25 18:09

KP.