知乎周源微信_每周源代码43-ASP.NET MVC和T4和NerdDinner

2023-12-20 11:50

本文主要是介绍知乎周源微信_每周源代码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")%>
image_7

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)

image_d417b977-3d4c-4d3c-88e6-438b7a32582c

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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/515944

相关文章

W外链微信推广短连接怎么做?

制作微信推广链接的难点分析 一、内容创作难度 制作微信推广链接时,首先需要创作有吸引力的内容。这不仅要求内容本身有趣、有价值,还要能够激起人们的分享欲望。对于许多企业和个人来说,尤其是那些缺乏创意和写作能力的人来说,这是制作微信推广链接的一大难点。 二、精准定位难度 微信用户群体庞大,不同用户的需求和兴趣各异。因此,制作推广链接时需要精准定位目标受众,以便更有效地吸引他们点击并分享链接

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get

2、PF-Net点云补全

2、PF-Net 点云补全 PF-Net论文链接:PF-Net PF-Net (Point Fractal Network for 3D Point Cloud Completion)是一种专门为三维点云补全设计的深度学习模型。点云补全实际上和图片补全是一个逻辑,都是采用GAN模型的思想来进行补全,在图片补全中,将部分像素点删除并且标记,然后卷积特征提取预测、判别器判别,来训练模型,生成的像

SWAP作物生长模型安装教程、数据制备、敏感性分析、气候变化影响、R模型敏感性分析与贝叶斯优化、Fortran源代码分析、气候数据降尺度与变化影响分析

查看原文>>>全流程SWAP农业模型数据制备、敏感性分析及气候变化影响实践技术应用 SWAP模型是由荷兰瓦赫宁根大学开发的先进农作物模型,它综合考虑了土壤-水分-大气以及植被间的相互作用;是一种描述作物生长过程的一种机理性作物生长模型。它不但运用Richard方程,使其能够精确的模拟土壤中水分的运动,而且耦合了WOFOST作物模型使作物的生长描述更为科学。 本文让更多的科研人员和农业工作者

Spring MVC 图片上传

引入需要的包 <dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

uniapp设置微信小程序的交互反馈

链接:uni.showToast(OBJECT) | uni-app官网 (dcloud.net.cn) 设置操作成功的弹窗: title是我们弹窗提示的文字 showToast是我们在加载的时候进入就会弹出的提示。 2.设置失败的提示窗口和标签 icon:'error'是设置我们失败的logo 设置的文字上限是7个文字,如果需要设置的提示文字过长就需要设置icon并给

MVC(Model-View-Controller)和MVVM(Model-View-ViewModel)

1、MVC MVC(Model-View-Controller) 是一种常用的架构模式,用于分离应用程序的逻辑、数据和展示。它通过三个核心组件(模型、视图和控制器)将应用程序的业务逻辑与用户界面隔离,促进代码的可维护性、可扩展性和模块化。在 MVC 模式中,各组件可以与多种设计模式结合使用,以增强灵活性和可维护性。以下是 MVC 各组件与常见设计模式的关系和作用: 1. Model(模型)

GitHub每周最火火火项目(9.2-9.8)

项目名称:polarsource / polar 项目介绍:polar 是一个开源项目,它是 Lemon Squeezy 的替代方案,并且具有更具优势的价格。该项目的目标是为开发者提供一种更好的选择,让他们能够在追求自己的热情和兴趣的同时,通过编码获得相应的报酬。通过使用 polar,开发者可以享受到更实惠的价格,同时也能够更自由地发挥自己的创造力和技能。 项目地址:https://github.