Creating Separate DLLs for App_Code Folders

Starting with ASP.NET 2.0, Web Site projects were introduced and allowed for the compilation of the code-behind file for each page and usercontrol into single page assemblies/DLLs (see Figure 1 below). This option works well for the individual pages and usercontrols, however, by default all classes in the App_Code directory are compiled into a single DLL, even if App_Code contains separate sub-directories.

Figure 1. Publish Web Site Options

This normally is not an issue, however, with a recent Web Site that only contain three pages (Medical, Dental and Life), we did not want the classes (the logical tiers) in the App_Code directory combined into one DLL because each page was independent and we could be delivering changes for one page while working on enhancements for the others…we needed separate DLLs for the tiers as well as the pages and usercontrols.  The simplest solution would have been to deploy the source code files, but that was not an option.  Another solution was to create separate Business, Model (custom business objects) and Data projects for each page, but that seemed like overkill.

ASP.NET does support the use of classes in multiple languages within the same project, and those are compiled into separate DLLs by creating codeSubDirectories under App_Code, but you must also specify that you are using code sub-directories in the Web.Config file, <compilation> element (see Web.Config snippet below).

In our case though, we are using the same language (C#), but the concept is the same. We created the sub-directories, updated the Web.config file compilation section to include the appropriate codeSubDirectories, re-compiled the site and a separate DLL was generated for each sub-directory (see Bin Folder contents below).

Note: According to the above link “The build order is inferred from the top-down order of the codeSubDirectories collection. The App_Code directory is built last. However, there are no build order dependencies, therefore that the build order is not significant.”  In practice, if you place the Business folder before the Data folder in the Web.Config file, classes in the Business folder won’t be able to reference classes in the Data folder.  It seems the build order does matter.

Sample Project Solution

Web.Config

<compilation debug=false>

<!–Code SubDirectories added in order to have separate DLLs
for each product/page to simplify future maintenance and deployment
–>
<
codeSubDirectories>

<add directoryName=Common/>
<add directoryName=Model/>
<add directoryName=MedicalData/>
<add directoryName=MedicalBusiness/>
<add directoryName=DentalData/>
<add directoryName=DentalBusiness/>
<add directoryName=LifeData/>
<add directoryName=LifeBusiness/>
</codeSubDirectories>

</compilation>

Bin Folder

App_SubCode_Common.dll
App_SubCode_DentalBusiness.dll
App_SubCode_DentalData.dll
App_SubCode_LifeBusiness.dll
App_SubCode_LifeData.dll
App_SubCode_MedicalBusiness.dll
App_SubCode_MedicalData.dll
App_SubCode_Model.dll
App_Web_dental.aspx.f9b0821e.dll
App_Web_life.aspx.f9b0821e.dll
App_Web_medical.aspx.f9b0821e.dll

  • Delicious
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter

3 Responses to “Creating Separate DLLs for App_Code Folders”

  1. MichaelG says:

    How would it be different if classes were created in multiple languages?

  2. mrrodd says:

    No, the process would be the same. However, you do need to keep the files for each language in separate folders so that the right .NET compiler is used.

  3. stroment says:

    really helpful post .. thanks for sharing.

Leave a Reply