DotNetNuke Modules – Data Access Layer
On Tuesday, we started working on the code for our Data Access Layer by having CodeSmith generate some classes for us. Today, we are going to generate the DataSets we need and modify the controller classes to use them. At the end, you will have a DataLayer and Business Logic Layer that will allow you to start coding your presentation layer using the familiar ASP.NET 2.0 model.
So the next step in our creation of the Data Access Layer (DAL) is to create our DataSet(s). This should be rather trivial. If you only have a few tables that you will be using, you can place all of your DataTables in one DataSet. But for large projects, I strongly recommend breaking things up so that you have as few tables in a DataSet as possible. It just makes things easier to maintain. I find that one DataTable per DataSet is the most practical. But if you need the entity relationships between tables, you might need to put more than one DataTable in a DataSet.
So add your DataSet into the module directory under app_code. If you have not already established a connection to your DotNetNuke database in server explorer, do that now. Next, select the table or tables you want in your DataSet (you can multi-select the tables from the server explorer) and drag and drop them into the DataSet design surface. By default, this will generate the DataTable and the associated TableAdapter. Since we will not be using the TableAdapter, you can delete it by clicking on the TableAdapter name and pressing the Delete key. Make sure you only delete the TableAdapter and not the DataTable.
Now we are almost done. All we need to do is to fix up the controllers that we generated at the beginning of the week.
You will notice that your controllers have several methods in them. Everything that returns an ArrayList we are going to modify to return the DataTable. Everything that returns an Info object we are going to change to return the typed DataRow. Similarly, anything that takes an Info object as a parameter will be replaced to accept the DataRow.
There are two advantages to going through all this trouble. First, the DataTable is much more strongly typed than the ArrayList. So we are much less likely to make a mistake casting the object in the ArrayList to something it is not. Second, many of the cool new databinding features that showed up in ASP.NET 2.0 ONLY work with DataSets. Automatic sorting of GridView columns is one such feature. So if you want your application to take advantage of these you’ll want to make these changes.
So using the HtmlText table as an example, the CodeSmith wizard would have created a Controller class that looks like:
public HtmlTextInfo Get(int moduleID) { return (HtmlTextInfo)DotNetNuke.Common.Utilities.CBO.FillObject( DataProvider.Instance().GetHtmlText(moduleID), typeof(HtmlTextInfo)); } public ArrayList List() { return DotNetNuke.Common.Utilities.CBO.FillCollection( DataProvider.Instance().ListHtmlText(), typeof(HtmlTextInfo)); } public ArrayList GetByModules(int moduleID) { return DotNetNuke.Common.Utilities.CBO.FillCollection( DataProvider.Instance().GetHtmlTextByModules(moduleID), typeof(HtmlTextInfo)); } public int Add(HtmlTextInfo objHtmlText) { return (int)DataProvider.Instance().AddHtmlText( objHtmlText.DesktopHtml, objHtmlText.DesktopSummary, objHtmlText.CreatedByUser, objHtmlText.CreatedDate); } public void Update(HtmlTextInfo objHtmlText) { DataProvider.Instance().UpdateHtmlText( objHtmlText.ModuleID, objHtmlText.DesktopHtml, objHtmlText.DesktopSummary, objHtmlText.CreatedByUser, objHtmlText.CreatedDate); } public void Delete(int moduleID) { DataProvider.Instance().DeleteHtmlText(moduleID); }
We want to change these to look like:
public DataSetDMBSample.HtmlTextRow Get(int moduleID) { DataSetDMBSample.HtmlTextDataTable table = new DataSetDMBSample.HtmlTextDataTable(); table.Load(DataProvider.Instance().GetHtmlText(moduleID)); if (table.Count > 0) return table[0]; return null; } public DataSetDMBSample.HtmlTextDataTable List() { DataSetDMBSample.HtmlTextDataTable table = new DataSetDMBSample.HtmlTextDataTable(); table.Load(DataProvider.Instance().ListHtmlText()); return table; } public DataSetDMBSample.HtmlTextDataTable GetByModules(int moduleID) { DataSetDMBSample.HtmlTextDataTable table = new DataSetDMBSample.HtmlTextDataTable(); table.Load(DataProvider.Instance().GetHtmlTextByModules(moduleID)); return table; } public int Add(DataSetDMBSample.HtmlTextRow objHtmlText) { return (int)DataProvider.Instance().AddHtmlText( objHtmlText.DesktopHtml, objHtmlText.DesktopSummary, objHtmlText.CreatedByUser, objHtmlText.CreatedDate); } public void Update(DataSetDMBSample.HtmlTextRow objHtmlText) { DataProvider.Instance().UpdateHtmlText( objHtmlText.ModuleID, objHtmlText.DesktopHtml, objHtmlText.DesktopSummary, objHtmlText.CreatedByUser, objHtmlText.CreatedDate); } public void Delete(int moduleID) { DataProvider.Instance().DeleteHtmlText(moduleID); }
Here’s what we’ve done:
- Replace all ArrayList with DataSetDMBSample.HtmlTextDataTable
- Replace all HtmlTextInfo with DataSetDMBSample.HtmlTextRow
- Create a table object and load it with the IDataReader that is returned by the DataProvider.Instance().Get… call for things that return Tables or a Row
- For our Get method (the only thing in this example that returns a row in this example) check to make sure we have a row to return and return it, otherwise, return null.
Here’s what hasn’t changed:
- The Delete() method stays the same.
- The Add and Update methods only have the parameter type changed. Nothing else changes in these two functions.
- The DataProvider.Instance().Get… functions are the same as in the original code.
I’m sure some really smart person could open up the CST files that CodeSmith uses and change them to generate the above code directly. But since this is something I only use once a year (maybe), it hasn’t been worth the effort to me to figure that part out. It only takes about 3 minutes to make these changes once you get the hang of it.
Other post in DotNetNuke - Module Development
- Creating DotNetNuke Modules - May 20th, 2008
- Creating DNN Modules - The Tools - May 22nd, 2008
- DotNetNuke Modules - Foundational Concepts - May 26th, 2008
- DotNetNuke Modules - Install DNN into VS 2008 - May 27th, 2008
- DotNetNuke Modules - Creating Base Modules - May 28th, 2008
- DotNetNuke Modules - Registering Your Module - May 29th, 2008
- DotNetNuke Modules - Where Stuff Shows Up - June 3rd, 2008
- DotNetNuke Modules - Benefits of Architecture - June 4th, 2008
- DotNetNuke Modules - Anatomy of the View - June 9th, 2008
- DotNetNuke Modules - Adding Actions - June 11th, 2008
- DotNetNuke Modules - DNN Controls - Label - June 18th, 2008
- DotNetNuke - Internationalization - June 25th, 2008
- DotNetNuke Modules - Internationalization (part 2) - June 30th, 2008
- DotNetNuke Modules - Labels w/ no Help - July 9th, 2008
- DotNetNuke Modules - LinkButtons - July 14th, 2008
- DotNetNuke Modules - Collapsible Panels - July 16th, 2008
- DotNetNuke - The Data Layer - Installing CodeSmith - July 22nd, 2008
- DotNetNuke - Modules - Creating The Tables - July 24th, 2008
- DotNetNuke - Modules - Creating Stored Procs - July 29th, 2008
- DotNetNuke - Modules - Portal Specific Modules - July 31st, 2008
- DotNetNuke Modules - Data Access Layer - August 5th, 2008
- DotNetNuke Modules - Data Access Layer - August 7th, 2008
- DotNetNuke - Data Access Layer Alternative - August 12th, 2008
- DotNetNuke - Modules - Linking within the module - August 14th, 2008
- DotNetNuke - Make Your Module Searchable - August 19th, 2008
- DotNetNuke Modules - Making Content Portable - August 25th, 2008
- DotNetNuke Modules - Exceptions the DNN Way - September 2nd, 2008
- DotNetNuke Modules - PortalModuleBase - September 4th, 2008
- DotNetNuke Modules - Inter Module Communication - September 9th, 2008
- DotNetNuke Modules - Finding The Page a Module is On - September 15th, 2008
- DotNetNuke Modules - Caching - September 17th, 2008
- DotNetNuke Modules - Module Settings - September 22nd, 2008
- DotNetNuke Modules - Retrieving Settings - September 24th, 2008
- DotNetNuke Modules - Advanced Architecture - October 20th, 2008
- DotNetNuke Modules - Creating the PA - October 30th, 2008
- DotNetNuke Modules - Automating the PA - November 5th, 2008
- DotNetNuke - FileUploadControl Danger! - February 26th, 2009
- DotNetNuke SecurityException AspnetHostingPermission - October 14th, 2009
Other Related Items:
Beginner's ASP.NET in VB.NET 2003 on DVDASP.NET (VB.NET) 2003 represents an important technology for building enterprise level web applications. Learn the basics of ASP.NET development as yo... Read More >
Beginning ASP.NET 3.5: In C# and VB (Programmer to Programmer)This book is for anyone who wants to learn how to build rich and interactive web sites that run on the Microsoft platform. With the knowledge you gain... Read More >











[...] DotNetNuke: Data Access Layer [...]