Initializing SubSonic in your DNN module

18 Jun

SubSonic requires you to put some stuff in the web.config. You may be wondering how you’re going to distribute your module without requiring the buyer to alter his web.config. Here’s the method that I use:

1
2
3
4
5
6
7
8
9
10
11
12
private static void InitializeProvider()
{
    DataService.Provider = new SqlDataProvider();
    DataService.Providers = new DataProviderCollection();
    DataProvider provider = DataService.Provider;
    var config = new NameValueCollection();
    config.Add("SubSonicSqlString", ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString);
    provider.Initialize("SubSonicSqlString", config);
    DataService.Provider.DefaultConnectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
    DataService.Provider.GeneratedNamespace = "Example.DataModel";
    DataService.Providers.Add(provider);
}

This method goes into my Page_Init event. Luckily I setup my module pages to inherit a base class that does all my setup. Here’s the base class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Web.UI.HtmlControls;
using DotNetNuke.Entities.Modules;
using SubSonic;
 
namespace Example.Common
{
    public class ModuleBase : PortalModuleBase
    {
        #region Events
 
        protected void Page_Init(Object sender, EventArgs e)
        {
            InitializeProvider();
        }
 
        #endregion
 
        #region Methods
 
        private static void InitializeProvider()
        {
            DataService.Provider = new SqlDataProvider();
            DataService.Providers = new DataProviderCollection();
            DataProvider provider = DataService.Provider;
            var config = new NameValueCollection();
            config.Add("SubSonicSqlString", ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString);
            provider.Initialize("SubSonicSqlString", config);
            DataService.Provider.DefaultConnectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
            DataService.Provider.GeneratedNamespace = "Example.DataModel";
            DataService.Providers.Add(provider);
        }
 
        protected void RegisterJavascript(string fullPath)
        {
            var script = new HtmlGenericControl("script");
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", fullPath);
 
            Page.Header.Controls.Add(script);
        }
 
        #endregion
    }
}

In my last post about including jQuery properly I introduced the RegisterJavascript method. We can put the method in this class so we can use it in all pages inheriting the class.

One thing worth mentioning is that the app.config for SubSonic must be setup before you can expect this to work. You should also have a SubSonic data model built and referenced. You can learn more about that here: http://michaelpardo.com/2009/06/setting-up-subsonic-in-you-dnn-module-solution/

One Response to “Initializing SubSonic in your DNN module”

  1. Jenny 23. Jun, 2009 at 9:09 pm #

    Pretty cool post. I just found your blog and wanted to say
    that I’ve really liked browsing your posts. Any way
    I’ll be subscribing to your blog and I hope you write again soon!

Leave a Reply