Before using CodeSmith Templates for code writing, we should design the data table first. Because CodeSmith Templates generates the corresponding code based on the fields in the data table.
First, the definition of the MyCompany_Article Article table in the "DNN module development series (1) -- Analysis Design" is designed in the SQL Server database. Remember to design the primary and Foreign keys, especially those associated with the Modules table.
Secondly, we can write the stored procedures used by the program first. When writing a stored procedure, we can use the StoredProcedures. cst template in CodeSmith Templates to write the stored procedure. However, the Code it compiled seems to be different from the naming rules I usually use, so I use part of the code it generates, such as the variable definition part. The function of this article module is relatively simple, that is, to add, edit, delete, view an article, and list all the articles in this module. Therefore, we can use five corresponding stored procedures. They are:
Add MyCompany_Article_Add
Edit MyCompany_Article_Update
MyCompany_Article_Delete Delete
MyCompany_Article_Get get an article
MyCompany_Article_GetByModules
You can try to write these stored procedures first. After this series is complete, I will provide all the code. Note that "dbo. ", in terms of stored procedures and table naming, we should also follow the principle of" company name_module name_function name ". Good naming rules not only make the Code look more readable, in addition, when we create only the database installation script in the later stage, we can easily use the find and replace method to quickly generate the database installation script.
Using CodeSmith Templates for programming, we certainly need to use the CodeSmith code to generate a powerful tool. You can http://www.codesmithtools.com/get it here. The process of using CodeSmith Templates is simple. First, I double-click the template to be used. Generally, I start with the Info Class and double-click the Vb BLL Info Class. cst template. First, select MultiSourceTable and select the created article information table MyCompany_Article, for example:
1. Click the MultiSourceTable attribute to bring up the Table Picker selection window. Select the database where the Table is located and select the Table. If no database exists, click Add.
2. Add a database connection
3. Input ObjectQualifer (it seems useless if any input is made) and click Generate to Generate the code.
4. Copy this code to the ArticleInfo. vb file.
Use Vb DataProvider. cst to generate the DataProvider. vb file code. When the program is generated, set IncludeList to false, instead of generating a method to retrieve the list of all articles. Copy the generated code to the corresponding location in DataProvider. vb and replace the prefix "MyCompany _" in the code.
We then use Vb BLL Controller Class. cst and Vb SqlDataProvider. cst to generate the code in ArticleController. vb and SqlDataProvider. vb. The method is the same as the previous one. When generating the SqlDataProvider code, we also need to modify the corresponding stored procedure name, because our naming rules are different from others.
Now we have completed most of the logic layer and data access layer code, and the rest is only the code of the presentation layer. Modify the three controls, Article. ascx, ArticleEdit. ascx, and Settings. ascx, and add some asp.net controls to complete the functions required by the design. Now, just like developing other asp.net programs, there is no other skill or difficulty. When writing, pay attention to the code of other DNN modules to see their structure. The DNN Project Templates also adds a lot of code for us, after filling in the remaining parts, the entire module is complete.
After the basic functions of the program are completed, I will introduce how to implement the ISearchable interface in the next article "DNN module development series (8) -- Implement the ISearchable interface, allows DNN to locate the data in this module and implement the document aggregation function.