![]() |
General Programming »
Programming Tips »
General
License: The Code Project Open License (CPOL)
Subsonic, MySQL and DAL DLLBy Dane-Garrin BaliaCreating a DAL DLL using Subsonic and Mysql |
C# (C# 1.0, C# 2.0, C# 3.0)
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
In this article I attempt to tackle some of the peculiarities in using SubSonic to generate a DAL into a DLL, while using MySql as our database. Why you might ask? Simply as most code samples on the WWW cover SQL Server as well as failing to show the problems that lie in using SubSonic itself.
Subsonic is a toolset that helps a website build itself. At its core it's:
With that being said, Subsonic is a tightly-coupled DAL builder that allows for an almost SQL like interaction into data classes that are a representation of your Database. But is this not what LINQ provides say someone……there is no stable provider for MySql as yet.
Moving on….The Beta works – 2.1 Beta 2 (March 13 2008)
I’ve tried and tried but was unable to get the Current Stable Version of Subsonic working as I kept getting the following error:
"System.InvalidOperationException: Connection must be valid and open"
This from my investigations seemed to involve updates to the MySql.Data.dll and SubCommander utility found within Subsonic. My point, use the Beta, it works!
Download, Install
You can download and install beta from the Official Website, here is the link anyways.
Start a New Project

Create your new project, and choose Class Library, name is appropriately.
Add DLLs
Next add the reference for Subsonic – they will be found in your Program Files directory
Only the following will be required:
Great start, now we just need to setup our Configuration File to build our class libraries or DAL. But before we do that, let’s make Subsonic (Commander) part of an external tool into Visual Studio 2008 for ease of use.
Configuring an External Tool
Clicking on this Button would automatically initiate the Build/generation process for your DLL, but first we need a Configuration File.
We’re not there yet, but once you have generated the DAL successfully, it will generate a folder in your Project which won’t be visible. To bring this folder into your Project for inclusion, click on the “Project” menu and select “Show All Files”.
You will then notice the “Generated” folder (provided that was your desired output folder), right-click and then say “Include in Project”, and it’s done.

Basic Configuration File
Configuration files are more or less self-explanatory and as such there is no need to go into too much detail around the configuration itself. Configuration File for Subsonic requires three parts:
Configuration Section Service Definition

Configuration for Connection Strings
This section is merely the definition of your databases from which you would like to generate the DAL. The provider name is important for Subsonic, to ensure the correct library is used to gain access to the MySQL Databases. In the above situation, I’ve excluded the “Password”, you might have need for it.
Configuration Settings for Providers relative to Connection Strings

This is the actual definition of the Subsonic Service and its Providers, which point back to the connection strings specified above. The entire configuration as a whole points to a Multiple Database Configuration option, I only make mention of this as I struggled for some time just to be able to get the configuration correct.
Tip. Now what you might notice different from the Subsonic Documentation in the “Provider Section” above (c), is that the “type” has been renamed from "ActionPak" to "SubSonic". Why? It was previously named ActionPak and now the Namespace has been changed.
Compilation Problems (namespace, multiple DAL)
Tip. Points worth noting, is that Subsonic will not generate the DAL for you unless all your tables in your Database contain “PRIMARY KEYS”. This is a design decision as far as I know, and revolves around some standard of integrity as opposed to none.
Tip. In you have intentions of using different databases then just that of a singleton, from which you used to generate the DAL itself; it’s important to know that you need to at least fetch the schema from that built-against Database, so that you can manipulate the Subsonic API. Huh? I’ll explain further in the Multiple Provider section, just keep this in your head.
Tip. Sometimes you might have compile issues once you have included your Generated class libraries, and the two that I encountered revolved conflicts with:
a) A generated class from a table called System, which conflicted with the .NET System namespace. Thus you can exclude that Table from the Project, Delete it, or rename you database table.
b) This one is relative to .NET, it does not allow variables to have names that begin with numbers, and in this case, you might have fields in a database that are called “1stEscalation” or “2ndEscalation” for example. The fix behind this, is to just insert a character or letter before these variables using the “Replace” functionality found under the “Find” popup menu in Visual Studio. In my case, everything ‘1st” I renamed to “_1st”.
Using Multiple Providers
In being able to use multiple providers, one needs to be able to fetch the schema for the table or object one is going to use, and then to actually make the call for the data. To access the multiple data providers specified in our configuration (i.e. Helpdesk and Super), check out the code:
Instead of accessing the Table Objects directly, one would need to use the Query class for building and accessing data. In the above situation, the first argument is our Table Name within our Database; and the second, Provider name defined in the configuration file above (App.config).
Now the second line makes the query possible, as Subsonic requires the table schema before it can manipulate and fetch the data from the database.
Building Dynamic Providers with Connection Strings from a MySql Database
Now if you have an environment that has multiple databases, then maintaining an App.config with all the connection strings is by no means practical. So as we developers often do, we place connection strings in a database table. Here is the code to build an in-memory data provider, which can be accessed:
The first step is creating a Provider which we can use, and in our case this is created using the SubSonic API Class, “DataProvider”. Notice that there are 2 parameters in creating a new instance of Data Provider, with a MySqlDataProvider type.
The next interesting point is the use that of the format of the connection strings, Subsonic expects them to look like that specified in the configuration file above; this being different from the usual “Driver={Driver….}” connection string. Code is below (thanks Andre)

Finally we just create our new Query class, instantiate with our new in Memory Provider and you’re done.
There you go!!!!| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 16 May 2008 Editor: Sean Ewington |
Copyright 2008 by Dane-Garrin Balia Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |