开发者

VsAddin or T4 for code generation (VS 2008)

I have build an Addin for code generation (C#, VS 08) for our team.

The addin creates a new menu entry if i click on a file in the solution explorer. There i can choose a destination test project where the 2 files are generated to.

For the code generation process i need informations from the selected item in the solution explorer (like Interfaces, generic types of the class declaration).

My source class looks like

public class CustomerLogic : BaseBL<T1, T2, T3>, ICustomerBL

The generated container class

public class CustomerContainer : BaseBLDummyContainer<T1, T2, T3>

The generated dummy class looks like

public class CustomerBLDummy : BaseBLDummy<T1, T2, T3, CustomerContainer>, ICustomer

How i realized it?

I created two templates (container and dummy), put placeholder to the spaces so the two template files look like

Containertemplate

public class $Classname$ : BaseBLDummyContainer<$T1$, $T2$, $T3$>

Dummytemplate

public class $Classname$ : BaseBLDummy<$T1$, $T2$, $T3$, $TContainer$>$, TInterface$

To generate the templates i have writen some code to

  • create the files
  • add them to the destination project
  • create methods if needed

You can see, a "lot" of work to do only to generate some code.

  1. Now i consider if the generation with T4 is a better solution for my situation?
  2. Does i have some benefits of the T4 mechanism ? (I've only see some T4 samples in combination with EF or database related generation)
  3. Should i be mor开发者_高级运维e flexible with T4?

Thanks a lot.


You don't need to make an either/or choice here 'k', you can mix and match add-ins and T4.

T4 has a very simple UI binding to Visual Studio out of the box, that just uses the Custom Tool mechanism (IVsSingleFileGenerator) to connect a template file to an output file in the project. (See T4Toolbox community project for more complex output).

However, T4 also exposes a service interface in VS (STextTemplating/ITextTemplating) that you can use from both VS add-ins and VS packages.

So you could keep your add-in's entry point and core way of working, but use T4 to provide the template engine and avoid having to maintain that part yourself. You'd likely use the <#@ parameter #> directive to pass replacement parameters in to your template for T1, T2, T3 etc.

Hope this gives you some ideas for options to move forward.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜