Teaching by ExampleSome concepts are easy to understand simply by listening to a lecture or reading a book. Others require examples in order to fully comprehend them. Enterprise templates are a perfect example of the latter. Because much of an enterprise template is shell code, it is sometimes hard to explain. In the following sections, you will learn how to create a new enterprise template. The entire process can be broken up into several distinct parts. Setting Up the PrerequisitesThe first step in developing anything is to set up your environment. The same holds true with the development of an enterprise template. Before you start this example, make sure that you set up your system to display hidden files, folders, and file extensions in the Windows Explorer. Also, because this template will include a web service, ensure that IIS has been installed on your machine. Laying Out the TemplateThe template that you will be developing is a standard n-tier application. As shown in Figure 41.1, it consists of a user interface layer (UIL), a business logic layer (BLL), and a data access layer (DAL). Figure 41.1. The enterprise template architecture is divided into three distinct layers.In this template, the user interface layer will be a Windows Forms application. This layer enables you to display all the necessary information to the user. In addition, it makes it possible for you to collect information from the user. The next layer is the business logic layer. This layer is a set of web services that contain the logic necessary to perform the necessary operations of the system. The final layer is the data access layer. This layer contains the functionality needed to store and retrieve the database entities used in the application. Although these layers would normally contain the functionality needed to perform the functions of the system, this is only a template and therefore contains only a thin shell of functionality. Naming the Parts of the TemplateWhen laying out the template, it is useful to write down the names of each section. Later in the project, it will be necessary to refer to the correct names of each part of the template. If you choose not to write down the names, you run the risk of generating errors by using inconsistent names. For the purposes of this demonstration, the name of this template will be nTierTemplate.etp. The three layers will use the name of the layer followed by .etp. For example, the data access layer will be dal.etp, the user interface layer will be uil.etp, and the business logic layer will be bll.etp. Creating the Template StructureThe first thing to do in creating the template structure is to create the enterprise template. The following steps will create an empty enterprise template:
With this done, you will need to create the subproject templates. To do this, follow these steps:
Figure 41.3 shows the completed template structure to this point. Figure 41.3. The empty structure of the nTierTemplate enterprise template.![]() Adding the Language ProjectsUp to this point, the work you have done is similar to creating four separate solutions. The only difference is that these solutions are contained within a solution and are known as an enterprise template (although similar to a solution, enterprise template projects have additional features). The next step is to actually add some substance to the template by adding what are known as language projects to the template. The following steps will create the DAL project:
After you've created the DAL project, you can create the UIL project template, as shown in the following steps:
Finally, you will want to create the BLL project. To do so, follow these steps:
The creation of the template structure is now complete and should resemble Figure 41.7. This structure will serve as the basis for all projects created with this template. As such, you can specify any additional settings that you want to be included with all of those projects. The settings could include compiler settings, such as warning levels, and other project defaults. You can also choose to add controls or other projects to these templates. Figure 41.7. The completed nTierTemplate structure.Assigning a Policy to the TemplateBefore you can use this structure as a template, you must assign a policy. Although you could create a policy from scratch, doing so is beyond the scope of this section. For the purposes of this demonstration, you will create a policy by cloning the default DAP.tdl file and modifying it as necessary. Follow these steps to configure the policy:
Making the TemplateThe previous sections created the basic structure from which to create the enterprise template. Unfortunately, those sections were the last of the automated processes (wizard based). This section will show you the manual steps that you must perform to finish the template and have it recognized by Visual Studio .NET. Locate, Organize, and Clean UpThe following steps walk you through finalizing the process of creating and using an enterprise template. Before starting the following steps, save the projects, close the solution, and exit Visual Studio .NET.
Throwing Out the TrashApplications such as those created by Visual Studio .NET require a few ancillary files and directories such as the solution file (.sln), .suo files, .eto files, and bin and obj directories. Although they serve a purpose in a regular application, they are not needed in an enterprise template and can therefore be deleted. Modifying the Enterprise Template Project FilesThis next section will show you how to make some minor modifications to all the associated enterprise template files (.etp).
To remove the Project ID from the Enterprise Template Files, start by deleting the line
<GUIDPROJECTID>A GUID Will Be Here</GUIDPROJECTID>
from the following files: nTierTemplate.etp, BLL\bll.etp, DAL\dal.etp, and UIL\uil.etp. The next thing you'll do in this sample is to modify the Web Project template to prompt for a URL. Listing 41.2 shows the contents of the business logic layer enterprise template project file. In order for the web project to prompt for the URL, Listing 41.2. The bll.etp File Before Modifications<?xml version="1.0"?> <EFPROJECT> <GENERAL> <BANNER>Microsoft Visual Studio Application Template File</BANNER> <VERSION>1.00</VERSION> <Views> <ProjectExplorer> <File>http://localhost/BLLWebService/BLLWebService.csproj</File> </ProjectExplorer> </Views> <References> <Reference> <FILE>http://localhost/BLLWebService/BLLWebService.csproj</FILE> </Reference> </References> </GENERAL> </EFPROJECT> Listing 41.3. The bll.etp File After Modifications<EFPROJECT> <GENERAL> <BANNER>Microsoft Visual Studio Application Template File</BANNER> <VERSION>1.00</VERSION> <Views> <ProjectExplorer> <File>BLLWebService\BLLWebService.csproj</File> </ProjectExplorer> </Views> <References> <Reference> <FILE>BLLWebService\BLLWebService.csproj</FILE> <REQUIRESURL>1</REQUIRESURL> The next step is to add global entries to the Enterprise Template Project (.etp) and user properties to the language projects (.csproj) to allow the policy file (.tdl) to identify your project files as elements. Add the following entry to the <GLOBALS> section in all four enterprise template projects:
<GLOBALENTRY>
<NAME>TDLELEMENTTYPE</NAME>
<VALUE>Insert Name Here</VALUE>
</GLOBALENTRY>
Make sure to replace Insert Name Here with the name of the template project. For example, after modifying the nTierTemplate.etp file, it should look like the following: <GLOBALENTRY> <NAME>TDLELEMENTTYPE</NAME> <VALUE>nTierTemplate</VALUE> </GLOBALENTRY> Add the following entry after the <Files> section in all three of the language projects:
<UserProperties
TDLFILE = "nTierTemplate.tdl"
TDLELEMENTTYPE = "Insert Name Here"
/>
Make sure to replace the Insert Name Here to the name of the language project. For example, after modifying the DALLibrary.csproj, it should look like the following:
<UserProperties
TDLFILE = "nTierTemplate.tdl"
TDLELEMENTTYPE = "DALLibrary"
/>
Making the Template Available to UsersNow that you have a completed template and are ready to use it, how do you make sure that it is easily accessible to all users of the system? After all, if you spend all the time and effort necessary to plan and create a usable enterprise template, the last thing you want to hear is that "I didn't use it because I couldn't find it." Creating a .vsdir FileTo identify the appropriate files to display in the Add New Project dialog, Visual Studio .NET uses a .vsdir file. In this file, each line represents a different project or template. Within each line is a set of fields delimited by pipes (|), as described in Table 41.3. To correctly expose your new template to users, you will need to create a .vsdir file that contains the appropriate information for your file and store it in the EnterpriseFrameworks\ProxyProjects directory. For the purposes of this discussion, create a new text file in Notepad, and copy and paste the following line into this file (space restrictions do not allow the code to be printed on a single line, but you should paste it into Notepad as one line): ..\Projects\nTierTemplate\nTierTemplate.etp|{AE77B8D0-6BDC-11d2-B354-0000F81F0C06}| Visual C# n-Tier Project|1|This is the sample enterprise template created in chapter 41 of the Visual C#.NET Unleashed Book.|{AE77B8D0-6BDC-11d2-B354-0000F81F0C06}| 125|0|nTierProject To complete the exposure of the template, save this file into the EnterpriseFrameworks\ProxyProjects directory. Testing the TemplateThe final step in this example is to test the newly created template.
As shown in Figure 41.11, when these steps have been completed, you should have an instance of the application that your template was designed to create. Figure 41.11. The solution created by the enterprise template. |