CodeInject 0.0.5
See the version list below for details.
dotnet add package CodeInject --version 0.0.5
NuGet\Install-Package CodeInject -Version 0.0.5
<PackageReference Include="CodeInject" Version="0.0.5"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="CodeInject" Version="0.0.5" />
<PackageReference Include="CodeInject"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add CodeInject --version 0.0.5
#r "nuget: CodeInject, 0.0.5"
#:package CodeInject@0.0.5
#addin nuget:?package=CodeInject&version=0.0.5
#tool nuget:?package=CodeInject&version=0.0.5
CodeInject - Code Region Source Generator
A powerful source generator that injects code regions from template files into partial classes at compile time.
✨ Features
- 📁 Template-based code injection - Extract code regions from template files
- 🔄 Placeholder replacement - Replace placeholders with custom values
- 🎯 Multi-attribute support - Apply multiple injections to a single class
- 🏗️ Nested region support - Handle nested
#regionblocks correctly - ⚡ Incremental generation - Efficient compilation with minimal rebuilds
📦 Installation
Install the package via NuGet Package Manager:
dotnet add package CodeInject
Or via Package Manager Console:
Install-Package CodeInject
🚀 Quick Start
1. Create a template file
Create a template file and add it as an AdditionalFiles item in your project:
<ItemGroup>
<AdditionalFiles Include="Templates/ApiTemplate.cs" />
</ItemGroup>
Template file content:
#region ApiMethods
public async Task<{ReturnType}> Get{EntityName}Async(int id)
{
// Implementation here
return await _repository.GetByIdAsync<{ReturnType}>(id);
}
public async Task<{ReturnType}> Create{EntityName}Async({ReturnType} entity)
{
// Implementation here
return await _repository.CreateAsync(entity);
}
#endregion
2. Apply the attribute
using CodeRegionSourceGenerator;
[RegionInject("Templates/ApiTemplate.cs", "ApiMethods",
"ReturnType", "User",
"EntityName", "User")]
public partial class UserService
{
private readonly IRepository _repository;
public UserService(IRepository repository)
{
_repository = repository;
}
// Generated methods will be injected here automatically
}
3. Generated code
The source generator will automatically create:
partial class UserService
{
public async Task<User> GetUserAsync(int id)
{
// Implementation here
return await _repository.GetByIdAsync<User>(id);
}
public async Task<User> CreateUserAsync(User entity)
{
// Implementation here
return await _repository.CreateAsync(entity);
}
}
🔧 Advanced Usage
Multiple Injections
[RegionInject("Templates/CrudTemplate.cs", "CreateMethods", "Entity", "Product")]
[RegionInject("Templates/CrudTemplate.cs", "UpdateMethods", "Entity", "Product")]
[RegionInject("Templates/ValidationTemplate.cs", "Validators", "Type", "Product")]
public partial class ProductService
{
// Multiple code regions will be injected
}
Using Placeholders Property
[RegionInject("Templates/ApiTemplate.cs", "ApiMethods",
Placeholders = new[] { "ReturnType", "Order", "EntityName", "Order" })]
public partial class OrderService
{
// Generated code with Order-specific implementations
}
⚙️ Configuration
Project Setup
Add template files to your project as AdditionalFiles:
<ItemGroup>
<AdditionalFiles Include="Templates/**/*.cs" />
<AdditionalFiles Include="CodeTemplates/**/*.txt" />
</ItemGroup>
Template File Format
- Use
#region RegionNameand#endregionto define code blocks - Support for nested regions
- Placeholders can be used in two formats:
{PlaceholderName}- with curly bracesPlaceholderName- without braces
📋 Use Cases
1. API Controller Templates
// Templates/ControllerTemplate.cs
#region CrudActions
[HttpGet]
public async Task<ActionResult<IEnumerable<{EntityType}>>> Get{EntityName}s()
{
var items = await _{entityName}Service.GetAllAsync();
return Ok(items);
}
[HttpGet("{id}")]
public async Task<ActionResult<{EntityType}>> Get{EntityName}(int id)
{
var item = await _{entityName}Service.GetByIdAsync(id);
return item == null ? NotFound() : Ok(item);
}
[HttpPost]
public async Task<ActionResult<{EntityType}>> Create{EntityName}({EntityType} {entityName})
{
var created = await _{entityName}Service.CreateAsync({entityName});
return CreatedAtAction(nameof(Get{EntityName}), new { id = created.Id }, created);
}
#endregion
2. Repository Pattern Templates
// Templates/RepositoryTemplate.cs
#region RepositoryMethods
public async Task<IEnumerable<{EntityType}>> GetAll{EntityName}sAsync()
{
return await _context.{EntityName}s.ToListAsync();
}
public async Task<{EntityType}> Get{EntityName}ByIdAsync(int id)
{
return await _context.{EntityName}s.FindAsync(id);
}
public async Task<{EntityType}> Create{EntityName}Async({EntityType} entity)
{
_context.{EntityName}s.Add(entity);
await _context.SaveChangesAsync();
return entity;
}
#endregion
🔍 Diagnostics
The source generator provides the following diagnostic information:
- CRG001: Template file not found
- CRG002: Region not found
- CRG003: File read error
💡 Best Practices
- Organize templates: Keep template files in a dedicated
Templatesfolder - Naming conventions: Use descriptive region names like
CrudMethods,ValidationRules - Placeholder naming: Use uppercase placeholder names like
ENTITY_NAME,RETURN_TYPE - Modularization: Group related functionality into different regions
📋 Requirements
- .NET Standard 2.0 or higher
- C# 7.3 or higher
- Visual Studio 2019 16.9+ or .NET 5.0+ SDK
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🆚 Comparison with Other Solutions
| Feature | CodeInject | T4 Templates | Manual Coding |
|---|---|---|---|
| Compile-time generation | ✅ | ❌ | ❌ |
| Incremental compilation | ✅ | ❌ | ✅ |
| IDE support | ✅ | ⚠️ | ✅ |
| Learning curve | Low | High | Low |
| Flexibility | High | High | Low |
📞 Support
If you encounter any issues:
- Check the FAQ
- Search existing issues
- Create a new issue
⭐ If this project helps you, please give it a star!
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.