Lost_In_Library
Lost_In_Library

Reputation: 3493

How to create .cs files automatically?

How to create .cs files automatically in Visual Studio ?

I want to create a macro / extension / template to not repeating common codes.

I want to generate them like; I want to write "Customer" in a textbox;

And this "generator" will generate CustomerRepository.cs to "Datas project". CustomerService.cs to "Services project", CustomerDto.cs to "Dtos project". So as you see I don't want to create every file one-by-one. I need a tool to generate and place those cs files to correct projects... How can I do that ?

I searched a lot and found CodeSmith Tools's Code Generator. http://www.codesmithtools.com/product/generator

But I want to buy it; it's too complex for what I need. I'm not sure that I can create this with nuget package system. Any idea/way to generating code files ?

Upvotes: 1

Views: 3487

Answers (2)

ntier ontime
ntier ontime

Reputation: 11

T4 template is definitely first choice.

I developed a whole set of T4 templates to generate a solution in the past. The risk is once you want to change code in multiple templates, or database schema changes, some code may mess up.

So I had to write a code generator based on database schema to generate a VS solution with DAL(Linq To SQL, Entity Framework), BLL, WCF, and UI Applications MVC, WPF, Windows PHone, Windows App Store, Silverlight.

You can goto https://github.com/ntierontime/Log4Net to see whether it meets your architecture requirements, or not. And send me an email with the .dbml(Linq To Sql Classes).

Upvotes: 1

Bryan Hadlock
Bryan Hadlock

Reputation: 2716

Use T4 templates.

Below is a link for a simple tutorial on how to use T4 templates in Visual Studio. It is the same for VS 2013. This is simple, but T4 templates are very powerful, and can do a ton of cool stuff.

https://www.youtube.com/watch?v=u_HYlgac5M8

Upvotes: 2

Related Questions