Custom code
On the designer project property page, you can change the template for certain code such as Using, inheritance, class attributes, property attributes, method attributes, constructor injection, and code snippet.
The purpose of custom code is to provide a solution to address the incompatibility where the generated code from this extension does not perfectly align with the existing project code. Custom code allows users to modify or add code to the generated code to meet specific needs.
If you want to import the custom code settings from referenced projects, you can select the Apply referenced projects custom code settings option, so the custom code settings in the referenced projects will be automatically imported and displayed here.
For each custom code, you can select the scope where the code will take effect: Enum, Entity, Filter, Entity Configuration, DataContext, Service, Controller.
For Constructor Injection, you can also specify the dependencies.
For Code Snippet, you can also specify where to insert the code.
After you finish the configuration, you can preview the code to make sure the code is generated as expected.
For code examples, refer to the following section Add custom code.
Add custom code
This section provides examples for how to write custom codes.
Using
Using is mainly used in conjunction with other code fragments.
For example, when you need to inherit an interface for DbContext, you first need to use the Using section to add the namespace where the interface is located, and then add the interface in the inheritance section.
Add the following in Using
Custom code | Scope |
---|---|
using Microsoft.Extensions.DependencyInjection; | DataContext |
using Microsoft.EntityFrameworkCore.Infrastructure; | DataContext |
Add the following in Inheritance
Custom code | Scope |
---|---|
IServiceScope | DataContext |
Add the following in Code snippet
Code snippet | Scope | Insert position |
---|---|---|
public IServiceProvider ServiceProvider => this.GetInfrastructure(); | DataContext | Top of the class |
Code preview:
Inheritance
When all entities inherit an interface or class, support templates {TClass} and {TInterface}, you can replace with the class name or interface name of the current class when generating code according to the template type.
For example, when all entities inherit the IEquatable<> generic interface (need to provide interface implementation in combination with Code Snippet)
Add the following in Inheritance:
Custom code | Scope |
---|---|
IEquatable<{TClass}> | DataContext |
Add the following in Code Snippet:
Code snippet | Scope | Insert position |
---|---|---|
public bool Equals({TClass}? other) { return other?.Equals(this) ?? false; } |
Entity | Bottom of the class |
Code preview:
Class attributes
For example, to add the [Serializable] attribute to all enumeration types
Add the following in Class attributes:
Custom attribute code | Scope |
---|---|
[Serializable] | Enum |
Code preview:
For another example, to add the [Table] attribute on all entity types and set the mapping table name
Add the following in Class attributes:
Custom attribute code | Scope |
---|---|
[Table("{TTable}")] | Entity |
Code preview:
Property attributes
For example, to add the JSON serialized property names to the properties of each entity
Add the following in Using:
Custom code | Scope |
---|---|
using System.Text.Json.Serialization; | Entity |
Add the following in Property attributes:
Custom attribute code | Scope |
---|---|
[JsonPropertyName("{TProperty}")] | Entity |
Code preview:
Method attributes
For example, to add the response type attribute [ProducesResponseType(StatusCodes.Status200OK)] to each Action of the Controller
Add the following in Using:
Custom code | Scope |
---|---|
using Microsoft.AspNetCore.Http; | Controller |
Add the following in Method attributes:
Custom attribute code | Scope |
---|---|
[ProducesResponseType(StatusCodes.Status200OK)] | Controller |
Code preview:
Constructor Injection
For example, when integrating the log service, inject the relevant interface of the log service.
Add the following in Using:
Custom code | Scope |
---|---|
using Microsoft.Extensions.Logging; | Controller |
Add the following in Constructor Injection:
Constructor argument type | Value (Dependency) | Scope |
---|---|---|
ILogger<{TClass}> | logger | Controller |
Code preview:
Code snippet
For example, write the logs when each request is processed, and the logs need to include the UserId of the caller and the API it calls.
Add the following in Using:
Custom code | Scope |
---|---|
using Microsoft.Extensions.Logging; | Controller |
Add the following in Constructor Injection:
Constructor argument type | Value (Dependency) | Scope |
---|---|---|
ILogger<{TClass}> | logger | Controller |
ICurrentUser | currentUser | Controller |
Add the following in Code snippet:
Code snippet | Scope | Insert position |
---|---|---|
_logger.LogInformation($"UserId: {_currentUser.GetUserId()} called this method - {TMethod}"); | Controller | Top of the method |
Code preview:
More examples
Example 1: The newly generated entity automatically inherits certain interfaces and properties to implement data operations for the entity.
Specify the namespace where the relevant interfaces are located and specify the inheritance of the corresponding interfaces:
IEntity: indicates the need for interaction with the database ISoftDelete: data needs to be logically deleted
Add the IsDeleted property to each entity:
All newly generated entities will automatically inherit IEntity and ISoftDelete, and have the IsDeleted property added.
Example 2: Automatically inject services
Specify the Injection attribute:
Relevant services are automatically injected.