SubSonic BlogProvider for BlogEngine.net
One of the things I love about BlogEngine.net is that it is built with extensibility in mind. On example of this extensibility is the ability to customize the provider which it uses to retrieve and persist posts, pages, categories, etc.
Currently there are two providers that are supported out of the box: The XmlProvider and the MSSQLProvider. After I moved my blog from wordpress.com to BlogEngine.net, I decided I wanted to store my posts and such in a database rather than in xml files. So I set out to get the MSSQLProvider up and running. There is not, however, any established way to convert all of your content from the XmlProvider to the MSSQLProvider. So I first attempted to create my own quick little script to select the content from one provider and insert it using the other. But this didn't work. I ran into connection management problems within the MSSQLProvider code.
SubSonic to the Rescue
Then it dawned on me: what I really wanted was an easy to use data access layer to access the Sql Server tables which I wanted to store my blog content in. Well, since I was planning on using the schema which ships with BlogEngine.net, I already had the schema created. This is the kind of thing that SubSonic is great at - generating an easy to use data access layer given a database schema.
Subsonic ships with a console application called Sonic.exe which can be used to generate the data access classes. Since the database schema in this case is fairly static, I figured I could just generate the classes once and not worry about using a BuildProvider or anything fancy like that. So, here is the command line I used to generate the classes:
sonic generate /override /server HOMIE /db BlogEngine
/userid OMMITED /password OMMITED /out C:\code\out
/generatedNamespace BlogEngine.Core.Providers.SubSonic /stripTableText be_
I used the stripTableText option to replace take out the 'be_' prefix from each table which is present in the Sql schema. This results in classes which are named 'Post' and 'Page' rather than 'BePost' and 'BePage'.
Now that I had my data access layer, it was time to write the BlogProvider.
All Your Provider Are Belong To Us
Implementing the provider was pretty straight forward. There isn't really documentation I can find about exactly how the provider should behave so I based my code of the MSSQLProvider. I will not really go over the code too much here because there is not all that much to it. Mostly, it is a matter of mapping the SubSonic generated data access objects to the BlogEngine.net types like this:
private static Post GetPost(SubSonicPost p)
{
Post post = new Post();
post.Id = p.PostID;
post.Title = p.Title;
post.Content = p.PostContent;
post.Description = p.Description;
post.DateCreated = p.DateCreated ?? DateTime.MinValue;
post.DateModified = p.DateModified ?? DateTime.MinValue;
post.Author = p.Author;
post.IsPublished = p.IsPublished ?? false; ;
post.IsCommentsEnabled = p.IsCommentEnabled ?? false;
post.Raters = p.Raters ?? 0;
post.Rating = p.Rating ?? 0;
post.Slug = p.Slug;
return post;
}
Pretty boring and repetitive code.
Configuration
In order to configure your site to use this provider you must first configure it for SubSonic and then add the SubSonic provider. First, add the SubSonic config section to your confSections in your web.config:
<section name="SubSonicService"
type="SubSonic.SubSonicSection, SubSonic"
requirePermission="false"
allowDefinition="MachineToApplication"
restartOnExternalChanges="true"/>
And here is the SubSonic section:
<SubSonicService defaultProvider="Default">
<providers>
<clear/>
<add name="Default" type="SubSonic.SqlDataProvider, SubSonic"
connectionStringName="BlogEngine"
generatedNamespace="BlogEngine.Core.Providers.SubSonic"
enableTrace="false" />
</providers>
</SubSonicService>
Lastly, you can edit the blogProvider section:
<BlogEngine>
<blogProvider defaultProvider="SubSonicProvider">
<providers>
<add name="XmlBlogProvider"
type="BlogEngine.Core.Providers.XmlBlogProvider, BlogEngine.Core"/>
<add name="MSSQLBlogProvider"
type="BlogEngine.Core.Providers.MSSQLBlogProvider, BlogEngine.Core"/>
<add name="SubSonicProvider"
type="BlogEngine.Core.Providers.SubSonicProvider, BlogEngine.Core.Providers.SubSonic"/>
</providers>
</blogProvider>
</BlogEngine>
One Gotcha
The one problem I ran into was that SubSonic doesn't appear to run in a medium trust environments because it uses Tracing calls which require elevated trust. This does not mix well with BlogEngine.net and running in a medium trust environment. To get around this I commented out the tracing calls from the SubSonic source code and recompiled the SubSonic.dll. This is just a workaround until I find a permanent fix. You can read this forum post for more details.
So that's it. Here is the code. Feel free to give me any feedback or questions you have.
BlogEngine.Core.Providers.SubSonic.zip
UPDATE: The problem with medium trust and SubSonic has since been fixed so I will remove the SubSonic.dll from this site and just send you to the SubSonic site for download.