{"id":19592699,"url":"https://github.com/nogic1008/t4sample","last_synced_at":"2025-07-14T06:35:22.455Z","repository":{"id":53696622,"uuid":"200508701","full_name":"nogic1008/T4Sample","owner":"nogic1008","description":"How to Use T4 in .NET Core + VSCode","archived":false,"fork":false,"pushed_at":"2021-03-19T02:39:44.000Z","size":22,"stargazers_count":38,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-27T14:39:21.716Z","etag":null,"topics":["code-generation","csharp","dotnet","dotnet-core","t4"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nogic1008.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-04T15:21:16.000Z","updated_at":"2024-12-16T16:02:37.000Z","dependencies_parsed_at":"2022-09-09T19:35:19.892Z","dependency_job_id":null,"html_url":"https://github.com/nogic1008/T4Sample","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/nogic1008/T4Sample","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nogic1008%2FT4Sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nogic1008%2FT4Sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nogic1008%2FT4Sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nogic1008%2FT4Sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nogic1008","download_url":"https://codeload.github.com/nogic1008/T4Sample/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nogic1008%2FT4Sample/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265253388,"owners_count":23735093,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-generation","csharp","dotnet","dotnet-core","t4"],"created_at":"2024-11-11T08:36:42.697Z","updated_at":"2025-07-14T06:35:22.420Z","avatar_url":"https://github.com/nogic1008.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# T4Sample (How to Use T4 in .NET Core + VSCode)\n\n[![License](https://img.shields.io/github/license/nogic1008/T4Sample)](LICENSE)\n\nThis is a sample to create a table Entity class using T4.\n\nIn Japanese, see also [.NET Core+VS CodeでもT4 テンプレートエンジンでコード生成したい！](https://qiita.com/nogic1008/items/2c4049d43a11e83df15b)\n\n## Environments\n\n- .NET Core 3.1 SDK\n  - For use [\"default implementation of interfaces\"](https://docs.microsoft.com/dotnet/csharp/tutorials/default-interface-members-versions).\n\n- Visual Studio Code\n  - Required: [C# Extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp)\n  - Recommended: [T4 Support](https://marketplace.visualstudio.com/items?itemName=zbecknell.t4-support) to highlight T4 Documents.\n\n## What is \"T4\"?\n\nT4 means \"Text Template Transformation Toolkit\".\nIt is bundled to Visual Studio since 2008.\n\nIt is often used for automatic code generation such as table definition classes.\n\nThis sample uses [Mono.TextTemplating](https://github.com/mono/t4) which is a T4 implemented by Mono Project.\n\n## Processes\n\n### 1. Install .NET local tool\n\n```console\n# You should create manifest file before install local tool.\n\u003e dotnet new tool-manifest\n# Install T4 tool in local\n\u003e dotnet tool install dotnet-t4\n```\n\n```json\n{\n  \"version\": 1,\n  \"isRoot\": true,\n  \"tools\": {\n    \"dotnet-t4\": {\n      \"version\": \"2.2.1\",\n      \"commands\": [\n        \"t4\"\n      ]\n    }\n  }\n}\n```\n\n**Note: You should call `dotnet tool restore` command after cloned repo.**\n\n### 2. Create Project\n\n```console\n\u003e dotnet new console -n T4Sample -o ./src\n```\n\n### 3. Create Table and Column Entities\n\n- [ColumnInfo.cs](./src/ColumnInfo.cs)\n- [TableInfo.cs](./src/TableInfo.cs)\n\n### 4. Install Mono.TextTemplating from NuGet\n\nThe program code generated by T4 file depends on this package.\n\n```console\n\u003e dotnet add package Mono.TextTemplating --version 2.2.1\n```\n\n### 5. Add Task to Prebuild and Cleanup\n\n#### Define Target Files\n\nDefine `TextTemplate` and `Genarated` using Glob.\n\n```diff\n  \u003cItemGroup\u003e\n     \u003cPackageReference Include=\"Mono.TextTemplating\" Version=\"2.2.1\" /\u003e\n+    \u003cTextTemplate Include=\"**\\*.tt\" /\u003e\n+    \u003cGenerated Include=\"**\\*.Generated.cs\" /\u003e\n   \u003c/ItemGroup\u003e\n```\n\n#### Define Prebuild Task\n\nDefine Task to run `dotnet t4`.\n\n- `%(TextTemplate.Identity)`\n  - T4 File Paths such as *foo/Template.tt*.\n- `$(RootNameSpace)`\n  - Default Namespace.\n  If you changed the T4 class's namespace, set it exactly.\n- `%(TextTemplate.Filename)`\n  - T4 File Name.\n\n```diff\n   \u003c/ItemGroup\u003e\n+  \u003cTarget Name=\"TextTemplateTransform\" BeforeTargets=\"BeforeBuild\"\u003e\n+    \u003cExec WorkingDirectory=\"$(ProjectDir)\" Command=\"dotnet t4 %(TextTemplate.Identity) -c $(RootNameSpace).%(TextTemplate.Filename) -o %(TextTemplate.Filename).Generated.cs\" /\u003e\n+  \u003c/Target\u003e\n```\n\n#### Define Cleanup Task\n\nDefine Task to delete `*.Generated.cs`.\n\n```diff\n   \u003c/Target\u003e\n+  \u003cTarget Name=\"TextTemplateClean\" AfterTargets=\"Clean\"\u003e\n+    \u003cDelete Files=\"@(Generated)\" /\u003e\n+  \u003c/Target\u003e\n```\n\n### 6. Create T4 Interface and define implements\n\n`TransFormText()`, Called by Program to use T4 Classes are defined in *\\*.Generated.cs*.\nBut \\*.Generated.cs is generated just before the build.\nSo calling `TransFormText()` will cause a compile error because there is no definition at before build time.\n\nThis sample avoids this by using the \"default implementation of interfaces\".\n\n```csharp\nusing System;\n\nnamespace T4Sample\n{\n    public interface ITextTemplate\n    {\n        string TransformText() =\u003e throw new NotImplementedException();\n    }\n}\n```\n\n### 7. Create T4 Template File \u0026 Class\n\nNote that T4 class should be defined with `partial` class.\n\n- [TableEntityTemplate.tt](./src/TableEntityTemplate.tt)\n- [TableEntityTemplate.cs](./src/TableEntityTemplate.cs)\n\n### 8. Call T4 Class in Program.cs\n\nNote that variable type should be an interface. **Don't** use `var` or `TableEntityTemplate`.\n\n- [Program.cs](./src/Program.cs)\n\n## Build \u0026 Run\n\n### Build\n\n```console\n\u003e dotnet build\n```\n\n`TableEntityTemplate.Generated.cs` is generated and the program is built including it.\n\n### Run\n\n`dotnet run` does not cause Beforebuild-Event, you should execute `dotnet build` first.\n\n```console\n\u003e cd src\n\u003e dotnet run\n```\n\n### Result\n\n```csharp\nusing System;\nusing System.Collections.Generic;\n\nnamespace MyNameSpace\n{\n    /// \u003csummery\u003e\n    /// Stores employee information.\n    /// \u003c/summery\u003e\n    public class StaffMaster\n    {\n        public int StaffId { get; }\n\n        public string StaffName { get; set; }\n\n        /// \u003csummery\u003e\n        /// Home Address.\n        /// \u003c/summery\u003e\n        public string Address { get; set; }\n\n        public DateTime CreatedDate { get; set; }\n\n        public StaffMaster(\n            int staffId\n        )\n        {\n            StaffId = staffId;\n        }\n\n        public StaffMaster(\n            int staffId,\n            string staffName,\n            string address,\n            DateTime createdDate\n        )\n        {\n            StaffId = staffId;\n            StaffName = staffName;\n            Address = address;\n            CreatedDate = createdDate;\n        }\n    }\n}\n```\n\n## References\n\n- [T4 Templates at Build Time with Dotnet Core](https://notquitepure.info/2018/12/12/T4-Templates-at-Build-Time-With-Dotnet-Core/)\n- [Mono/T4](https://github.com/mono/t4)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnogic1008%2Ft4sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnogic1008%2Ft4sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnogic1008%2Ft4sample/lists"}