About

SubSonic is a .net open source project developed by Rob Conery and a core team of developers including Eric Kemp, Scott Watermasysk, Jon Galloway, Phil Haack, and Gavin Joyce. The current stable release is version 2.0.3. Nightly builds are available in our SVN respository.

Tags

Magic Code: Using the BuildProvider

Let's Watch a Movie...

Setting Up Your Website With SubSonic
This is a walkthrough of how to get rolling quickly with SubSonic

Installing and Using SubSonic

1) Head on over to CodePlex and Download the current release of SubSonic (.msi)

2) Install to the default directory

3) Open up Visual Studio, and select "File > New Website"

Once your website is created, right-click on the project and select "Add Reference". A dialog box will pop up - click on the "Browse" tab and select SubSonic.dll from the install location.

4) Once SubSonic is installed, right-click your web project and select "Add New Item". From the list select "Web Configuration File"

Open up the Web.config file - we need to do a little surgery. First, declare a config section for SubSonic, right under the <configuration tag:

  <configSections>

    <section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" requirePermission="false"/>

  </configSections>

Next, add a connection to your database (let's assume it's Northwind, located on the local SQLExpress instance):

<connectionStrings>

  <add name="Northwind" connectionString="Data Source=localhost\SQLExpress; Database=Northwind; Integrated Security=true;"/>

</connectionStrings>

Next, we need to define our providers. Each SubSonic provider defines one database:

  <SubSonicService defaultProvider="Northwind">

    <providers>

      <clear/>

      <add name="Northwind" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="Northwind" generatedNamespace="Northwind"/>

    </providers>

  </SubSonicService>

Notice how here, we told the provider that it's generatedNamespace is "Northwind" - this means that in our code we'll be able to access all the generated bits using "Northwind.[TableName].Fetch()"

Next, if you want SubSonic to generate the DAL "behind the scenes", with no need for you to interfere, you'll need to declare a BuildProvider. A BuildProvider is a process the runs when Visual Studio compiles an application, or when a file is added to the App_Code folder. In our case we're going to define a BuildProvider that gets run for every file in the App_Code directory, having an extension of ".abp" (notice that this goes under the "<compilation" tag):

    <compilation debug="true" defaultLanguage="C#">

      <!--########################## SubSonic Build Provider ###############################-->

      <!--This will NOT WORK in Medium Trust-->

      <buildProviders>

        <add extension=".abp" type="SubSonic.BuildProvider, SubSonic"/>

      </buildProviders>

If you will be deploying your application to an ISP that uses shared hosting, you probably won't be able to use the BuildProvider since it won't run in MediumTrust. If this is the case, you can use SubSonicCentral (the web site downloaded with SubSonic) to generate the code for you, or you can use the Command Line tool.

The last step here is adding a file to the App_Code directory. If you don't have one in your project, right click the project title and select "Add ASP.NET Folder > App_Code". Right-click this folder and add a text file and call it "subsonic.abp". The actual file name is not important - it just has to have this extension however. The text in the file is also not important.

Now, right-click and compile your site! You're done.

Subscribe