Some years ago, if you wanted to divide your project with layers having a business and a “data access” layer you could create a special folder in your .Net application called “App_Code”. Just doing a right Click on the project and then selecting Add -> .Net Folder -> App_Code.
While that helped dividing layers some people found it less reusable since the layers where part of the project in the end, and then it became a good practice to use libraries instead to a point where the folder App_Code has disappeared from Visual Studio and you shouldn’t use it anymore.
So now, to have your “App_Code” you should use Class Libraries instead. You can create them separately and add them to the project or create them directly inside the project. Let’s see both ways:
Adding a Class Library to the project
For doing this, we first create the Class Library. So, open Visual Studio, New Project > (C# or VB) > Class Library. Give it a name and a folder and press Create. Now we have the Library and we can add classes to it or folders and classes to organize them as we please.
In case you have not created your main project yet, now let’s do something similar to what we did before: open Visual Studio > New Project > (C# or VB) > Web Application (or another). Give it a name and folder and now we have our project ready with probably just the Default.aspx page.
Now let’s add the Library: With your Main Project opened in Visual Studio go to File > Add > “Existing Project…” and select the Library .csproj or .vbproj we created before. Now the Library belongs to this project too despite its files will be saved in a different folder in your hard drive (more reusable).
Creating a Class Library inside the project
In case you want to make a Business Layer just for THIS project that, despite you can reuse, you are not really planning on doing that at the moment, you can just create it directly inside the project.
So go to File > Add > New Project > (C# or VB) > Class Library and give it a name. The Class Library will be created inside your project and will have its own folder (so you can still reuse it) but its folder is inside the Main Project folder making it easier to make backups or move the entire project.
Referencing the Library from the project
The Library is now part of the Assembly and you can move between the Main Project and the Library files in the Solution Explorer. But if you try to access the Library classes you realize you can’t, even being in the same assembly. So, you need to select the project and you can right click its References then > Add Reference > Select “Projects” Tab > Select the Library.
Now that the Library is referenced you can call its classes from your project. Don’t forget to use a namespace!
Adding some references
If you are used to work using classes inside your Web Application, you may miss some library here. In a Web Application, Visual Studio automatically includes some necessary libraries in case you plan on using them, but in a Class Library you will need to add them manually.
For example, imagine you want to access your Webconfig configuration using:
string connStr = ConfigurationManager.ConnectionStrings["MyConn"]; string set = ConfigurationManager.AppSettings["MySetting"].ToString();
Your class will not be able to access webconfig since, by default, class libraries are not supposed to have any webconfig access. So now you have to right click on References (in the class library! not in the web project!) and add the .Net Reference for System.Configuration to have access to the webconfig. I also recommend you to add a reference to System.Web.