Browse Source

Support to specify different types for view models for the BlazoriseCrudPageBase

pull/5575/head
Halil İbrahim Kalkan 6 years ago
parent
commit
c13f5e8e39
  1. 210
      framework/src/Volo.Abp.BlazoriseUI/Volo/Abp/BlazoriseUI/BlazoriseCrudPageBase.cs

210
framework/src/Volo.Abp.BlazoriseUI/Volo/Abp/BlazoriseUI/BlazoriseCrudPageBase.cs

@ -18,51 +18,155 @@ using Volo.Abp.ObjectMapping;
namespace Volo.Abp.BlazoriseUI namespace Volo.Abp.BlazoriseUI
{ {
public abstract class BlazoriseCrudPageBase<TAppService, TEntityDto, TKey> public abstract class BlazoriseCrudPageBase<
: BlazoriseCrudPageBase<TAppService, TEntityDto, TKey, PagedAndSortedResultRequestDto> TAppService,
where TAppService : ICrudAppService<TEntityDto, TKey> TEntityDto,
where TEntityDto : IEntityDto<TKey>, new() TKey>
: BlazoriseCrudPageBase<
TAppService,
TEntityDto,
TKey,
PagedAndSortedResultRequestDto>
where TAppService : ICrudAppService<
TEntityDto,
TKey>
where TEntityDto : class, IEntityDto<TKey>, new()
{ {
} }
public abstract class BlazoriseCrudPageBase<TAppService, TEntityDto, TKey, TGetListInput> public abstract class BlazoriseCrudPageBase<
: BlazoriseCrudPageBase<TAppService, TEntityDto, TKey, TGetListInput, TEntityDto> TAppService,
where TAppService : ICrudAppService<TEntityDto, TKey, TGetListInput> TEntityDto,
where TEntityDto : IEntityDto<TKey>, new() TKey,
TGetListInput>
: BlazoriseCrudPageBase<
TAppService,
TEntityDto,
TKey,
TGetListInput,
TEntityDto>
where TAppService : ICrudAppService<
TEntityDto,
TKey,
TGetListInput>
where TEntityDto : class, IEntityDto<TKey>, new()
where TGetListInput : new() where TGetListInput : new()
{ {
} }
public abstract class BlazoriseCrudPageBase<TAppService, TEntityDto, TKey, TGetListInput, TCreateInput> public abstract class BlazoriseCrudPageBase<
: BlazoriseCrudPageBase<TAppService, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput> TAppService,
where TAppService : ICrudAppService<TEntityDto, TKey, TGetListInput, TCreateInput> TEntityDto,
TKey,
TGetListInput,
TCreateInput>
: BlazoriseCrudPageBase<
TAppService,
TEntityDto,
TKey,
TGetListInput,
TCreateInput,
TCreateInput>
where TAppService : ICrudAppService<
TEntityDto,
TKey,
TGetListInput,
TCreateInput>
where TEntityDto : IEntityDto<TKey> where TEntityDto : IEntityDto<TKey>
where TCreateInput : new() where TCreateInput : class, new()
where TGetListInput : new() where TGetListInput : new()
{ {
} }
public abstract class BlazoriseCrudPageBase<TAppService, TEntityDto, TKey, TGetListInput, TCreateInput, public abstract class BlazoriseCrudPageBase<
TAppService,
TEntityDto,
TKey,
TGetListInput,
TCreateInput,
TUpdateInput>
: BlazoriseCrudPageBase<
TAppService,
TEntityDto,
TEntityDto,
TKey,
TGetListInput,
TCreateInput,
TUpdateInput>
where TAppService : ICrudAppService<
TEntityDto,
TKey,
TGetListInput,
TCreateInput,
TUpdateInput> TUpdateInput>
: BlazoriseCrudPageBase<TAppService, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TAppService : ICrudAppService<TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntityDto : IEntityDto<TKey> where TEntityDto : IEntityDto<TKey>
where TCreateInput : new() where TCreateInput : class, new()
where TUpdateInput : new() where TUpdateInput : class, new()
where TGetListInput : new()
{
}
public abstract class BlazoriseCrudPageBase<
TAppService,
TGetOutputDto,
TGetListOutputDto,
TKey,
TGetListInput,
TCreateInput,
TUpdateInput>
: BlazoriseCrudPageBase<
TAppService,
TGetOutputDto,
TGetListOutputDto,
TKey,
TGetListInput,
TCreateInput,
TUpdateInput,
TGetListOutputDto,
TCreateInput,
TUpdateInput>
where TAppService : ICrudAppService<
TGetOutputDto,
TGetListOutputDto,
TKey,
TGetListInput,
TCreateInput,
TUpdateInput>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
where TCreateInput : class, new()
where TUpdateInput : class, new()
where TGetListInput : new() where TGetListInput : new()
{ {
} }
public abstract class BlazoriseCrudPageBase<TAppService, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, public abstract class BlazoriseCrudPageBase<
TCreateInput, TUpdateInput> TAppService,
TGetOutputDto,
TGetListOutputDto,
TKey,
TGetListInput,
TCreateInput,
TUpdateInput,
TListViewModel,
TCreateViewModel,
TUpdateViewModel>
: OwningComponentBase : OwningComponentBase
where TAppService : ICrudAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, where TAppService : ICrudAppService<
TGetOutputDto,
TGetListOutputDto,
TKey,
TGetListInput,
TCreateInput,
TUpdateInput> TUpdateInput>
where TGetOutputDto : IEntityDto<TKey> where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey> where TGetListOutputDto : IEntityDto<TKey>
where TCreateInput : new() where TCreateInput : class
where TUpdateInput : new() where TUpdateInput : class
where TGetListInput : new() where TGetListInput : new()
where TListViewModel : IEntityDto<TKey>
where TCreateViewModel : class, new()
where TUpdateViewModel : class, new()
{ {
[Inject] protected TAppService AppService { get; set; } [Inject] protected TAppService AppService { get; set; }
[Inject] protected IUiMessageService UiMessageService { get; set; } [Inject] protected IUiMessageService UiMessageService { get; set; }
@ -74,10 +178,10 @@ namespace Volo.Abp.BlazoriseUI
protected int CurrentPage; protected int CurrentPage;
protected string CurrentSorting; protected string CurrentSorting;
protected int? TotalCount; protected int? TotalCount;
protected IReadOnlyList<TGetListOutputDto> Entities = Array.Empty<TGetListOutputDto>(); protected IReadOnlyList<TListViewModel> Entities = Array.Empty<TListViewModel>();
protected TCreateInput NewEntity; protected TCreateViewModel NewEntity;
protected TKey EditingEntityId; protected TKey EditingEntityId;
protected TUpdateInput EditingEntity; protected TUpdateViewModel EditingEntity;
protected Modal CreateModal; protected Modal CreateModal;
protected Modal EditModal; protected Modal EditModal;
@ -129,8 +233,8 @@ namespace Volo.Abp.BlazoriseUI
protected BlazoriseCrudPageBase() protected BlazoriseCrudPageBase()
{ {
NewEntity = new TCreateInput(); NewEntity = new TCreateViewModel();
EditingEntity = new TUpdateInput(); EditingEntity = new TUpdateViewModel();
} }
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
@ -161,10 +265,20 @@ namespace Volo.Abp.BlazoriseUI
{ {
var input = await CreateGetListInputAsync(); var input = await CreateGetListInputAsync();
var result = await AppService.GetListAsync(input); var result = await AppService.GetListAsync(input);
Entities = result.Items; Entities = MapToListViewModel(result.Items);
TotalCount = (int?) result.TotalCount; TotalCount = (int?) result.TotalCount;
} }
private IReadOnlyList<TListViewModel> MapToListViewModel(IReadOnlyList<TGetListOutputDto> dtos)
{
if (typeof(TGetListOutputDto) == typeof(TListViewModel))
{
return dtos.As<IReadOnlyList<TListViewModel>>();
}
return ObjectMapper.Map<IReadOnlyList<TGetListOutputDto>, List<TListViewModel>>(dtos);
}
protected virtual Task<TGetListInput> CreateGetListInputAsync() protected virtual Task<TGetListInput> CreateGetListInputAsync()
{ {
var input = new TGetListInput(); var input = new TGetListInput();
@ -187,7 +301,7 @@ namespace Volo.Abp.BlazoriseUI
return Task.FromResult(input); return Task.FromResult(input);
} }
protected virtual async Task OnDataGridReadAsync(DataGridReadDataEventArgs<TGetListOutputDto> e) protected virtual async Task OnDataGridReadAsync(DataGridReadDataEventArgs<TListViewModel> e)
{ {
CurrentSorting = e.Columns CurrentSorting = e.Columns
.Where(c => c.Direction != SortDirection.None) .Where(c => c.Direction != SortDirection.None)
@ -204,7 +318,7 @@ namespace Volo.Abp.BlazoriseUI
{ {
await CheckCreatePolicyAsync(); await CheckCreatePolicyAsync();
NewEntity = new TCreateInput(); NewEntity = new TCreateViewModel();
CreateModal.Show(); CreateModal.Show();
} }
@ -224,9 +338,29 @@ namespace Volo.Abp.BlazoriseUI
EditModal.Show(); EditModal.Show();
} }
protected virtual TUpdateInput MapToEditingEntity(TGetOutputDto entityDto) protected virtual TUpdateViewModel MapToEditingEntity(TGetOutputDto entityDto)
{
return ObjectMapper.Map<TGetOutputDto, TUpdateViewModel>(entityDto);
}
protected virtual TCreateInput MapToCreateInput(TCreateViewModel createViewModel)
{ {
return ObjectMapper.Map<TGetOutputDto, TUpdateInput>(entityDto); if (typeof(TCreateInput) == typeof(TCreateViewModel))
{
return createViewModel.As<TCreateInput>();
}
return ObjectMapper.Map<TCreateViewModel, TCreateInput>(createViewModel);
}
protected virtual TUpdateInput MapToUpdateInput(TUpdateViewModel updateViewModel)
{
if (typeof(TUpdateInput) == typeof(TUpdateViewModel))
{
return updateViewModel.As<TUpdateInput>();
}
return ObjectMapper.Map<TUpdateViewModel, TUpdateInput>(updateViewModel);
} }
protected virtual Task CloseEditModalAsync() protected virtual Task CloseEditModalAsync()
@ -238,20 +372,24 @@ namespace Volo.Abp.BlazoriseUI
protected virtual async Task CreateEntityAsync() protected virtual async Task CreateEntityAsync()
{ {
await CheckCreatePolicyAsync(); await CheckCreatePolicyAsync();
await AppService.CreateAsync(NewEntity); var createInput = MapToCreateInput(NewEntity);
await AppService.CreateAsync(createInput);
await GetEntitiesAsync(); await GetEntitiesAsync();
CreateModal.Hide(); CreateModal.Hide();
} }
protected virtual async Task UpdateEntityAsync() protected virtual async Task UpdateEntityAsync()
{ {
await CheckUpdatePolicyAsync(); await CheckUpdatePolicyAsync();
await AppService.UpdateAsync(EditingEntityId, EditingEntity); var updateInput = MapToUpdateInput(EditingEntity);
await AppService.UpdateAsync(EditingEntityId, updateInput);
await GetEntitiesAsync(); await GetEntitiesAsync();
EditModal.Hide(); EditModal.Hide();
} }
protected virtual async Task DeleteEntityAsync(TGetListOutputDto entity) protected virtual async Task DeleteEntityAsync(TListViewModel entity)
{ {
await CheckDeletePolicyAsync(); await CheckDeletePolicyAsync();
@ -264,7 +402,7 @@ namespace Volo.Abp.BlazoriseUI
await GetEntitiesAsync(); await GetEntitiesAsync();
} }
protected virtual string GetDeleteConfirmationMessage(TGetListOutputDto entity) protected virtual string GetDeleteConfirmationMessage(TListViewModel entity)
{ {
return UiLocalizer["ItemWillBeDeletedMessage"]; return UiLocalizer["ItemWillBeDeletedMessage"];
} }

Loading…
Cancel
Save