From 786d538da5549c5d7d12c73c010f20bf4ec82b2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 5 Sep 2020 14:44:07 +0300 Subject: [PATCH] Use AutoMapper on the Identity Blazor UI --- .../AbpIdentityBlazorAutoMapperProfile.cs | 13 +++++++++ .../AbpIdentityBlazorModule.cs | 16 +++++++++-- .../Pages/Identity/RoleManagement.razor.cs | 28 ++++++++----------- .../Volo.Abp.Identity.Blazor.csproj | 6 +++- .../Shared/LoginDisplay.razor | 3 +- 5 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorAutoMapperProfile.cs diff --git a/modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorAutoMapperProfile.cs b/modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorAutoMapperProfile.cs new file mode 100644 index 0000000000..f4f111df41 --- /dev/null +++ b/modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorAutoMapperProfile.cs @@ -0,0 +1,13 @@ +using AutoMapper; + +namespace Volo.Abp.Identity.Blazor +{ + public class AbpIdentityBlazorAutoMapperProfile : Profile + { + public AbpIdentityBlazorAutoMapperProfile() + { + CreateMap() + .MapExtraProperties(); + } + } +} diff --git a/modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorModule.cs b/modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorModule.cs index 4b5cabda8c..0e8477d75c 100644 --- a/modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorModule.cs +++ b/modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorModule.cs @@ -1,13 +1,25 @@ -using Volo.Abp.Modularity; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.AutoMapper; +using Volo.Abp.Modularity; using Volo.Abp.BlazoriseUI; namespace Volo.Abp.Identity.Blazor { [DependsOn( typeof(AbpIdentityHttpApiClientModule), - typeof(AbpBlazoriseUIModule) + typeof(AbpBlazoriseUIModule), + typeof(AbpAutoMapperModule) )] public class AbpIdentityBlazorModule : AbpModule { + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddAutoMapperObjectMapper(); + + Configure(options => + { + options.AddProfile(validate: true); + }); + } } } diff --git a/modules/identity/src/Volo.Abp.Identity.Blazor/Pages/Identity/RoleManagement.razor.cs b/modules/identity/src/Volo.Abp.Identity.Blazor/Pages/Identity/RoleManagement.razor.cs index cd3c8db609..9b86d68fdf 100644 --- a/modules/identity/src/Volo.Abp.Identity.Blazor/Pages/Identity/RoleManagement.razor.cs +++ b/modules/identity/src/Volo.Abp.Identity.Blazor/Pages/Identity/RoleManagement.razor.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Components; using Volo.Abp.ObjectExtending; using Volo.Abp.Application.Dtos; using Volo.Abp.AspNetCore.Components.WebAssembly; +using Volo.Abp.ObjectMapping; namespace Volo.Abp.Identity.Blazor.Pages.Identity { @@ -15,6 +16,7 @@ namespace Volo.Abp.Identity.Blazor.Pages.Identity { [Inject] private IIdentityRoleAppService RoleAppService { get; set; } [Inject] private IUiMessageService UiMessageService { get; set; } + [Inject] private IObjectMapper ObjectMapper { get; set; } private int _currentPage; private string _currentSorting; @@ -22,17 +24,17 @@ namespace Volo.Abp.Identity.Blazor.Pages.Identity private IReadOnlyList _roles; - private IdentityRoleCreateDto _newRole; + private IdentityRoleCreateDto _newRole; //TODO: Would be better to create a UI model class private Guid _editingRoleId; - private IdentityRoleUpdateDto _editingRole; + private IdentityRoleUpdateDto _editingRole; //TODO: Would be better to create a UI model class private Modal _createModal; private Modal _editModal; public RoleManagement() { - _newRole = new IdentityRoleCreateDto(); - _editingRole = new IdentityRoleUpdateDto(); + _newRole = new IdentityRoleCreateDto(); //TODO: Can we discard this (create on modal opening)? + _editingRole = new IdentityRoleUpdateDto(); //TODO: Can we discard this (create on modal opening)? } protected override async Task OnInitializedAsync() @@ -62,6 +64,7 @@ namespace Volo.Abp.Identity.Blazor.Pages.Identity .JoinAsString(","); _currentPage = e.Page - 1; + await GetRolesAsync(); StateHasChanged(); } @@ -69,6 +72,7 @@ namespace Volo.Abp.Identity.Blazor.Pages.Identity private void OpenCreateModal() { _newRole = new IdentityRoleCreateDto(); + _createModal.Show(); } @@ -82,18 +86,8 @@ namespace Volo.Abp.Identity.Blazor.Pages.Identity var role = await RoleAppService.GetAsync(id); _editingRoleId = id; - - //TODO: User AutoMapper! - _editingRole = new IdentityRoleUpdateDto - { - Name = role.Name, - ConcurrencyStamp = role.ConcurrencyStamp, - IsDefault = role.IsDefault, - IsPublic = role.IsPublic - }; - - role.MapExtraPropertiesTo(_editingRole); - + _editingRole = ObjectMapper.Map(role); + _editModal.Show(); } @@ -106,6 +100,7 @@ namespace Volo.Abp.Identity.Blazor.Pages.Identity { await RoleAppService.CreateAsync(_newRole); await GetRolesAsync(); + _createModal.Hide(); } @@ -113,6 +108,7 @@ namespace Volo.Abp.Identity.Blazor.Pages.Identity { await RoleAppService.UpdateAsync(_editingRoleId, _editingRole); await GetRolesAsync(); + _editModal.Hide(); } diff --git a/modules/identity/src/Volo.Abp.Identity.Blazor/Volo.Abp.Identity.Blazor.csproj b/modules/identity/src/Volo.Abp.Identity.Blazor/Volo.Abp.Identity.Blazor.csproj index d5bca0ad55..6d7f3f83bb 100644 --- a/modules/identity/src/Volo.Abp.Identity.Blazor/Volo.Abp.Identity.Blazor.csproj +++ b/modules/identity/src/Volo.Abp.Identity.Blazor/Volo.Abp.Identity.Blazor.csproj @@ -9,8 +9,12 @@ - + + + + + diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Shared/LoginDisplay.razor b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Shared/LoginDisplay.razor index 10a8c06278..39e1dc3462 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Shared/LoginDisplay.razor +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Shared/LoginDisplay.razor @@ -1,5 +1,4 @@ -@using Microsoft.AspNetCore.Components.Authorization -@using Microsoft.AspNetCore.Components.WebAssembly.Authentication +@using Microsoft.AspNetCore.Components.WebAssembly.Authentication @inject NavigationManager Navigation @inject SignOutSessionStateManager SignOutManager