本文主要是介绍知乎周源微信_每周源代码43-ASP.NET MVC和T4和NerdDinner,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
知乎周源微信
UPDATE: David's put the T4 template with some nice updates on CodePlex - It's the last download here.
更新: David在T4模板上对CodePlex进行了一些不错的更新-这是这里的最新下载。
I really advocate folks reading as much source as they can because you become a better writer by reading as much as writing. That's the whole point of the Weekly Source Code - reading code to be a better developer.
我真的提倡人们阅读尽可能多的资料,因为您会通过阅读和写作而成为更好的作家。 这就是每周源代码的重点-阅读代码以成为一名更好的开发人员。
Reading code in Open Source projects is a good way to learn, especially if the project has been around a while and been successful, or if you already respect the team of people working on it. Less reliably, you can find snippets of code by searching and sharing code.
在开放源代码项目中阅读代码是一种学习的好方法,特别是如果该项目已经进行了一段时间并且取得了成功,或者您已经尊重从事该项目的团队的话。 不太可靠的是,您可以通过搜索和共享代码来查找代码片段。
David Ebbo is scary clever. You know someone is smart when they come up with something you don't think you yourself could come up with on your own, but you still kick yourself for not thinking of it in the first place.
大卫·埃博(David Ebbo)非常聪明。 您知道某人提出某件事时很聪明,您认为自己无法独自提出,但是您一开始就因为没有考虑到问题而踢自己。
David's been experimenting with ways to make ASP.NET MVC better, specifically in the area of strongly-typed helpers. He's trying to get rid of strings, magic or otherwise.
David一直在尝试使ASP.NET MVC更好的方法,特别是在强类型帮助程序领域。 他试图摆脱弦乐,魔术或其他方面的困扰。
He started with a BuildProvider (not a lot of folks know about BuildProviders...it's a powerful secret.) David didn't like these kinds of links in ASP.NET MVC:
他从BuildProvider开始(并不是很多人都知道BuildProviders ...这是一个强大的秘密。)David不喜欢ASP.NET MVC中的这些类型的链接:
<%= Html.ActionLink("Home", "Index", "Home")%>
So his BuildProvider would get added to the web.config:
因此,他的BuildProvider将被添加到web.config中:
<buildProviders>
<add extension=".actions" type="MvcActionLinkHelper.MvcActionLinkBuildProvider" />
</buildProviders>
And it would give you better methods like this, by poking around in your project and dynamically generating helper methods for you:
通过在项目中四处浏览并为您动态生成帮助器方法,它将为您提供更好的方法,例如:
<%= Html.ActionLinkToHomeIndex("Home")%>
Then, just days later, David got the T4 religion (I've argued we are doing a poor job of promoting this, so I'm telling everyone I know) and you should too. David explored the pros and cons of CodeDom vs. T4 for CodeGen then recreated his ASP.NET MVC Helpers using T4.
然后,几天后,戴维( David)获得了T4宗教(我认为我们在推广该宗教上做得很差,所以我要告诉我认识的每个人),您也应该这样做。 David探索了CodeDom与T4的优缺点,然后使用T4重新创建了ASP.NET MVC帮助器。
He's enabling not only better ActionLinks, but also Action Url Helpers, Constants for View Names, and a very clever thing, helpers for View Models, courtesy of David Fowler.
他不仅启用了更好的ActionLink,而且还启用了Action Url帮助程序,用于视图名称的常量,以及一个非常聪明的东西,即由David Fowler提供的用于视图模型的帮助器。
I thought this was particular cool, because it's a really visceral example of what you can do with Code Generation when you start exploring what you know at compile time with what you wish you knew at Design Time. It also reminds us that it's more than just Compile/Test/Run - your projects become more "meta" when you've got Inspect/CodeGen/Compile/Test/Run.
我认为这特别酷,因为这是当您开始在编译时使用设计时希望了解的知识时,代码生成可以做什么的真实例子。 它还提醒我们,它不仅仅是编译/测试/运行-当您具有Inspect / CodeGen / Compile / Test / Run时,您的项目将变得更加“元”。
For example, if you have a ViewModel that say, has a string, rather than:
例如,如果您有一个ViewModel,它说一个字符串,而不是:
<label for="Name">Name:</label>
<%= Html.TextBox("Name", Model.Name) %>
<%= Html.ValidationMessage("Name", "*") %>
Why not this instead:
为什么不这样呢?
<%= ViewModel.Name.Label() %>
<%= ViewModel.Name.TextBox() %>
<%= ViewModel.Name.Validation() %>
This ViewModel stuff is in the earlier CodeDom templates, but not (yet?) the T4 templates. I hope we see it again, don't you? What do you think, Dear Reader, of this approach vs. the "Opinionated Input Builder" approach?
ViewModel的内容位于早期的CodeDom模板中,但不是(尚未?)T4模板中。 我希望我们再次看到它,不是吗? 亲爱的读者,您如何看待这种方法与“有意见的输入生成器”方法之间的关系?
Just recently David put out his "new and improved" MVC T4 template, which is the one you should check out. I'm gently pushing him to put it on CodePlex somewhere and bake this goodness in. (Go tell him and Phil!)
David最近发布了他的“新的和改进的” MVC T4模板,您应该检查一下该模板。 我轻轻地推动他将它放在CodePlex上的某个地方,并烘烤这种优点。(去告诉他和菲尔!)
He was doing some stuff at runtime, but has since moved to a pure design-time approach.
他在运行时做了一些工作,但此后转而使用纯设计时方法。
David Ebbo的ASP.NET MVC T4模板应用于Nerd Dinner (David Ebbo's ASP.NET MVC T4 Template applied to Nerd Dinner)
David took the NerdDinner app as his base, and installed his T4 template. Just drop it in, and magic gets generated. How does it work, though?
David以NerdDinner应用程序为基础,并安装了T4模板。 只需将其放入,就会产生魔法。 不过,它如何运作?
Before he was using reflection but since this is a Design Time Code Generation process he had to delve into the *gasp* COM-based VS File Code Model API. However, this does really show off the power and flexibility of T4 itself. Kudos to David for looking COM in the face and not blinking!
在使用反射之前,但由于这是设计时代码生成过程,因此他必须深入研究基于* gasp * COM的VS文件代码模型API。 但是,这确实展示了T4本身的功能和灵活性。 感谢David看着COM而不眨眼!
Go download his template (direct ZIP here) and open it up.
去下载他的模板(在这里直接ZIP )并打开它。
T4 files can be scary to view in a text editor as they are constantly switching between <% code blocks %> and the code that's being generated. I use Notepad2 to view them, using the (interestingly enough) XML Document option that gives me rudimentary syntax highlighting.
T4文件经常在<%代码块%>和所生成的代码之间切换,因此在文本编辑器中查看可能会令人恐惧。 我使用Notepad2来查看它们,并使用了(有趣的是)XML Document选项,该选项使我的语法基本高亮。
If you're serious about T4, go get the Visual T4 Community Edition from Clarius for half-way syntax highlighting, or pay for the Professional Edition. They'll register themselves with Visual Studio and give you a better experience than just all-black text.
如果您对T4感到认真,请从Clarius获得Visual T4社区版,以突出显示语法中的一半,或者付费购买Professional Edition。 他们将向Visual Studio注册自己,并为您提供比纯黑文本更好的体验。
The first line in David's T4 template that made me chuckle was the the first line that's executed:
David的T4模板中让我发笑的第一行是执行的第一行:
<# PrepareDataToRender(this); #>
As they say, it's funny because it's true. This is typical of code generation templates of all kinds. It's the "DoIt()" method that takes the left-hand's code (the COM crap) and prepares it for the right-hand (the template itself).
正如他们所说,这很有趣,因为它是真实的。 这是各种代码生成模板的典型代表。 这是“ DoIt()”方法,它采用左手的代码(COM废话)并为右手(模板本身)做准备。
In order for David to make his template tidy and enable this nice clean level of abstraction:
为了使David的模板整洁并启用这种干净的抽象级别:
<# foreach (var controller in Controllers.Where(c=>c.IsPartialClass)) { #>
namespace <#= controller.Namespace #> {
public partial class <#= controller.ClassName #> {
protected RedirectToRouteResult RedirectToAction(ControllerActionCallInfo callInfo) {
return RedirectToAction(callInfo.Action, callInfo.Controller, callInfo.RouteValues);
}
...snip...
...he has to prepare an object model, and that's what PrepareDataToRender does. It's funny also because it's buried at the end, as it should be. It's really a pleasure to read and does a LOT considering it's only a 415 line template.
...他必须准备一个对象模型,这就是PrepareDataToRender所做的。 这也很有趣,因为它应该被埋在最后。 考虑到这只是一个415行模板,阅读并做很多工作真的很高兴。
He has a number of cute gems like this one-line helper function that combines a little COM a little LINQ and a little magic to a list of methods to his template using the Visual Studio API. I laughed at the "functionFunction" enum. "Do you like her? or do you like her like her?"
他有许多可爱的宝石,例如单线助手功能,它使用Visual Studio API将一个COM,一个LINQ和一个魔术结合到他的模板方法列表中。 我嘲笑“ functionFunction”枚举。 “你喜欢她吗?还是喜欢她喜欢她?”
// Return all the CodeFunction2 in the CodeElements collection
public static IEnumerable<CodeFunction2> GetMethods(CodeClass2 codeClass) {
// Only look at regular method (e.g. ignore things like contructors)
return codeClass.Members.OfType<CodeFunction2>()
.Where(f => f.FunctionKind == vsCMFunction.vsCMFunctionFunction);
}
David is clearly deep into this exploration right now, and is asking your opinion about the design. I would encourage you to flood him with feedback, or leave it hear and I'll tell him. ;)
David现在显然已经深入了这一探索,并在询问您对设计的看法。 我鼓励您向他提供反馈,或者让它听到,我会告诉他。 ;)
翻译自: https://www.hanselman.com/blog/the-weekly-source-code-43-aspnet-mvc-and-t4-and-nerddinner
知乎周源微信
这篇关于知乎周源微信_每周源代码43-ASP.NET MVC和T4和NerdDinner的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!