Spontaneous Publicity
blogs are the new phone book

SubSonic BlogProvider for BlogEngine.net

March 26, 2008 16:06 by Luke

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.

xmltodatabase 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'.

ssconvert

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.

kick it on DotNetKicks.com

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: .Net | BlogEngine.net | C# | Development
Actions: E-mail | Permalink | Comments (15) | Comment RSSRSS comment feed

Related posts

Comments

March 26. 2008 17:40

Zack Owens

Damn... you beat me to the chase Smile

Anyways, I can't wait to try it out!

Zack Owens

March 27. 2008 03:57

Rob Conery

Sweet! Nice work on this Smile. We're fixing the Medium Trust issue - that was my fault BTW Smile.

Rob Conery

March 27. 2008 05:39

Jason Kealey

Keep up the good work.

Jason Kealey

March 27. 2008 15:25

pingback

Pingback from alvinashcraft.com

Dew Drop - March 27 | Alvin Ashcraft's Morning Dew

alvinashcraft.com

March 27. 2008 15:40

Boyan Kostadinov

Neato. Thanks! I am glad to see new uses for SubSonic.

Boyan Kostadinov

March 27. 2008 16:03

Eric Kemp

Nice stuff!

FYI, the MediumTrust trace issue was fixed in SVN just after SubSonic Beta 2 rolled... It will be included in Beta 3....

Eric Kemp

March 27. 2008 16:04

Boyan Kostadinov

Neato. Thanks! I am glad to see new uses for SubSonic.

Boyan Kostadinov

March 28. 2008 02:58

Yitzchok

Nice post.

And I think it goes like this
All Your Provider Are Belong To Us

Yitzchok

March 29. 2008 07:32

megaupload links

thanks for the help on BlogEngine.net

megaupload links

March 29. 2008 14:21

LesNoticias.com

Good site.

Thanks

Muchas gracias

www.lesnoticias.com

LesNoticias.com

March 31. 2008 16:48

mikedopp

Can't tell you how cool this is. Now I can Run not walk from Subtext. Great work! Thanks for the info.

mikedopp

April 23. 2008 14:06

Codes Web

thanks you

Codes Web

April 28. 2008 12:22

Sean Gilbert

Great job!

I'm happy to see I was not the only person excited to tackle this endeavor. I'm itching to take a more in depth look at this.

Sean Gilbert

May 30. 2008 10:58

Matt

Hello,

I am looking for someone local to Boston, MA to perform a short term SubSonic Project. This could even be done part-time off hours. A start up company we work with is new to SubSonic and looking for someone to perform some tasks and provide some expertise to the other developers. If you are intersted, please let me know. If you know anyone who may be intersted, we could get you a referral bonus. Thanks.

- Matt

Matt

June 10. 2008 04:46

Steven

I tried to download the
www.spontaneouspublicity.com/.../...s.SubSonic.zip
but I got the following message, feel free send me a copy, thanks.

Application Firewall Alert


Your request triggered an alert! If you feel that you have received this page in error, please contact the administrator of this web site.


To immediately resolve this problem. You can login to your hosting control panel->security->Security guard and set the security guard to Medium Level for your domain name.

Steven

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

July 24. 2008 08:32