@ngxtm/devkit 3.11.0 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/SKILLS_INDEX.md +1 -1
- package/cli/init.js +1 -1
- package/cli/update.js +1 -1
- package/merged-commands/learn.md +84 -413
- package/merged-commands/skill/sync.md +108 -0
- package/package.json +3 -2
- package/rules-index.json +8 -1
- package/scripts/merge-commands.js +0 -1
- package/skills-index.json +1 -1
- package/templates/base/rules/auto-skill.md +3 -2
- package/templates/base/rules/command-routing.md +71 -0
- package/templates/dotnet/rules/dotnet/aspnet-core/SKILL.md +92 -0
- package/templates/dotnet/rules/dotnet/aspnet-core/references/REFERENCE.md +335 -0
- package/templates/dotnet/rules/dotnet/best-practices/SKILL.md +101 -0
- package/templates/dotnet/rules/dotnet/best-practices/references/REFERENCE.md +256 -0
- package/templates/dotnet/rules/dotnet/blazor/SKILL.md +146 -0
- package/templates/dotnet/rules/dotnet/blazor/references/REFERENCE.md +392 -0
- package/templates/dotnet/rules/dotnet/language/SKILL.md +82 -0
- package/templates/dotnet/rules/dotnet/language/references/REFERENCE.md +222 -0
- package/templates/dotnet/rules/dotnet/patterns.rule.md +388 -0
- package/templates/dotnet/rules/dotnet/razor-pages/SKILL.md +124 -0
- package/templates/dotnet/rules/dotnet/razor-pages/references/REFERENCE.md +321 -0
- package/templates/dotnet/rules/dotnet/security/SKILL.md +89 -0
- package/templates/dotnet/rules/dotnet/security/references/REFERENCE.md +295 -0
- package/templates/dotnet/rules/dotnet/tooling/SKILL.md +92 -0
- package/templates/dotnet/rules/dotnet/tooling/references/REFERENCE.md +300 -0
- package/merged-commands/scout/ext.md +0 -35
- package/merged-commands/scout.md +0 -28
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: C# Best Practices
|
|
3
|
+
description: Idiomatic C# patterns for clean, maintainable code.
|
|
4
|
+
metadata:
|
|
5
|
+
labels: [csharp, conventions, naming, structure, di]
|
|
6
|
+
triggers:
|
|
7
|
+
files: ['**/*.cs']
|
|
8
|
+
keywords: [class, namespace, using, public, private, internal]
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# C# Best Practices
|
|
12
|
+
|
|
13
|
+
## **Priority: P1 (OPERATIONAL)**
|
|
14
|
+
|
|
15
|
+
Idiomatic patterns for clean, maintainable C# code.
|
|
16
|
+
|
|
17
|
+
## Implementation Guidelines
|
|
18
|
+
|
|
19
|
+
- **Naming Conventions**:
|
|
20
|
+
- `PascalCase`: Types, methods, properties, events, namespaces
|
|
21
|
+
- `camelCase`: Parameters, local variables
|
|
22
|
+
- `_camelCase`: Private fields
|
|
23
|
+
- `IPascalCase`: Interfaces (prefix with `I`)
|
|
24
|
+
- `TPascalCase`: Type parameters (prefix with `T`)
|
|
25
|
+
- **Project Structure**: Clean/Onion architecture. Feature folders over layer folders.
|
|
26
|
+
- **Dependency Injection**: Constructor injection only. Register in `IServiceCollection`.
|
|
27
|
+
- **Logging**: `ILogger<T>` with structured logging. Use appropriate log levels.
|
|
28
|
+
- **Configuration**: `IOptions<T>` pattern with validation. Never hardcode settings.
|
|
29
|
+
- **File Organization**: One type per file. Use `global using` for common namespaces.
|
|
30
|
+
- **Immutability**: Prefer `readonly`, `init`, `record` for data integrity.
|
|
31
|
+
- **Expression-bodied Members**: Use for simple single-line methods/properties.
|
|
32
|
+
|
|
33
|
+
## Anti-Patterns
|
|
34
|
+
|
|
35
|
+
- **No static services**: Use DI instead of `static class ServiceHelper`.
|
|
36
|
+
- **No service locator**: Avoid `IServiceProvider.GetService()` in business logic.
|
|
37
|
+
- **No `new()` for dependencies**: Inject via constructor.
|
|
38
|
+
- **No magic strings**: Use `nameof()`, constants, or configuration.
|
|
39
|
+
- **No God classes**: Split large classes by responsibility.
|
|
40
|
+
- **No regions**: Use partial classes or extract types instead.
|
|
41
|
+
|
|
42
|
+
## Code
|
|
43
|
+
|
|
44
|
+
```csharp
|
|
45
|
+
// Proper DI registration
|
|
46
|
+
public static class ServiceCollectionExtensions
|
|
47
|
+
{
|
|
48
|
+
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
|
|
49
|
+
{
|
|
50
|
+
services.AddScoped<IUserService, UserService>();
|
|
51
|
+
services.AddScoped<IOrderService, OrderService>();
|
|
52
|
+
|
|
53
|
+
services.AddOptions<EmailSettings>()
|
|
54
|
+
.BindConfiguration("Email")
|
|
55
|
+
.ValidateDataAnnotations()
|
|
56
|
+
.ValidateOnStart();
|
|
57
|
+
|
|
58
|
+
return services;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Structured logging with semantic names
|
|
63
|
+
public class OrderService(IOrderRepository repo, ILogger<OrderService> logger)
|
|
64
|
+
{
|
|
65
|
+
public async Task<Order?> GetOrderAsync(int orderId, CancellationToken ct)
|
|
66
|
+
{
|
|
67
|
+
logger.LogDebug("Fetching order {OrderId}", orderId);
|
|
68
|
+
|
|
69
|
+
var order = await repo.GetByIdAsync(orderId, ct);
|
|
70
|
+
|
|
71
|
+
if (order is null)
|
|
72
|
+
logger.LogWarning("Order {OrderId} not found", orderId);
|
|
73
|
+
|
|
74
|
+
return order;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Options pattern with validation
|
|
79
|
+
public class EmailSettings
|
|
80
|
+
{
|
|
81
|
+
public const string SectionName = "Email";
|
|
82
|
+
|
|
83
|
+
[Required]
|
|
84
|
+
public string SmtpHost { get; init; } = string.Empty;
|
|
85
|
+
|
|
86
|
+
[Range(1, 65535)]
|
|
87
|
+
public int SmtpPort { get; init; } = 587;
|
|
88
|
+
|
|
89
|
+
[Required, EmailAddress]
|
|
90
|
+
public string FromAddress { get; init; } = string.Empty;
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Reference & Examples
|
|
95
|
+
|
|
96
|
+
For project structure templates and DI patterns:
|
|
97
|
+
See [references/REFERENCE.md](references/REFERENCE.md).
|
|
98
|
+
|
|
99
|
+
## Related Topics
|
|
100
|
+
|
|
101
|
+
language | security | tooling
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# C# Best Practices Reference
|
|
2
|
+
|
|
3
|
+
Project structure, dependency injection, and configuration patterns.
|
|
4
|
+
|
|
5
|
+
## References
|
|
6
|
+
|
|
7
|
+
- [**Project Structure**](project-structure.md) - Clean Architecture layout.
|
|
8
|
+
- [**DI Lifetimes**](di-lifetimes.md) - Transient, Scoped, Singleton comparison.
|
|
9
|
+
- [**Logging**](logging.md) - Structured logging best practices.
|
|
10
|
+
|
|
11
|
+
## Project Structure (Clean Architecture)
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
src/
|
|
15
|
+
├── Domain/ # Core business logic (no dependencies)
|
|
16
|
+
│ ├── Entities/
|
|
17
|
+
│ │ ├── User.cs
|
|
18
|
+
│ │ └── Order.cs
|
|
19
|
+
│ ├── ValueObjects/
|
|
20
|
+
│ │ └── Money.cs
|
|
21
|
+
│ ├── Enums/
|
|
22
|
+
│ │ └── OrderStatus.cs
|
|
23
|
+
│ └── Interfaces/
|
|
24
|
+
│ ├── IUserRepository.cs
|
|
25
|
+
│ └── IOrderRepository.cs
|
|
26
|
+
│
|
|
27
|
+
├── Application/ # Use cases (depends on Domain)
|
|
28
|
+
│ ├── Common/
|
|
29
|
+
│ │ ├── Interfaces/
|
|
30
|
+
│ │ │ └── IEmailService.cs
|
|
31
|
+
│ │ └── Behaviors/
|
|
32
|
+
│ │ └── ValidationBehavior.cs
|
|
33
|
+
│ ├── Users/
|
|
34
|
+
│ │ ├── Commands/
|
|
35
|
+
│ │ │ └── CreateUserCommand.cs
|
|
36
|
+
│ │ └── Queries/
|
|
37
|
+
│ │ └── GetUserQuery.cs
|
|
38
|
+
│ └── DependencyInjection.cs
|
|
39
|
+
│
|
|
40
|
+
├── Infrastructure/ # External concerns (DB, Email, etc.)
|
|
41
|
+
│ ├── Data/
|
|
42
|
+
│ │ ├── AppDbContext.cs
|
|
43
|
+
│ │ └── Repositories/
|
|
44
|
+
│ │ └── UserRepository.cs
|
|
45
|
+
│ ├── Services/
|
|
46
|
+
│ │ └── EmailService.cs
|
|
47
|
+
│ └── DependencyInjection.cs
|
|
48
|
+
│
|
|
49
|
+
└── WebApi/ # Presentation layer
|
|
50
|
+
├── Controllers/
|
|
51
|
+
│ └── UsersController.cs
|
|
52
|
+
├── Middleware/
|
|
53
|
+
│ └── ExceptionMiddleware.cs
|
|
54
|
+
└── Program.cs
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## DI Lifetime Comparison
|
|
58
|
+
|
|
59
|
+
| Lifetime | Created | Disposed | Use Case |
|
|
60
|
+
|----------|---------|----------|----------|
|
|
61
|
+
| **Transient** | Every request | After use | Lightweight, stateless services |
|
|
62
|
+
| **Scoped** | Once per HTTP request | End of request | DbContext, UnitOfWork |
|
|
63
|
+
| **Singleton** | Once per app | App shutdown | Caches, HttpClientFactory |
|
|
64
|
+
|
|
65
|
+
```csharp
|
|
66
|
+
// Registration examples
|
|
67
|
+
services.AddTransient<IEmailSender, EmailSender>(); // New instance each time
|
|
68
|
+
services.AddScoped<IUserRepository, UserRepository>(); // Per HTTP request
|
|
69
|
+
services.AddSingleton<ICacheService, MemoryCacheService>(); // App lifetime
|
|
70
|
+
|
|
71
|
+
// Common mistake: Scoped in Singleton (captive dependency)
|
|
72
|
+
// ❌ DON'T: Singleton depending on Scoped
|
|
73
|
+
public class SingletonService(IScopedService scoped) { } // Runtime error!
|
|
74
|
+
|
|
75
|
+
// ✅ DO: Use IServiceScopeFactory for scoped in singleton
|
|
76
|
+
public class SingletonService(IServiceScopeFactory scopeFactory)
|
|
77
|
+
{
|
|
78
|
+
public async Task DoWork()
|
|
79
|
+
{
|
|
80
|
+
using var scope = scopeFactory.CreateScope();
|
|
81
|
+
var scoped = scope.ServiceProvider.GetRequiredService<IScopedService>();
|
|
82
|
+
await scoped.ProcessAsync();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Logging Best Practices
|
|
88
|
+
|
|
89
|
+
```csharp
|
|
90
|
+
// Log levels usage
|
|
91
|
+
public class OrderProcessor(ILogger<OrderProcessor> logger)
|
|
92
|
+
{
|
|
93
|
+
public async Task ProcessAsync(Order order)
|
|
94
|
+
{
|
|
95
|
+
// Trace: Very detailed, only for debugging specific issues
|
|
96
|
+
logger.LogTrace("Processing order with data: {@Order}", order);
|
|
97
|
+
|
|
98
|
+
// Debug: Development diagnostics
|
|
99
|
+
logger.LogDebug("Starting order processing for {OrderId}", order.Id);
|
|
100
|
+
|
|
101
|
+
// Information: Normal operation milestones
|
|
102
|
+
logger.LogInformation("Order {OrderId} processed successfully", order.Id);
|
|
103
|
+
|
|
104
|
+
// Warning: Unexpected but handled situations
|
|
105
|
+
if (order.Items.Count == 0)
|
|
106
|
+
logger.LogWarning("Order {OrderId} has no items", order.Id);
|
|
107
|
+
|
|
108
|
+
// Error: Failures that need attention
|
|
109
|
+
try { await SendEmailAsync(order); }
|
|
110
|
+
catch (Exception ex)
|
|
111
|
+
{
|
|
112
|
+
logger.LogError(ex, "Failed to send email for order {OrderId}", order.Id);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Critical: App-breaking failures
|
|
116
|
+
// logger.LogCritical("Database connection lost");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// High-performance logging with source generators
|
|
121
|
+
public static partial class LogMessages
|
|
122
|
+
{
|
|
123
|
+
[LoggerMessage(Level = LogLevel.Information, Message = "Order {OrderId} created by {UserId}")]
|
|
124
|
+
public static partial void OrderCreated(this ILogger logger, int orderId, int userId);
|
|
125
|
+
|
|
126
|
+
[LoggerMessage(Level = LogLevel.Error, Message = "Payment failed for order {OrderId}")]
|
|
127
|
+
public static partial void PaymentFailed(this ILogger logger, int orderId, Exception ex);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Usage
|
|
131
|
+
logger.OrderCreated(order.Id, user.Id);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Configuration Patterns
|
|
135
|
+
|
|
136
|
+
```csharp
|
|
137
|
+
// appsettings.json structure
|
|
138
|
+
{
|
|
139
|
+
"Database": {
|
|
140
|
+
"ConnectionString": "...",
|
|
141
|
+
"CommandTimeout": 30
|
|
142
|
+
},
|
|
143
|
+
"Email": {
|
|
144
|
+
"SmtpHost": "smtp.example.com",
|
|
145
|
+
"SmtpPort": 587,
|
|
146
|
+
"FromAddress": "noreply@example.com"
|
|
147
|
+
},
|
|
148
|
+
"Features": {
|
|
149
|
+
"EnableNewDashboard": true
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Strongly-typed options
|
|
154
|
+
public class DatabaseSettings
|
|
155
|
+
{
|
|
156
|
+
public const string SectionName = "Database";
|
|
157
|
+
|
|
158
|
+
[Required]
|
|
159
|
+
public string ConnectionString { get; init; } = string.Empty;
|
|
160
|
+
|
|
161
|
+
[Range(1, 300)]
|
|
162
|
+
public int CommandTimeout { get; init; } = 30;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Registration with validation
|
|
166
|
+
builder.Services.AddOptions<DatabaseSettings>()
|
|
167
|
+
.BindConfiguration(DatabaseSettings.SectionName)
|
|
168
|
+
.ValidateDataAnnotations()
|
|
169
|
+
.ValidateOnStart(); // Fail fast at startup
|
|
170
|
+
|
|
171
|
+
// Usage patterns
|
|
172
|
+
public class DataService(IOptions<DatabaseSettings> options)
|
|
173
|
+
{
|
|
174
|
+
// IOptions<T> - Singleton, read once at startup
|
|
175
|
+
private readonly DatabaseSettings _settings = options.Value;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public class FeatureService(IOptionsSnapshot<DatabaseSettings> options)
|
|
179
|
+
{
|
|
180
|
+
// IOptionsSnapshot<T> - Scoped, re-reads on each request
|
|
181
|
+
public void DoWork() => Console.WriteLine(options.Value.CommandTimeout);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
public class BackgroundService(IOptionsMonitor<DatabaseSettings> options)
|
|
185
|
+
{
|
|
186
|
+
// IOptionsMonitor<T> - Singleton with change notifications
|
|
187
|
+
public BackgroundService()
|
|
188
|
+
{
|
|
189
|
+
options.OnChange(settings => Console.WriteLine("Config changed!"));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Extension Method Patterns
|
|
195
|
+
|
|
196
|
+
```csharp
|
|
197
|
+
// Service registration extensions
|
|
198
|
+
public static class ServiceCollectionExtensions
|
|
199
|
+
{
|
|
200
|
+
public static IServiceCollection AddInfrastructure(
|
|
201
|
+
this IServiceCollection services,
|
|
202
|
+
IConfiguration configuration)
|
|
203
|
+
{
|
|
204
|
+
// Database
|
|
205
|
+
services.AddDbContext<AppDbContext>(options =>
|
|
206
|
+
options.UseSqlServer(configuration.GetConnectionString("Default")));
|
|
207
|
+
|
|
208
|
+
// Repositories
|
|
209
|
+
services.AddScoped<IUserRepository, UserRepository>();
|
|
210
|
+
services.AddScoped<IOrderRepository, OrderRepository>();
|
|
211
|
+
|
|
212
|
+
// External services
|
|
213
|
+
services.AddHttpClient<IPaymentClient, PaymentClient>(client =>
|
|
214
|
+
{
|
|
215
|
+
client.BaseAddress = new Uri(configuration["Payment:BaseUrl"]!);
|
|
216
|
+
client.Timeout = TimeSpan.FromSeconds(30);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
return services;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Usage in Program.cs
|
|
224
|
+
builder.Services
|
|
225
|
+
.AddInfrastructure(builder.Configuration)
|
|
226
|
+
.AddApplication();
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Naming Convention Examples
|
|
230
|
+
|
|
231
|
+
```csharp
|
|
232
|
+
// ✅ Correct naming
|
|
233
|
+
public interface IUserRepository { } // Interface: IPascalCase
|
|
234
|
+
public class UserRepository : IUserRepository // Class: PascalCase
|
|
235
|
+
{
|
|
236
|
+
private readonly DbContext _context; // Private field: _camelCase
|
|
237
|
+
private readonly ILogger<UserRepository> _logger;
|
|
238
|
+
|
|
239
|
+
public string ConnectionString { get; } // Property: PascalCase
|
|
240
|
+
public event EventHandler? UserCreated; // Event: PascalCase
|
|
241
|
+
|
|
242
|
+
public async Task<User?> GetByIdAsync( // Method: PascalCase
|
|
243
|
+
int userId, // Parameter: camelCase
|
|
244
|
+
CancellationToken cancellationToken)
|
|
245
|
+
{
|
|
246
|
+
var user = await _context.Users // Local: camelCase
|
|
247
|
+
.FindAsync(userId, cancellationToken);
|
|
248
|
+
return user;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
public class Repository<TEntity> // Type parameter: TPascalCase
|
|
253
|
+
where TEntity : class { }
|
|
254
|
+
|
|
255
|
+
public const string DefaultRole = "User"; // Constant: PascalCase
|
|
256
|
+
```
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Blazor
|
|
3
|
+
description: Blazor component patterns for interactive web UIs.
|
|
4
|
+
metadata:
|
|
5
|
+
labels: [blazor, components, wasm, server]
|
|
6
|
+
triggers:
|
|
7
|
+
files: ['**/*.razor', '**/*.razor.cs']
|
|
8
|
+
keywords: [Component, Parameter, CascadingParameter, EventCallback, StateHasChanged]
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# Blazor
|
|
12
|
+
|
|
13
|
+
## **Priority: P1 (OPERATIONAL)**
|
|
14
|
+
|
|
15
|
+
Blazor component patterns for interactive web UIs.
|
|
16
|
+
|
|
17
|
+
## Implementation Guidelines
|
|
18
|
+
|
|
19
|
+
- **Components**: Keep components focused. Split large components into smaller ones.
|
|
20
|
+
- **Parameters**: Use `[Parameter]` for one-way, `EventCallback` for two-way binding.
|
|
21
|
+
- **Cascading Values**: Use for app-wide state (theme, user context).
|
|
22
|
+
- **State Management**: Component state for local, Fluxor/custom service for global.
|
|
23
|
+
- **Forms**: `EditForm` with `DataAnnotationsValidator` or FluentValidation.
|
|
24
|
+
- **JS Interop**: Use `IJSRuntime` sparingly. Prefer Blazor bindings.
|
|
25
|
+
- **Render Modes**: Choose based on requirements (Server, WASM, Auto in .NET 8+).
|
|
26
|
+
- **Streaming**: Use `[StreamRendering]` for long-loading components.
|
|
27
|
+
|
|
28
|
+
## Anti-Patterns
|
|
29
|
+
|
|
30
|
+
- **No direct DOM manipulation**: Use Blazor bindings and refs.
|
|
31
|
+
- **No `StateHasChanged()` in lifecycle**: Called automatically after lifecycle methods.
|
|
32
|
+
- **No heavy computation in render**: Move to `OnInitialized` or background task.
|
|
33
|
+
- **No sync JS interop in Server mode**: Causes UI blocking.
|
|
34
|
+
|
|
35
|
+
## Code
|
|
36
|
+
|
|
37
|
+
```razor
|
|
38
|
+
@* UserList.razor *@
|
|
39
|
+
@page "/users"
|
|
40
|
+
@attribute [StreamRendering]
|
|
41
|
+
@inject IUserService UserService
|
|
42
|
+
|
|
43
|
+
<PageTitle>Users</PageTitle>
|
|
44
|
+
|
|
45
|
+
<h3>Users</h3>
|
|
46
|
+
|
|
47
|
+
@if (_users is null)
|
|
48
|
+
{
|
|
49
|
+
<p><em>Loading...</em></p>
|
|
50
|
+
}
|
|
51
|
+
else if (_users.Count == 0)
|
|
52
|
+
{
|
|
53
|
+
<p>No users found.</p>
|
|
54
|
+
}
|
|
55
|
+
else
|
|
56
|
+
{
|
|
57
|
+
<div class="user-grid">
|
|
58
|
+
@foreach (var user in _users)
|
|
59
|
+
{
|
|
60
|
+
<UserCard User="user" OnDelete="HandleDelete" />
|
|
61
|
+
}
|
|
62
|
+
</div>
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@code {
|
|
66
|
+
private List<User>? _users;
|
|
67
|
+
|
|
68
|
+
protected override async Task OnInitializedAsync()
|
|
69
|
+
{
|
|
70
|
+
_users = await UserService.GetAllAsync();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private async Task HandleDelete(int userId)
|
|
74
|
+
{
|
|
75
|
+
await UserService.DeleteAsync(userId);
|
|
76
|
+
_users = await UserService.GetAllAsync();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```razor
|
|
82
|
+
@* UserCard.razor - Reusable component *@
|
|
83
|
+
<div class="card">
|
|
84
|
+
<h5>@User.Name</h5>
|
|
85
|
+
<p>@User.Email</p>
|
|
86
|
+
<button @onclick="() => OnDelete.InvokeAsync(User.Id)">Delete</button>
|
|
87
|
+
</div>
|
|
88
|
+
|
|
89
|
+
@code {
|
|
90
|
+
[Parameter, EditorRequired]
|
|
91
|
+
public User User { get; set; } = default!;
|
|
92
|
+
|
|
93
|
+
[Parameter]
|
|
94
|
+
public EventCallback<int> OnDelete { get; set; }
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
```razor
|
|
99
|
+
@* EditForm with validation *@
|
|
100
|
+
<EditForm Model="@_model" OnValidSubmit="HandleSubmit" FormName="CreateUser">
|
|
101
|
+
<DataAnnotationsValidator />
|
|
102
|
+
<ValidationSummary class="text-danger" />
|
|
103
|
+
|
|
104
|
+
<div class="mb-3">
|
|
105
|
+
<label class="form-label">Name</label>
|
|
106
|
+
<InputText @bind-Value="_model.Name" class="form-control" />
|
|
107
|
+
<ValidationMessage For="@(() => _model.Name)" />
|
|
108
|
+
</div>
|
|
109
|
+
|
|
110
|
+
<div class="mb-3">
|
|
111
|
+
<label class="form-label">Email</label>
|
|
112
|
+
<InputText @bind-Value="_model.Email" class="form-control" type="email" />
|
|
113
|
+
<ValidationMessage For="@(() => _model.Email)" />
|
|
114
|
+
</div>
|
|
115
|
+
|
|
116
|
+
<button type="submit" class="btn btn-primary" disabled="@_isSubmitting">
|
|
117
|
+
@if (_isSubmitting)
|
|
118
|
+
{
|
|
119
|
+
<span class="spinner-border spinner-border-sm"></span>
|
|
120
|
+
}
|
|
121
|
+
Submit
|
|
122
|
+
</button>
|
|
123
|
+
</EditForm>
|
|
124
|
+
|
|
125
|
+
@code {
|
|
126
|
+
private CreateUserModel _model = new();
|
|
127
|
+
private bool _isSubmitting;
|
|
128
|
+
|
|
129
|
+
private async Task HandleSubmit()
|
|
130
|
+
{
|
|
131
|
+
_isSubmitting = true;
|
|
132
|
+
await UserService.CreateAsync(_model);
|
|
133
|
+
_isSubmitting = false;
|
|
134
|
+
NavigationManager.NavigateTo("/users");
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Reference & Examples
|
|
140
|
+
|
|
141
|
+
For lifecycle, state management, and JS interop:
|
|
142
|
+
See [references/REFERENCE.md](references/REFERENCE.md).
|
|
143
|
+
|
|
144
|
+
## Related Topics
|
|
145
|
+
|
|
146
|
+
aspnet-core | razor-pages | security
|