Click here to Skip to main content
Click here to Skip to main content

Use SmartCode Create DnnModuleDataProvider Code

, 20 Jan 2008
Rate this:
Please Sign up or sign in to vote.
SmartCode Template to create dotnetnuke modules BaseCode

Introduction

DotNetNuke (DNN), a beautiful web portal application,It do very good job of sloving the problerm of base web application pluming. Module Development is a primary and important work if you want to create a individuation website DNNModule.Template is a template that is based upon SmartCode.You can create the dotnetnuke module code quickly,it include DataProvide,SqlDataprovider,ModuleInfo and controller.

Background

I had learned DNN two years ago,Developte some Modules.When I find SmartCode from Codeproject,I decide to build a SmartCode Template to use to create dotnetnuke module basecode quickly.

Using the code

the following code just for create "SqlDataProvider":

           using System;
using System.Collections.Generic;
using System.Text;
using SmartCode.Template;
using SmartCode.Model;
namespace DNNModule.Template
{
     public  class SqlDataProvider:TemplateBase 
    {
         public SqlDataProvider()
        {
            this.createFile = true;
            this.description = "";
            this.name = "Providers.DataProviders.SqlDataProvider";
            this.outputFolder = @"Components/Providers/DataProviders/SqlDataProvider";
        }
        public override string OutputFileName()
        {
            return Table.Name + "SqlDataProvider.cs";
        }

        public override void ProduceCode()
        {
            WriteLine("using System; ");
            WriteLine("using DotNetNuke;");
            WriteLine("using System.Data;");
            WriteLine("using DotNetNuke.Framework;");
            WriteLine("using System.Data.SqlClient;");
            WriteLine("using Microsoft.ApplicationBlocks.Data;");
            WriteLine("using DotNetNuke.Common.Utilities;");
            WriteLine("using DotNetNuke.Framework.Providers;");

            WriteLine("namespace YourCompany.Modules.ModuleName.Data");
            WriteLine("{");
            WriteLine("     public class SqlDataProvider :DataProvider");
            WriteLine("     {");

            WriteLine("     ");
            WriteLine("         #region Private Members");
            WriteLine("     ");
            WriteLine("         private const string ProviderType = \"data\";");
            WriteLine("         private const string ModuleQualifier = \"_\";");
            WriteLine("     ");
            WriteLine("         private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType);");
            WriteLine("         private string _connectionString;");
            WriteLine("         private string _providerPath;");
            WriteLine("         private string _objectQualifier;");
            WriteLine("         private string _databaseOwner;");
            WriteLine("     ");
            WriteLine("         #endregion");
            WriteLine("     ");
            WriteLine("         #region Constructors");
            WriteLine("     ");
            WriteLine("         /// <summary>");
            WriteLine("         /// Constructs new SqlDataProvider instance");
            WriteLine("         /// </summary>");
            WriteLine("         public SqlDataProvider()");
            WriteLine("         {");
            WriteLine("             //Read the configuration specific information for this provider");
            WriteLine("             Provider objProvider = (Provider)_providerConfiguration.Providers[_providerConfiguration.DefaultProvider];");
            WriteLine("     ");
            WriteLine("             //Read the attributes for this provider");
            WriteLine("             //Get Connection string from web.config");
            WriteLine("             _connectionString = Config.GetConnectionString();");
            WriteLine("     ");
            WriteLine("             if (_connectionString.Length == 0)");
            WriteLine("             {");
            WriteLine("                 //Use connection string specified in provider");
            WriteLine("                 _connectionString = objProvider.Attributes[\"connectionString\"];");
            WriteLine("             }");
            WriteLine("     ");
            WriteLine("             _providerPath = objProvider.Attributes[\"providerPath\"];");
            WriteLine("     ");
            WriteLine("             _objectQualifier = objProvider.Attributes[\"objectQualifier\"];");
            WriteLine("     ");
            WriteLine("             if ((_objectQualifier != \"\") && (_objectQualifier.EndsWith(\"_\") == false))");
            WriteLine("             {");
            WriteLine("                 _objectQualifier += \"_\";");
            WriteLine("             }");
            WriteLine("     ");
            WriteLine("             _databaseOwner = objProvider.Attributes[\"databaseOwner\"];");
            WriteLine("             if ((_databaseOwner != \"\") && (_databaseOwner.EndsWith(\".\") == false))");
            WriteLine("             {");
            WriteLine("                 _databaseOwner += \".\";");
            WriteLine("             }");
            WriteLine("     ");
            WriteLine("         }");
            WriteLine("         #endregion");
            WriteLine("     ");
            WriteLine("         #region Properties");
            WriteLine("     ");
            WriteLine("         public string ConnectionString");
            WriteLine("         {");
            WriteLine("             get { return _connectionString; }");
            WriteLine("         }");

            WriteLine("     ");
            WriteLine("         public string ProviderPath");
            WriteLine("         {");
            WriteLine("             get { return _providerPath; }");
            WriteLine("         }");
            WriteLine("         "); 

            WriteLine("         public string ObjectQualifier");
            WriteLine("         {");
            WriteLine("             get { return _objectQualifier; }");
            WriteLine("         }");
            WriteLine("     ");

            WriteLine("         public string DatabaseOwner");
            WriteLine("         {");
            WriteLine("             get { return _databaseOwner; }");
            WriteLine("         }");
            WriteLine("         ");
            WriteLine("         #endregion");
            WriteLine("         ");
            WriteLine("         #region Private Methods");
            WriteLine("         ");
            WriteLine("         private string GetFullyQualifiedName(string name)");
            WriteLine("         {");
            WriteLine("             return DatabaseOwner + ObjectQualifier + ModuleQualifier + name;");
            WriteLine("         }");
            WriteLine("         private Object GetNull(Object Field)");
            WriteLine("         {");
            WriteLine("            return Null.GetNull(Field, DBNull.Value);");
            WriteLine("         }");
            WriteLine("     ");
            WriteLine("         #endregion");
            WriteLine("     ");
            

            #region Public Methods
            string AddPamas = "";
            string UpdatePamas = "";
            string keys = "";

            string AddPamas2 = "";
            string UpdatePamas2 = "";
            string keys2 = "";
            foreach (Column column in Table.AllColumns())
            {
                if (!column.IsIdentity)
                {
                    AddPamas += Common.GetFieldTypeAsTSQLType(column) + " " + column.Name + ",";
                    AddPamas2 += column.Name + ",";
                }
                if (column.IsPrimaryKey)
                {
                    keys += Common.GetFieldTypeAsTSQLType(column) + " " + column.Name + ",";
                    keys2 += column.Name + ",";
                }
                UpdatePamas += Common.GetFieldTypeAsTSQLType(column) + " " + column.Name + ",";
                UpdatePamas2 += column.Name + ",";

            }
            AddPamas = AddPamas.Substring(0, AddPamas.Length - 1);
            keys = keys.Substring(0, keys.Length - 1);
            UpdatePamas = UpdatePamas.Substring(0, UpdatePamas.Length - 1);
            WriteLine("       ");
            WriteLine("         #region override methods");
            WriteLine(" ");
            WriteLine("         public override void Add" + Table.Name + "(" + AddPamas + ")");
            WriteLine("         {");
            WriteLine("             SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(Add" + Table.Name + ")," + AddPamas2+ ");");
            WriteLine("         }");
            WriteLine(" ");
            WriteLine("         public override void Update" + Table.Name + "(" + UpdatePamas + ")");
            WriteLine("         {");
            WriteLine("             SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(Update" + Table.Name + ")," + UpdatePamas2 + ");");
            WriteLine("         }");
            WriteLine(" ");
            WriteLine("         public override void Delete" + Table.Name + "(" + keys + ")");
            WriteLine("         {");
            WriteLine("             SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(Delete" + Table.Name + ")," + keys2 + ");");
            WriteLine("         }");
            WriteLine(" ");
            WriteLine("         public override IDataReader Get" + Table.Name + "(" + keys + ")");
            WriteLine("         {");
            WriteLine("             return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(Get" + Table.Name + "), " + keys + ");");
            WriteLine("         }");
            WriteLine(" ");
            WriteLine("         public override IDataReader Get" + Table.Name + "s(string strWhere)");
            WriteLine("         {");
            WriteLine("             return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(Get" + Table.Name + "s), strWhere);");
            WriteLine("         }");
            WriteLine(" ");
            WriteLine("         #endregion");
            #endregion
            WriteLine("     }");
            WriteLine("}");
        }
    }
}

        

Others please download project code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

yesdnn.com
Web Developer http://www.yesdnn.com
China China
Dear friends,I love you.

Comments and Discussions

 
GeneralMVC PinmemberDesignGeek4U19-May-10 8:44 
GeneralThanks PinmemberMaulikkk9-Dec-09 21:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 20 Jan 2008
Article Copyright 2008 by yesdnn.com
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid