Generating Your DAL
-
Create sample database
create database subSonicExampleDB
use subSonicExampleDB
CREATE TABLE [codeSnippets] (
[id] int IDENTITY(1, 1) NOT NULL,
[languageID] int NOT NULL,
[name] varchar(255) NOT NULL,
[body] varchar(8000) NULL,
PRIMARY KEY CLUSTERED ([id])
)
GO
CREATE TABLE [langauges] (
[id] int IDENTITY NOT NULL,
[name] varchar(255) NOT NULL,
[syntaxHighlighterClass] varchar(10) NULL,
PRIMARY KEY CLUSTERED ([id])
)
GO
EXEC sp_grantdbaccess 'test', 'test'
GO
EXEC sp_addrolemember 'db_owner', 'test'
GO
insert into dbo.languages(name) values('VB.NET')
insert into dbo.languages(name) values('C Sharp')
insert
into dbo.codeSnippets (languageID, name, body)
select (select id from languages where name = 'vb.net'),
'SubSonic Collection',
'Dim posts As PostCollection = New
PostCollection().OrderByDesc("PostedOn").Load()'
insert
into dbo.codeSnippets (languageID, name, body)
select (select id from languages where name = 'c #'),
'SubSonic Collection with Clauses',
'Northwind.CustomerCollection customers = new
Northwind.CustomerCollection()
.Where("Country", "Canada")
.Where(Northwind.Customer.Columns.Region, "BC")
.OrderByDesc(Northwind.Customer.Columns.PostalCode)
.Load();'
insert
into dbo.codeSnippets (languageID, name, body)
select (select id from languages where name = 'vb.net'),
'SubSonic Entity',
'If Not
String.IsNullOrEmpty(Request.QueryString.Item("id")) Then
Dim id As Integer =
Int32.Parse(Request.QueryString("id"))
post = New MyBlog.Post(id)
End If'
insert
into dbo.codeSnippets (languageID, name, body)
select (select id from languages where name = 'c #'),
'SubSonic Relationships',
'Northwind.Customer.FetchByID("BOTM")
.Orders()[0]
.OrderDetails()[0]
.Product
.ProductName;'
ALTER TABLE [dbo].[codeSnippets]
ADD CONSTRAINT [codeSnippets_fk] FOREIGN KEY ([languageID])
REFERENCES [dbo].[languages] ([id])
ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
create proc listAll
as
select cs.id, cs.languageID,
cs.name, l.description as language
from [dbo].[codeSnippets] as cs
inner join
[dbo].[languages] as l
on
cs.languageID = l.id
GO
-
Setup configuration
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="SubSonicService"
type="SubSonic.SubSonicSection, SubSonic"
requirePermission="false" />
</configSections>
<connectionStrings>
<clear/>
<add name="subSonicExampleDB"
connectionString="Data
Source=beehive;Initial Catalog=subSonicExampleDB;User Id=test;Password=test"
/>
</connectionStrings>
<SubSonicService defaultProvider="subSonicExampleDBProvider">
<providers>
<clear/>
<add name="subSonicExampleDBProvider"
type="SubSonic.SqlDataProvider, SubSonic"
connectionStringName="subSonicExampleDB"
generatedNamespace="dal"
spClassName="storedProcs"
/>
<!--fixPluralClassNames="false"-->
<!--stripSPText="sp"-->
<!--stripViewText="v"-->
</providers>
</SubSonicService>
</configuration>
-
Generate DAL
-
With NAnt
<project name="subsonicExample"
default="build" basedir="."
xmlns="http://nant.sf.net/release/0.86-beta1/nant.xsd">
<!-- The relative path to the directory where SubSonic.dll is
found -->
<property name="subSonicRelativePath" value="..\SubSonic 2.1
Beta 2\SubSonic" />
<!-- The relative path to the SubSonic commander -->
<property name="sonicCommanderRelativePath" value="..\SubSonic
2.1 Beta 2\SubCommander\sonic.exe" />
<!-- The command line arguments for the SubSonic commander
-->
<property name="sonicCommanderArguments" value="generate" />
<!-- The root namespace for the project -->
<property name="build.rootNamespace" value="dal" />
<!-- The build type (release or debug) for the project -->
<property name="build.config" value="release" />
<!-- The relative path to the project directory (from the
location of the build file) -->
<property name="targetDirectoryRelativePath"
value="." overwrite="false" />
<!-- The relative path to directory where SubSonic
will generated the files (from the project target directory) -->
<property name="subsonicGeneratedFilesRelativePath"
value="generated" />
<property name="targetDirectoryFullPath"
value="${path::get-full-path(targetDirectoryRelativePath)}"
overwrite="false" />
<property name="subsonicGeneratedFilesFullPath"
value="${path::combine(targetDirectoryFullPath, 'generated')}" />
<property name="binDirectory"
value="${path::combine(targetDirectoryFullPath, 'bin')}" overwrite="false"
/>
<echo message="${subsonicGeneratedFilesFullPath}"/>
<property name="buildDirectoryFullPath"
value="${path::combine(binDirectory, build.config)}" overwrite="false"
/>
<property name="buildOutput" value="dal.dll" />
<property name="subSonicFullPath"
value="${path::get-full-path(subSonicRelativePath)}" />
<!-- The full path to the SubSonic commander -->
<property name="sonicCommanderFullPath"
value="${path::get-full-path(sonicCommanderRelativePath)}" />
<!-- Default build target -->
<target name="build">
<delete>
<fileset>
<!-- Delete any visual studio related files -->
<include
name="${subsonicGeneratedFilesFullPath}/*.*" />
</fileset>
</delete>
<!-- Execute the SubSonic commander to
generated the files in the defined "sonicCommanderFullPath" directory
-->
<exec
basedir="${targetDirectoryFullPath}"
program="${sonicCommanderFullPath}"
commandline="${sonicCommanderArguments} /out
"${subsonicGeneratedFilesFullPath}""
workingdir="${targetDirectoryFullPath}"
failonerror="true" />
<!-- Execute the vb compiler to compile the SubSonic
generated files -->
<csc target="library"
output="${path::combine(buildDirectoryFullPath, buildOutput)}">
<sources>
<!-- Include all
the SubSonic generated files -->
<include
name="${subsonicGeneratedFilesFullPath}\*.cs" />
</sources>
<!-- Include a reference to
the SubSonic dll -->
<references
basedir="${subSonicFullPath}">
<include name="SubSonic.dll" />
</references>
</csc>
</target>
</project>
-
With SubCommander as external VS tool
-
Go to Tools/External tools
-
Add a new tool:
-
Title: NAnt
-
Command: "path\to\SubCommander\sonic.exe"
-
Arguments: generate /out generated
-
Initial Directory: $(ProjectDir)
-
Check "Use Output Window"
-
With a build provider in ASP.NET
-
Reference SubSonic.dll
-
Add provider setting
<buildProviders>
<add extension=".abp"
type="SubSonic.BuildProvider, SubSonic"/>
</buildProviders>
-
Create empty .apb file in "App_Code"
-
Rest Access to your data
http://localhost/subSonicWeb/codesnippets/list.xml
http://localhost/subSonicWeb/codesnippets/list.json
http://localhost/subSonicWeb/codesnippets/show.xml?id=1
http://localhost/subSonicWeb/codesnippets/show.json?id=1