From e4942b2ba72af2d2f672ff885acb549258e6f1f8 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Tue, 5 Aug 2025 08:40:56 +0300 Subject: [PATCH] Update C# code samples for correct namespace usages on Blazor --- docs/en/tutorials/todo/layered/index.md | 116 +++++++++++++----------- 1 file changed, 61 insertions(+), 55 deletions(-) diff --git a/docs/en/tutorials/todo/layered/index.md b/docs/en/tutorials/todo/layered/index.md index 03c06f75c9..98d0a0286e 100644 --- a/docs/en/tutorials/todo/layered/index.md +++ b/docs/en/tutorials/todo/layered/index.md @@ -171,13 +171,13 @@ This application has a single [entity](../../../framework/architecture/domain-dr using System; using Volo.Abp.Domain.Entities; -namespace TodoApp +namespace TodoApp; + +public class TodoItem : BasicAggregateRoot { - public class TodoItem : BasicAggregateRoot - { - public string Text { get; set; } = string.Empty; - } + public string Text { get; set; } = string.Empty; } + ```` `BasicAggregateRoot` is the simplest base class to create root entities, and `Guid` is the primary key (`Id`) of the entity here. @@ -281,15 +281,15 @@ using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Application.Services; -namespace TodoApp +namespace TodoApp; + +public interface ITodoAppService : IApplicationService { - public interface ITodoAppService : IApplicationService - { - Task> GetListAsync(); - Task CreateAsync(string text); - Task DeleteAsync(Guid id); - } + Task> GetListAsync(); + Task CreateAsync(string text); + Task DeleteAsync(Guid id); } + ```` ### Data Transfer Object @@ -299,14 +299,14 @@ namespace TodoApp ````csharp using System; -namespace TodoApp +namespace TodoApp; + +public class TodoItemDto { - public class TodoItemDto - { - public Guid Id { get; set; } - public string Text { get; set; } = string.Empty; - } + public Guid Id { get; set; } + public string Text { get; set; } = string.Empty; } + ```` This is a very simple DTO class that matches our `TodoItem` entity. We are ready to implement the `ITodoAppService`. @@ -323,19 +323,18 @@ using System.Threading.Tasks; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; -namespace TodoApp +namespace TodoApp; + +public class TodoAppService : ApplicationService, ITodoAppService { - public class TodoAppService : ApplicationService, ITodoAppService - { - private readonly IRepository _todoItemRepository; + private readonly IRepository _todoItemRepository; - public TodoAppService(IRepository todoItemRepository) - { - _todoItemRepository = todoItemRepository; - } - - // TODO: Implement the methods here... + public TodoAppService(IRepository todoItemRepository) + { + _todoItemRepository = todoItemRepository; } + + // TODO: Implement the methods here... } ```` @@ -412,8 +411,8 @@ Open the `Index.cshtml.cs` file in the `Pages` folder of the *TodoApp.Web* proje using System.Collections.Generic; using System.Threading.Tasks; -namespace TodoApp.Web.Pages -{ +namespace TodoApp.Web.Pages; + public class IndexModel : TodoAppPageModel { public List TodoItems { get; set; } @@ -430,7 +429,7 @@ namespace TodoApp.Web.Pages TodoItems = await _todoAppService.GetListAsync(); } } -} + ```` This class uses the `ITodoAppService` to get the list of todo items and assign the `TodoItems` property. We will use it to render the todo items on the razor page. @@ -578,34 +577,41 @@ using Microsoft.AspNetCore.Components; using System.Collections.Generic; using System.Threading.Tasks; -namespace TodoApp.Blazor.Pages +{{if UI=="Blazor" || UI=="BlazorWebApp"}} +namespace TodoApp.Blazor.Client.Pages; +{{else if UI=="BlazorServer"}} +namespace TodoApp.Blazor.Pages; +{{else if UI=="MAUIBlazor"}} +namespace TodoApp.MauiBlazor.Pages; +{{end}} + +namespace TodoApp.Blazor.Pages; + +public partial class Index { - public partial class Index - { - [Inject] - private ITodoAppService TodoAppService { get; set; } + [Inject] + private ITodoAppService TodoAppService { get; set; } - private List TodoItems { get; set; } = new List(); - private string NewTodoText { get; set; } = string.Empty; + private List TodoItems { get; set; } = new List(); + private string NewTodoText { get; set; } = string.Empty; - protected override async Task OnInitializedAsync() - { - TodoItems = await TodoAppService.GetListAsync(); - } - - private async Task Create() - { - var result = await TodoAppService.CreateAsync(NewTodoText); - TodoItems.Add(result); - NewTodoText = null; - } + protected override async Task OnInitializedAsync() + { + TodoItems = await TodoAppService.GetListAsync(); + } + + private async Task Create() + { + var result = await TodoAppService.CreateAsync(NewTodoText); + TodoItems.Add(result); + NewTodoText = null; + } - private async Task Delete(TodoItemDto todoItem) - { - await TodoAppService.DeleteAsync(todoItem.Id); - await Notify.Info("Deleted the todo item."); - TodoItems.Remove(todoItem); - } + private async Task Delete(TodoItemDto todoItem) + { + await TodoAppService.DeleteAsync(todoItem.Id); + await Notify.Info("Deleted the todo item."); + TodoItems.Remove(todoItem); } } ```