Sharp MVC Architecture will create a default CrudScaffolding code generation project in each New Project it creates. Sharp’s scaffolding uses the Visual Studio T4 Templating technology for code generation. T4 (Text Template Transformation Toolkit) is the code generator built right into Visual Studio. To create a template all you need is to add a text file but with the name extension “.tt” and save any changes.
Using the code generation that comes with Sharp Architecture requires little customization to start creating your classes. For any serious or more in deep code generation like creating your own custom templates I will have to recommend the Clarious Visual T4 Editor. Visual T4 provides support for C# code, preview of the transformation output, preview of the generated transformation class, enhanced debugging, support for ASP.NET MVC, MS Build support and even NHibernate code generation samples. Clarius offers a free T4 Editor Community Edition with less features too.
Okay, back to our Sharp MVC scaffolding project:
Running T4 scaffolding will create the following classes:
- An entity class (ex. User.cs) in {MyProject}.Core namespace.
- An entity test class (ex. UserTests.cs) in Tests.{MyProject}.Core namespace.
- A new folder (ex. Users) under Views containing the following pages (views):
- Create.aspx
- Edit.aspx
- Index.aspx
- Show.aspx
- UserForm.ascx (control)
- A controller class (ex. UsersController.cs) in {MyProject}.Web.Controllers namespace.
- A controller test class (ex. UserControllerTests) in Tests.{MyProject}.Web.Controllers namespace.
Steps to create an entity class using Sharp MVC scaffolding:
- Open “ScaffoldingGeneratorCommand.tt” and pass the name of your Entity to the EntityScaffoldingDetails constructor.
- Add instances of EntityProperty, one for each property on the Entity (not including the Id property). Obviously, delete the few existing properties included for demonstration purposes. Please review CrudScaffolding/EntityScaffoldingDetails.tt for a described listing of parameters which the overloaded constructor of EntityProperty can accept.
- Un-comment the last line, generator.Run() , and save the file. The file generation will occur automatically. After it has completed, re-comment // generator.Run()
Compilation Transformation Error Messages
If you get a lot of errors like this: “Compiling Transformation: ‘Microsoft….ShowTemplate’ does not implement inherited abstract member ‘Microsoft.VisualStudio.TextTemplating.TextTransformation.TransformText()’ do this:
- Replace all the protected override void RenderCore() in the Templates (such as Templates/Core/DomainObjectTemplate.tt) to: ‘public override string TransformText()’
Append return this.GenerationEnvironment.ToString(); at the end of public override string TransformText() method
After code generation by T4 is completed, build and run some tests to make sure everything is fine. And after that don’t forget to add your table to your database if you haven’t done that yet.
Links:
- Clarius Visual T4 can be found here: http://www.visualt4.com/home.html
- Sharp Wiki Visual Studio Templates and Code Generation can be found here: http://wiki.sharparchitecture.net/VSTemplatesAndCodeGen.ashx
KS
You have no idea how many hours of frustration your posts on Sharp have saved me! We both seem to be progressing in our projects at the same pace so keep these glorious bits of information coming.
I look forward to your next post.
DaCoder
Thanks for your kind words. Sharp Architecture is an awesome open source framework that has a steep learning curve (and yes! many hours of frustration for all) but the rewards are huge.
Shadowskill
Yeah, like KS I’ve been looking for solutions to the generators’ compilation Transformation Error Messages for hours, and your post is the only one that mentions this and right on the spot.
Thanks alot for the article and keep ’em coming!
DaCoder
Thanks, I am really glad my posts are helping. I thought I was the only getting the “weird errors” 😉
Victor
How can I have scaffolding generate the tables as well?
DaCoder
The scaffolding project included in Sharp MVC Architecture framework generates only entity, controller, views and test classes. I am sure a schema generator can be created using T4 or a similar code generation tool, but the template is not included in Sharp, my guess is that much like other frameworks the approach is database schema first, entities second (at least for code generation and not coding principles). A combination of T4 and Linq to Sql can probably be used to create a schema generator but I am not familiar with that process. Remember too that if you take the approach of generating tables you will have to generate data inserts just because every time you generate will most likely be a drop database first and all data will be lost. Thanks for your good question and let me know if you find a good solution.
Anthony Alejo
Excellent post. help me a lot dealing with a similar issue. Thanks.