Using abstract classes for applicationServices

46 views Asked by At

I'm building an application on top of abp framework and struggle with a design decision I made.

I have an abstract class step, which can be one of the following:

 - Introduction
 - Regular 
 - Navigation 
 - Finish

These StepTypes are very similar to each other, so I chose to use an abstract class and a discriminator in efCore to save all entities into one table.

Now I want to use a single applicationService to work with these entities but I run into issues since abp doesn't know which explicit entityType to return after creation.

public class StepAppService :
    CrudAppService<
        BaseStep,                        //Abstract step entity
        BaseStepDto,                     //Abstract step for transfer
        Guid,                            //Primary key of the step entity
        PagedAndSortedResultRequestDto,  //Used for paging/sorting
        CreateUpdateBaseStepDto>,        //Absract step used to create/update a step
    IBaseStepAppService {

    // ..... other ....
    
    public async Task<BaseStepDto> CreateByTypeAsync(CreateUpdateBaseStepDto input)
    {
        // Try to pass an explicit type to the repository
        if(input.StepType == StepType.Introduction)
        {
            return await base.CreateAsync(ObjectMapper.Map<CreateUpdateBaseStepDto, CreateUpdateIntroductionStepDto>(input));
        }
        else if (input.StepType == StepType.Navigation)
        {
            return await base.CreateAsync(ObjectMapper.Map<CreateUpdateBaseStepDto, CreateUpdateNavigationStepDto>(input));
        }
        else if (input.StepType == StepType.Regular)
        {
            return await base.CreateAsync(ObjectMapper.Map<CreateUpdateBaseStepDto, CreateUpdateRegularStepDto>(input));
        }
        else if (input.StepType == StepType.Finish)
        {
            return await base.CreateAsync(ObjectMapper.Map<CreateUpdateBaseStepDto, CreateUpdateFinishStepDto>(input));
        }
        else
        {
            return await base.CreateAsync(ObjectMapper.Map<CreateUpdateBaseStepDto, CreateUpdateNavigationStepDto>(input));
        }
    }}

I feel like I'm following an off-beat path and there should be a better design-decision for my problem. Do you have any suggestions on how I can use different steps but keep the comfort of using one applicationService?

Thanks :)

0

There are 0 answers