What is a scaffold?

Asked

Viewed 17,728 times

54

I started studying ASP.NET MVC and came across the term scaffold, but I didn’t understand it very well, so:

  1. What is scaffold?

  2. What is its use within ASP.NET MVC?

4 answers

45


What is Scaffold?

Scaffold is an age-old code generation technique based on common operations templates that are used in different applications.

Contrary to what many people believe, probably fruit of having learned by cake recipe, there are several techniques to get the final result.

It is possible to accomplish this at runtime or development time, generating source codes that are aggregated to the project as if written by a person.

This technique uses a specification somewhere to generate all the necessary code based on pre-established code templates somewhere. This specification can be directly or indirectly obtained from the database, model classes or otherwise (an XML/JSON file or specific auxiliary format or even a database with a data dictionary*, for example). The rest is generated by the scaffolding.

The technique is best employed when this same data is used at various points in the code, but even in other situations it can be useful, including generating codes for some better known design patterns.

Also, contrary to what many people think, it is not only used to make CRUD, even if it is at a point where the technique is well employed.

The tool is a great helper to get the DRY.

With C# 7 and Visual Studio 15 started incentives for scaffolding. And in C# 9/10 you should have even more with the addition of Source Generators (even if it is used for something other than scaffold.

Use with MVC

The MVC is a design pattern that proposes the creation of 3 layers, mainly the model and the vision normally are strongly linked to the contents of the database. That is, the database columns are the members of a model class and will be the screen controls in the view. There is a one-to-one ratio. It obviously makes no sense for you to have each of these dissonant parts with each other. And of course also the MVC does not necessarily need to use one database as a data source, this depends on the tool used. the database structure can even be generated by the scaffolding.

In some cases the controller may have something well standardized, especially when it comes to CRUD. And this can be generated by the scaffold, in some cases taking advantage of the same data source of other parties.

So in MVC you have at least three sites, or more, with very bureaucratic code, which we can call Boilerplate, which is repeated in all applications only varying the "fields" that will be used. Its basic structure is the same, no matter in which language, what type of application, design pattern or technology is being applied.

Normally I always preferred the generation done at runtime, even by the type of application I usually work, but for ASP.NET MVC I see generation more common during development, and lately I have seen some value in this.

ASP.NET MVC

It is possible to adopt several tools scaffolding with ASP.NET MVC. You can create your own, but most use the most current ready tool available from Microsoft.

ASP.NET MVC already has good tools integrated with Visual Studio and allows a lot personalization. In general they work best with Entity Framework using the model as specification for the rest. Remembering that the model can be generated using the database as a basis, as confirmed by another answer, contradicting the comments that generated negative in my reply. I quote specifically the excerpt "may be any other element that feeds the code generation chain" and add that one of these elements is the database, even if this is done indirectly.

As the tool uses by ASP.NET MVC is integrated with EF it can generate the codes (including SQL) needed to keep the database in sync with the application.

Then the tool keeps the sync between DB-Model-View (HTML/JS).

All this description and data source freedom of the specification and final result is even more evident with the modern tool of scaffolding Yeoman. Surely time will make it open the mind of those who are accustomed to just a tool of scaffolding.


*Data dictionary is something that has existed for decades, but that has not caught traction in the market. As everything that is not talked about is ignored by the immense majority of people. This is a very powerful tool to actively document what the software works and mainly subsidize the automation of code creation, precisely the scaffolding. If this were better publicized there would be a huge productivity gain for many people, opening a window of opportunity and new form of software development avoiding the bureaucratic work that takes most of the programmers' time. That is why I emphasize that we need to be creative, to seek what is not mainstream, think outside the box and look at the experience of others rather than just follow the flow, look only at existing tools and repeat what everyone else is doing without questions.

  • A library called Way Generator is responsible for scaffold of Laravel :D +1

  • 7

    The vision is not strongly linked to the database. What happens in ASP.NET MVC is that the model is used to generate the views in some predefined operations. The definitions of generating clichés are not obtained from the database. The scaffolding generates, even, the incremental step of the database, called Migration.

  • To me, what you described is being connected to the database. I didn’t say that the vision communicates with the database, only that the vision is generated based on the same model and database information. The view is the "screen" of the data, it is not something that appears out of nowhere with no link to the database, you do not create a view to a customer table by placing information from a tax note there. Indirectly can be obtained from the database.

  • 5

    It’s still wrong. The Scaffolding does not communicate with the database to generate views. Generation is whole offline. If I generate a vision from a Viewmodel, with.

  • I didn’t say that, but there are times when you cling to something and don’t let go, so it won’t generate anything productive here, no point in arguing. Actually your answer confirms what I said.

  • "That is why I emphasize that it is necessary to be creative, to search for what is not mainstream" Where could I find content focused on what would be in the "underground"? Following his vision, meet the scaffold aroused me more interest in search than this out of the recipes or what we do not even get (I) to hear, but that are there to complement strongly. I had no idea scaffold existed, it’s really interesting. @bigown

  • @Martial doesn’t have that much, it’s things you research, study, try. What you can’t do is settle down and think you know everything because you’ve seen what everyone else is talking about. Things cross paths if you have an open attitude. A little luck helps. But there are people who are lucky, and they don’t want her to stay closed to a single vision.

  • Trying to add something, there is the spec MDA created by OMG for quite some time more than 10 years that tried to even standardize something of the kind with a language that could define metamodels in UML and generate systems from that using specific cartridges per platform as in the abandoned Andromda. Where you could model a system with UML tighten play and choose a cartridge. NET or Java for example and exit a practically ready system implemented with MVC from this with use of stereotypes in the model that would be interpreted, something close to a Jhipster d hje.At the time it did not work.

Show 3 more comments

37

What is?

Scaffold is a pre-molded. The term comes borrowed from construction. The conversion technique of the Scaffold on an element of the ASP.NET MVC architecture is called Scaffolding.

What is its use within Asp.net mvc?

Be a development facilitator, generating structures that repeat many times in the system in a quick and simple way.

The starting point for this generation is usually a Model, but it can be any other element that feeds the code generation chain, such as a configuration file. The Scaffolding native became standard in Visual Studio 2013, improperly retiring the other mechanism, known as Mvcscaffolding, because, according to Microsoft, Mvcscaffolding did not follow the usability standard desired by Microsoft.

Last year (from the date of this reply), I joined David Douglas Anderson for make Mvcscaffolding work again in versions of Visual Studio 2013 and 2015. So there are the options of generating cliche code both by Visual Studio interface and by command line.

In ASP.NET MVC projects, Scaffolding enables the generation of CRUD’s, the generation of incremental database steps (in conjunction with the Entity Framework, called Migrations) and the automation of some tasks, such as adding configuration lines or updating code snippets with some structure, such as adding a new element in the data context (again talking about Entity Framework). There is still the possibility to use Scaffolding for generating Javascript elements, in case of typical SPA screens.

The approach of Scaffolding will be fully refurbished from ASP.NET Core 1.0, which is expected to retire from the Scaffolding native and Mvcscaffolding, giving preference to Yeoman.

23

The Mvcscaffolding is a package for ASP.NET that is installed via NuGet using the command MvcScaffolding Install-Package and allows you to automatically create the complete structure for controllers and views.

In general, the scaffold allows you to create a fast working skeleton for your system for high productivity.

In fact, it is a concept, also used in other languages such as Ruby, Python and Grails.

9

The explanations above have been quite thorough about what scaffolding is and its use. I will describe below how to use it in practice. Just to put my explanation in context, every time a controller and views are created, scaffolding uses templates that generate the controller and its views (crud). Therefore, in projects that I have many tables I import and modify the templates to give me more agility, example: I change the terms in English to Portuguese, add libraries, filters, new methods, new view structure, etc.

1) Go to the templates folder of the installation of your visual studio, in my case it is the visual studio 2015 and is at the address:
C: Program Files (x86) Microsoft Visual Studio 14.0 Common7 IDE Extensions Microsoft Web Mvc Scaffolding Templates inserir a descrição da imagem aqui

2) Create the "Codetemplates" folder in your project and import the templates you want to customize:

inserir a descrição da imagem aqui

3) Then edit the templates of the controller or the views you want to modify according to the language you use, in this case I put templates only in ". Cs". In the example below I’m including a line below the controller namespace: "[Authorize(Roles = "Administrator")]" so that every time you create a controller you have this instruction. inserir a descrição da imagem aqui

Okay, now just create a new controller that is in accordance with the template you modified scaffolding will generate the code with the changes made.

  • 1

    I missed the Tangible plugin there, for the T4 Highlight: http://t4-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.