(转)Rapid DotNetNuke 4 Module Development Using New CodeSmith Templates and VS 2005

Updated 3.17.2007 

I recently made available a set of CodeSmith-compatible templates for DotNetNuke 4.x module development. The templates create the following output files for each selected table, allowing you to quickly and easily create functioning DotNetNuke modules. The generated View[ModuleName].ascx control contains a GridView and FormView that allows you to View, Add, Edit and Delete records right out of the box. The templates also generate a [ModuleName]PartialClasses.vb file for adding custom code - see tutorial here.

 

Here's a quick "How To" for getting the most out of the templates.

Preliminary Setup 

  • Download and install CodeSmith v2.6 (freeware). Michael Washington has produced a great step-by-step tutorial on setting up and configuring CodeSmith v2.6 here.
  • Download the DotNetNuke Starter Kit, then install and configure your development environment using instructions here or here.

Create Table Schema 

  • Create one or more tables to support your module(s). Ensure that each table has a primary key that is an identity field, and a ModuleId field of type Integer.
  • The templates, by default, generate five stored procedures and methods for each table:
    • Get
    • GetAll
    • Add
    • Edit
    • Delete
  • However, you can generate additional "Get[ModuleName]By[ColumnName] stored procs and methods by adding the word "get" to the "Description" extended property of any column. For example, if I add the word "get" to the Description property of the "SecretIdentity" column in the "Superhero" table, the templates will also generate a GetSuperheroBySecretIdentity() method and a supporting SuperheroGetBySecretIdentity stored proc.

Choose Your Template Properties 

  • Open the CodeSmith application and select the DnnMaster.cst file.
  • Multi Source Table - one complete set of module files created for each selected table. You can select multiple tables at a time.
  • Other Database - allows you to execute your modules against a database other than the one that your DNN installation is using. See tutorial, otherwise just leave this blank.
  • Stripped Table Prefixes - Signals the templates to render a table like tbl_Employee into a class named "Employee", instead of "tbl_Employee". Enter a comma-delimited set of table prefixes (for example, tbl_, dnn_, etc)
  • Create Manifest - When set to "True", generates a *.dnn manifest file that allows you to quickly and easily register your modules.
  • IsPortable, IsSearchable - When set to "True", adds stub methods to the Controller.vb class which you can then implement according to your preferences.
  • Keywords - Allows you to select the naming conventions for your stored procedures and class methods.
  • Root Namespace - The templates generate a namespace according to the following convention: [RootNamespace].Modules.[ModuleName].
  • Output Directory - root directory where you would like the generated files output to. The templates will generate sub-directories within the root directory you select.
  • Create Settings Control - When set to "True", generates a Settings control for your View control.
  • Create View Control - When set to "True", generates a View Control complete with a GridView and FormView and code that allows you to View, Add, Edit and Delete records from your module.

Generating and Working with the Code

  • Click the "Generate" button and after a few seconds you should see a list of files that have been generated.
  • Either copy or output these files into the root directory of your DNN development solution.
  • To have the generated files appear in your VS 2005 Solution Explorer window, select the "Refresh" icon.

  • Build your website to verify that everything compiles correctly.

Adding the Module to Your DNN Site

  • You'll need to manually execute the stored procedure scripts through the DNN interface, so log on with the "Host" account and select the "SQL" option from the "Host" menu.

  • Copy the stored procedure creation scripts from the generated "01.00.00.SqlDataProvider" file and paste them into the text box.
  • Select the "Run as Script" checkbox and then select the "Execute" link.

  • To register your module, select the "Module Definitions" option from the "Host" menu.
  • On the Module Definitions page, select the "Create New Module" option

  • On the Edit Module Definitions page, select the generated Manifest (*.dnn) file from the Manifest drop-down, and click the "Install" link.


  • At this point your module is registered with your DNN site and ready to add to a page.
  • Browse to the page you want your module on, then select it from the Module drop-down list. Add an optional title, as well as the Content Pane you'd like it to be placed in, then click the "Add" icon.

 

Working with Your Module

  •  Once your module has been added to the page, you'll be able to add new records to the underlying table by clicking on the "Add..." link. This will make a FormView available to you. Fill out the fields and select the "Insert" link. At this point, the record should now appear in the GridView.

  • Once a record has been added to the GridView, you are able to Edit or Delete it using the appropriate links.

 

The templates currently only generate VB.NET code; however, a C# version is on my "To Do" list, along with a number of other improvements. There is also a forum set up for comments, questions and suggestions for improvements.

Credit Where Credit Is Most Certainly Due: Portions of the template code are based on the DNN 3 templates created by Vicenç Masanas. I've also borrowed some concepts and code from both the .NET Tiers and NHibernate templates for generating output files and stripping table prefixes. Also, the code for the View.ascx file and codebehind is based on a great series of articles by Michael Washington here.

原文地址:http://quiziqal.com/blogs/icode/archive/2007/02/03/118.aspx

posted @ 2007-09-28 16:17  ColorWind  阅读(218)  评论(0)    收藏  举报