代码生成器或T4模板,他们真的很邪恶? [英] Code Generators or T4 Templates, are they really evil?

查看:354
本文介绍了代码生成器或T4模板,他们真的很邪恶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说有人说不应该使用代码生成器和T4模板。这背后的逻辑是,如果您使用生成器生成代码,那么通过泛型和模板生成代码更好。

虽然我稍微同意上面的这个说法,但我还没有真正找到有效的方法来构建模板,例如可以实例化自己。换句话说,我永远无法做到:

  return new T(); 

另外,如果我想根据数据库值生成代码,我发现使用 Microsoft.SqlServer.Management.SMO 结合T4模板在生成大量代码方面非常出色,无需复制/粘贴或使用resharper。



我在泛型中遇到的很多问题是,令我震惊的是有很多开发人员不理解他们。当我研究泛型的解决方案时,有时会变得复杂,因为C#指出你不能做一些看起来合乎逻辑的事情。



什么是你的想法?你更喜欢建造一个发电机,还是更喜欢使用泛型?另外,泛型能走多远?我知道关于泛型的一个体面的数量,但有一些陷阱和陷阱,我经常遇到这个问题,导致我采用T4模板。



什么是更正确的方法处理需要大量灵活性的场景?噢,作为这个问题的一个奖励,C#和泛型上的优秀资源是什么?

解决方案

;如果你这样做的话

  public class Meh< T> 
其中T:new()
{
public static T CreateOne()
{
return new T();
}
}

至于代码生成器。我每天使用一个没有任何问题。我现在正在使用一个: - )



泛型解决了一个问题,代码生成器解决了另一个问题。例如,使用UML编辑器创建业务模型,然后使用持久性代码生成类,因为我一直使用这个工具不能用泛型实现,因为每个持久化类是完全不同的。



至于泛型的一个很好的来源。当然,最好的必须是 Jon Skeet的书! : - )

I have heard people state that Code Generators and T4 templates should not be used. The logic behind that is that if you are generating code with a generator then there is a better more efficient way to build the code through generics and templating.

While I slightly agree with this statement above, I have not really found effective ways to build templates that can say for instance instantiate themselves. In otherwords I can never do :

return new T();

Additionally, if I want to generate code based on database values I have found that using Microsoft.SqlServer.Management.SMO in conjunction with T4 templates have been wonderful at generating mass amounts of code without having to copy / paste or use resharper.

Many of the problems I have found with Generics too is that to my shock there are a lot of developers who do not understand them. When I do examine generics for a solution, there are times where it gets complicated because C# states that you cannot do something that may seem logical in my mind.

What are your thoughts? Do you prefer to build a generator, or do you prefer to use generics? Also, how far can generics go? I know a decent amount about generics, but there are traps and pitfalls that I always run into that cause me to resort to a T4 template.

What is the more proper way to handle scenarios where you need a large amount of flexibility? Oh and as a bonus to this question, what are good resources on C# and Generics?

解决方案

You can do new T(); if you do this

public class Meh<T>
  where T : new()
{
  public static T CreateOne()
  {
    return new T();
  }
}

As for code-generators. I use one every day without any problems. I'm using one right now in fact :-)

Generics solve one problem, code-generators solve another. For example, creating a business model using a UML editor and then generating your classes with persistence code as I do all of the time using this tool couldn't be achieved with generics, because each persistent class is completely different.

As for a good source on generics. The best has got to be Jon Skeet's book of course! :-)

这篇关于代码生成器或T4模板,他们真的很邪恶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆