How to inherit from a bindable component?

Viewed 113

I'm refactoring some code and I would like to inherit from a third-party combobox component to avoid repetitive code. Currently forms contain the component like this:

<DxComboBox 
    TData="@(Category)"
    TValue="@(Category)"
    CustomData="@LoadCategories"
    @bind-Value="@product.Category"
    TextFieldName="@nameof(Category.Description)" />

And the end-result should be:

<CategoryComboBox @bind-Value="@product.Category" />

So I created a class-only component:

public class CategoryComboBox : DxComboBox<Category, Category>
{
    private readonly ICategoryService service;
    private readonly ILogger<CategoryComboBox > logger;

    public CategoryComboBox () { }

    public CategoryComboBox (
        ICategoryService service,
        ILogger<CategoryComboBox> logger)
    {
        this.service= service;
        this.logger = logger;
        this.CustomData = LoadCategories;
        this.TextFieldName = nameof(Category.Description);
    }

    private async Task<IEnumerable<Category>> LoadCategories(CancellationToken ct)
    {
        return await service.GetCategories(ct);
    }
}

I had the above code without the parameterless ctor but when I ran the app, I got an exception and I had to add the ctor.

The component does render without errors, however the list is empty. The list should contain a few items.

The problem is that the parameterless ctor is called at runtime. Is this not the correct way to inherit from a component? What am I missing?

0 Answers
Related