I found a reference on Scott Watermasysk's blog about a project called "SubSonic" on CodePlex.  I watched the video demo for the project and I was very impressed with quite a few things here.

First off, the DAL that SubSonic generates is top notch.  I think it's very flexible and a lot of programmers will like the flexibility that it provides.  This is not what caught my eye, though.

SubSonic has the ability to generate code on the fly at compile-time via something called a BuildProvider (I'd never heard of this).  Basically, you just specify this custom BuildProvider in the web.config of your ASP.NET project and at design and/or compile time, the BuildProvider will connect to the specified database, and generate SubSonic's DAL model.

Now here's where I got excited.  SubSonic is great, but it limits you to using its own DAL template (which is again, the bee's knees), but what if you like the Wilson O/RMapper?  Maybe you like .netTiers for your DALs.  Wouldn't it be great to use a BuildProvider to leverage CodeSmith?  Yes.

So I wrote one.

This gave me the chance to really learn about BuildProviders and also to take a look at the CodeSmith SDK while I was at it.  I'd never used it before, but it was really easy to use.

To use it, you simply add the BuildProvider to the web.config of your choice.

I've specified that for this website, any files named with the .example extension (I know, original right?) should be compiled with the custom BuildProvider.

To create the file to be compiled by the BuildProvider, simply create a text file with the aforementioned extension and insert lines with a Codesmith template file and an associated settings file (separated by the pipe, "|", symbol) in this form:

<Codesmith Template File>|<Settings For This Template>

 Example:

c:\MyCodesmithTemplate.cst|c:\MyCodesmithTemplateSettings.xml

To generate the settings file, open your template in Codesmith explorer, fill in the appropriate fields and then click "Save Property Set XML".

Armed with this and the providers in the right spot, you should get almost immediate intellisense information back for the types that were just generated.

I'm including the provider and a sample web project so that you can see how this works.  I hope at the very least this will help someone understand BuildProviders.  They are interesting and very powerful tools if used correctly.

Some caveats:

  1. BuildProviders cannot function in Medium Trust
  2. I cannot provide a copy of the Codesmith assemblies with this project.  You can get your own eval copy of Codesmith by visiting http://www.codesmithtools.com.
  3. If you do use an eval copy of Codesmith with this, you will get a Codesmith nag screen every time you compile.  You have been warned. (I developed this using an eval copy, so I know a bit about it.)
  4. To deploy this to a production environment, I believe you would have to have purchased the Codesmith SDK or at a minimum purchased one license of Codesmith Pro for the production environment.  No, I don't know how much the Codesmith SDK is - could be a million dollars.
  5. I provide no warrantees.  Use at your own risk.
  6. There are most-likely bugs.  Feel free to contact me about them anytime.

Download: CodesmithAddons
Video Demo: Codesmith Video Demo

References

SubSonic Community
MSDN BuildProvider Documentation
ASP.NET 2.0 Build Providers - Mitch Denny

[Update: Uploaded a new version that includes a fix for a locked file issue.  Forget to close a stream?  Not me!]

Trackbacks

Anderson On... :

I was reviewing the latest version of SubSonic that was released recently and got excited about it again

Link

Anderson On... :

I've been diving deep into some emerging technologies lately. One of the areas of interest to me has

Link

Comments

No Comments

Leave a Comment