How to improve Project structure in c# web application

1.3k views Asked by At

I am working on C# web application and i build various application with simple project structure like image is given below.

Solution Explorer

.cs page

.aspx page using master page

Basically i want to know about the best project structure of large web application on C#? or is above project structure(in the image) is right way to build an web application ?

Now i want to improve my project structure. So where should i start improve myself to build an lage web application.

2

There are 2 answers

0
CoderGirl42 On

I don't think there is really a correct way to structure a project. It depends on what the goals of the project our and your plans for implementation. Each project is different and have will different requirements.

I would go with the most logical approach for your particular project. Whether that's grouping similar components together or grouping whole sub-systems together.

You should carefully analyze your requirements up front before you even begin development and plan your structure accordingly. Having a good plan before you begin coding will save you a bunch of hassle down-stream if things aren't working out and you need to restructure.

I would suggest reading a book like Code Complete, it'll give you excellent tips on how to plan and structure your projects. You can find it here: Code Complete

2
John Saunders On

There's no "best way", but there are some "worst ways". I see that you are using some of the "worst ways". Here are a few of them:

  1. Do not use web site "projects". They are not projects. They're just whatever happens to be in a particular directory tree. I notice that you have ~1.pdf in your project, or did you just happen to have it in the same folder? Use Web Application Projects instead - use File->New Project.

  2. Do not use App_Code. App_Code is for a small number of code files which are compiled at runtime. No other kind of "project" has code compiled at runtime. This is a bizarre corruption of computer programming.

    At the very least, create separate folders for the different parts of your code which are not in code-behind. You can start with a single folder (maybe called "My_Code", not App_Code), and when you start to get "too many" files in that one folder, start separating the files based on their function. Keep one class per file, and it will become obvious quickly that different sets of classes have different sets of functions. For instance, you'll find you have a set of classes related to database access. Put those into a "DataAccess" folder.

    Better, once you find that you have many such folders, move each of the folders out into their own class library project. This way, you don't have to jump right into a complex project structure - the structure can evolve over time.

    And if you wind up in ten years with only three classes in My_Code, then you have saved yourself the waste of creating tiny class libraries.

  3. You may have too much code in your code behind. I can't see it, but projects which have the first two symptoms usually have the third. Your codebehind should only have code in it that directly involves user interface - for instance, it can call a method to fetch data to bind to a data bound control, but the code to fetch the data should be in a different class, not in the codebehind. This different class should not be in App_Code, as I say in #2.

  4. Are you using source control? I don't see any source control icons on your project icons. Use source control. Visual Studio Online is free for up to five users, and allows you to use either TFS or Git for source control, as well as providing work item tracking, automated builds and quite a bit more. For free.