The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  83 Posts :: 0 Stories :: 57 Comments :: 2 Trackbacks

Ads

 

Donate via PayPal...

...if you feel the site helped.

Archives

Post Categories

Open Source Projects

Other Blogs

I was busy with a(nother) comparison of Castle ActiveRecord and the Microsoft offerings that have shipped with VS2010 just to make sure that I wasn’t missing out on anything. I ended up fiddling with EDMX files, and then custom generation, which then led me to downloading the ADO.Net POCO T4 generation templates and checking out what was in the files.

I have to admit: when I saw what was in it, I got quite giddy with excitement, followed shortly by feeling like a tool. I can’t believe I’ve missed out on this excellent piece of functionality that has been around for ages.

I decided to tackle making a template that generated output that matched what is generated by Generator Studio, which is an open source project I started with FryHard a few years back, primarily to generate my ActiveRecord classes from my database model. I based my template on the ADO.Net POCO template from Microsoft and used it as my guideline.

Explaining the code in the TT file is a little out of the scope of this article, but it is available here for download

You can paste the zip file in

%userprofile%\Documents\Visual Studio 2010\Templates\ItemTemplates\Visual C#\

and then open the Visual Studio Command Prompt (as Administrator) and run:

devenv /installvstemplates

The command above installs the new templates for Visual Studio.

The quickest way to use it in much the same way as a generator is as follows:

  1. Create a new class library or windows app project.
  2. Add a reference to Castle.ActiveRecord.
  3. Add an ADO.NET Entity Data Model to your project.
  4. Choose the “Generate from Database” option in the wizard
  5. Run through the rest of the wizard adding the tables you’d like to generate ActiveRecord classes from.
  6. Open the entity model in the IDE, and change the “Code Generation Strategy” property in the properties tab to “None”. This remove all code from the designer class attached to the EDMX file.
  7. Add a new “Castle ActiveRecord Basic Template” item to your project. This is the template contained in the download above.
  8. Once the template has been added, you can look at the files generated by it, and there should be 1 each for each table in your database, as well as an additional file that has the same name as the template (except with a .CS extension), that has no functionality in it. This is created by the template (as it was in the original from MS), and I’ve left it there because I’ll most likely use for custom features/base classes as I build on the template.

You should now be able to use the ActiveRecord classes generated as you normally would.

If you make changes to your model in the EDMX designer, you can simply right-click on the template file in the solution and select the “Run Custom Tool” to regenerate your classes.

posted on Wednesday, August 18, 2010 11:32 AM

Feedback

# re: T4 Text Template for Castle ActiveRecord 9/3/2010 2:41 PM Michael Maddox
I have written a Castle ActiveRecord code generator based on T4 which is more point and click. If you are willing to try it (there is a free version), I'd be interested to know what you think:

www.AgilityForORMs.com/.../AfoCastleActiveRecor...


Comments have been closed on this topic.