Das Blog

Debugging one line at a time
$1*/ mo WordPress hosting! Get going with GoDaddy!
Menu
  • Home
  • Coding
    • Entity Framework
    • MVC
    • NHibernate
  • JavaScript
    • TypeScript
  • Hosting
  • SEO
  • Social
  • Database
    • MySql
    • SSIS
  • WP
  • Other
    • CakePHP
    • Error Messages
    • Google Maps
Home
Coding
MVC
Running Sharp MVC Scaffolding project
MVC

Running Sharp MVC Scaffolding project

Codex Discipulus March 8, 2010

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:

Sharp Scaffolding generated classes

Sharp Scaffolding generated classes

Running T4 scaffolding will create the following classes:

  1. An entity class (ex. User.cs) in {MyProject}.Core namespace.
  2. An entity test class (ex. UserTests.cs) in Tests.{MyProject}.Core namespace.
  3. A new folder (ex. Users) under Views containing the following pages (views):
    • Create.aspx
    • Edit.aspx
    • Index.aspx
    • Show.aspx
    • UserForm.ascx (control)
  4. A controller class (ex. UsersController.cs) in {MyProject}.Web.Controllers namespace.
  5. A controller test class (ex. UserControllerTests) in Tests.{MyProject}.Web.Controllers namespace.

Steps to create an entity class using Sharp MVC scaffolding:

  1. Open “ScaffoldingGeneratorCommand.tt” and pass the name of your Entity to the EntityScaffoldingDetails constructor.

    EntityScaffoldingDetails constructor

    EntityScaffoldingDetails constructor

  2. 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.

    EntityProperty constructor parameters

    EntityProperty constructor parameters

  3. 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:

  1. 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
RenderCore method replace locations

RenderCore method replace locations

Replaced from template

Replaced from template

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
Share
Tweet
Google+
Linkedin
Stumble
Email
Prev Article
Next Article

Related Articles

Html.ActionLink not working in Sharp
Framework: SharpArchitecture 1.0.48.410 RC2. Problem: Links (using any form of …

Html.ActionLink not working in Sharp

Sharp Architecture
Sharp Architecture is based on the ASP.NET MVC framework with …

Setup Sharp Architecture

About The Author

Codex Discipulus

Hello, my name is David and I am a disciple of coding, always learning and always ready to help. Welcome to my blog, I hope you find it useful, please send your comments and share an article with your friends.

7 Comments

  1. 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.

    March 23, 2010
    • 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.

      March 25, 2010
  2. 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!

    March 27, 2010
    • DaCoder

      Thanks, I am really glad my posts are helping. I thought I was the only getting the “weird errors” 😉

      April 25, 2010
  3. Victor

    How can I have scaffolding generate the tables as well?

    July 6, 2010
    • 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.

      July 6, 2010
  4. Anthony Alejo

    Excellent post. help me a lot dealing with a similar issue. Thanks.

    July 26, 2013

Leave a Reply

Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Hosting ASP.Net Core 3.x Worker Service as …
    Using the Worker template now is possible to host an …
  • Calling an HTTPS WCF Service from Net …
    We all know how to call a WCF service from …
  • How to Add a VS Code Project …
    Let’s try to add a Visual Studio Code project to …
  • Step by Step Token based Authentication in …
    Token Authentication is a very popular method to secure a …
  • How to Ignore NuGet Packages in TFS …
    This should be a short post. It is really just …

Categories

  • CakePHP
  • Coding
  • Database
  • Entity Framework
  • Error Messages
  • General
  • Google Maps
  • Hosting
  • JavaScript
  • MVC
  • MySql
  • Net Core
  • NHibernate
  • SEO
  • SSIS
  • TypeScript
  • Wordpress

Tags

ActiveDirectory analytics apps aspnet CakePHP coding database projects design patterns entity framework error messages fatcow first steps ftp github gitlab godaddy google maps api hangfire Hosting iis7 java javascript membership MVC MySql NHibernate nuget nuget packages oracle owin repository SEO sharp ssis t4 tfs thesis token authentication typescript vs2010 vs2015 vs2017 web api Wordpress wp-pagenavi

Das Blog

Debugging one line at a time
Copyright © 2020 Das Blog
ScottsdaleWebStudio.com © All Rights Reserved 2017