First of all, thank you for your support of the magicodes.net framework. As I said in my last article, it may take at least a year for the framework to mature, after all the personal power is limited. We hope that interested small partners can join us and contribute. At the same time the problem of small partners please do not ask questions in the group, QQ Group is limited to technical exchanges.
For all questions about Magicodes.net, please submit the appropriate issues on this Https://github.com/magicodes/Magicodes.NET/issues page based on the type. Before using Magicodes.net, please review the official documentation and readFAQ (click here to read).
The previous article mentioned T4 code generation, and as the build went deeper, I felt that there was a problem with this generation pattern:
- Too dependent on the front-end script (the deletion of the check is processed on the same page, the background controller just return to view)
- Inconvenient to change page theme (front end too heavy)
- View server-side code support is not good enough (because it basically relies on the front-end JS binding).
- It's not easy to scale up or expand. For example, when adding and editing, the drop-down box bindings, check box group bindings (of course, there are workarounds), you need to write a large number of generic scripts. With the complexity of the data model, this approach to maintenance becomes more and more cumbersome.
Based on the above reasons, I decided to abandon this mode and start a new T4 template generation study tour.
Most of the time, only re-come, can stand higher. So I said that the framework may be mature for at least a year, because I am still trying and learning. Also hope that the small partners can support a lot, Daniel can be a lot of guidance.
At the time of the study, I fixed my gaze on the ASP. Scaffolding ...
ASP. NET scaffolding template (ASP)
ASP. Scaffolding. Maybe a lot of little friends haven't heard of it, but you're already using it. This thing was written by an engineer at Microsoft and then integrated into the VS2013. What is this thing? Take a look at the picture below and you may be reminded of it.
When adding a controller:
Then select the Model class, DbContext, etc.:
When you click Add, you'll find that he's done a lot of bunkers:
Controller and additions and deletions are all generated!!!
Let's take a look at the generated controller code:
Additions and deletions were readily found, and safety was also taken into account. Similarly, the associated view is generated, showing a simple list and adding deletions.
For details, see: Http://www.asp.net/visual-studio/overview/2013/aspnet-scaffolding-overview
Let's take a look at the generated original interface:
Although Dusty, it is a very powerful way. It seems that the code generation in an instant pulled up a few grades, personally think this way is quite good. Give me the first feeling is, mom, good tall ah, I want to customize.
Customizing the ASP. NET MVC Scaffolding
About ASP. NET MVC scaffolding The domestic is still relatively deficient, FQ study for a while, also have a little effect:
Is it an instant and big on a lot? People depend on clothes, do not mind, this is not beautiful enough. It's just superficial. Think about it, if you can generate the corresponding JS control according to the control type, add logic control, then perfect!!
Don't talk nonsense, let's see how to customize this stuff.
The first step is to install Sidewaffle:
Can be downloaded at this URL: http://sidewaffle.com/
The second step is to select the ASP. T4 File:
Then you will find that there is a codetemplates directory in the project and there are many T4 files in it:
This is the MVC Scaffolding T4 template (including Webapi and so on), and then we can happy the customization, just now:
For example, edit the template, we can add some style embellishment embellishment:
And that's what it looks like.
Just happy soon, then found the problem!!!
Issues with ASP. NET MVC Scaffolding templates
For example, in the List.cs.t4 template, I need to get the class characteristics of the model class to make it easier to do something bad. Like getting a list title and so on, and then I found out I had a cup. First look at the template parameters (in file Imports.include.t4):
<#@ parameter type= "System.String" name= "Viewdatatypename" #>
<#@ Parameter type= "System.String" name= "Viewdatatypeshortname" #>
<#@ parameter type= "System.Boolean" Name= " Ispartialview "#>
<#@ parameter type=" System.Boolean "name=" islayoutpageselected "#>
<#@ parameter Type= "System.Boolean" name= "referencescriptlibraries" #>
<#@ parameter type= "System.Boolean" Name= " Isbundleconfigpresent "#>
<#@ parameter type=" System.String "name=" ViewName "#>
<#@ parameter type= "System.String" name= "Layoutpagefile" #>
<#@ parameter type= "System.String" name= "jqueryversion" #>
<#@ parameter type= "Microsoft.AspNet.Scaffolding.Core.Metadata.ModelMetadata" name= "Modelmetadata" #>
It is not difficult to see from the parameters that we are able to use these. A moment of silence. Then we found out that the man in Microsoft opened a window for us, but he was caught in the head by the door. The only thing we can hope for is the parameter in type: Microsoft.AspNet.Scaffolding.Core.Metadata.ModelMetadata
Hey, this thing so familiar, I look at MVC and Modelmetadata and EF modelmetadata a bit like. But in fact, a little far.
Https://msdn.microsoft.com/en-us/library/microsoft.aspnet.scaffolding.core.metadata.modelmetadata (v=vs.118). aspx
Give me something like this, model type can not get, you let me play!!! The same property is not get the type of, mom eggs, let me white happy. Microsoft that buddy, brother know the parameters T4 template is not written, parameters passed over the serialization, not good delivery type, otherwise the deserialization will have problems, but you also want to think of ways Ah, are your home things, you put on a magnificent shelf, and then no, let Brother love why!! Sure enough, it's just scaffolding. To this end, the elder brother also in StackOverflow in poor English to ask a question:
Http://stackoverflow.com/questions/28063731/how-to-get-model-type-in-microsoft-aspnet-scaffolding-list-cs-t4
This is the air ticket of the blog park: http://q.cnblogs.com/q/69370/
Well, in order to facilitate the T4 template parameter passing, the Microsoft Buddy is to handle the properties of ViewModel and then passed through the parameters, T4 template parameters used serialization and deserialization, if the model type, you have to throw the relevant DLL inside the GAC, so it is now. Naturally, the theory should be no solution, so helpless to give up.
Epilogue
It seems that the great idea does not work ah, it seems that you have to honestly re-write it. Now that the T4 generation of the controller has been refactored, it is ready to start rewriting the view's T4 template. Turning on the new mode, I will introduce the new T4 template generation in the next article.
╮(╯▽╰)╭, the egg hurts.
Magicodes.net Frame Path-Let the code fly for a while (ASP. Scaffolding)