0

I have a class like...

public class IndexBase<T> : ComponentBase

inside that class is a class which has this code...

public class DataGridPageSetting
{
    public DataGridSearchFilter DataGridSearchFilter { get; set; } = new DataGridSearchFilter();
    public DataGridSettings? DataGridSettings { get; set; }
}
public class DataGridPageSetting<T> where T : class, new()
{
    public DataGridSearchFilter DataGridSearchFilter { get; set; } = new DataGridSearchFilter();
    public DataGridSettings? DataGridSettings { get; set; }
    public T AdditionalSettings { get; set; } = new T();
}

I can create DataGridPageSetting easily enough but I need to create DataGridPageSetting with T in this class. I can't do that because I am inheriting from ComponentBase...

Abbreviated version of full code...

public class IndexBase<T> : ComponentBase
{
  
    [Inject]
    public NavigationManager NavigationManager { get; set; } = default!;

    [Inject]
    public ILocalStorageService LocalStorageService { get; set; } = default!;

    public string ODataQuerySerialized { get; set; } = String.Empty;

    public string GridFilter { get; set; } = string.Empty;

    //Datagrid
    public IEnumerable<T>? GridData { get; set; }

    public SpDataGrid<T> Grid { get; set; } = default!;
    public DataGridPageSetting DataGridPageSetting { get; set; } = new DataGridPageSetting();

    public int[] PageSizeOptions { get; set; } = new int[] { 10, 25, 50, 100 };
    public int Count { get; set; }
    public bool IsLoading { get; set; }
    public bool GridStateLoaded { get; set; } = false;

    public string ErrorMessage { get; set; } = "";

    public bool EnableExport = true;

    public bool Busy { get; set; } = false;

    public DataGridSettings? Settings
    {
        get
        {
            return DataGridPageSetting.DataGridSettings;
        }
        set
        {
            if (DataGridPageSetting.DataGridSettings != value)
            {
                DataGridPageSetting.DataGridSettings = value;
                InvokeAsync(SaveStateAsync);
            }
        }
    }

    public async Task GridOnAfterRenderAsync(bool firstRender)
    {
        if (firstRender || !GridStateLoaded)
        {
            GridStateLoaded = true;
            await LoadStateAsync();
            await Grid.Reload();
        }
    }

    public virtual async Task SaveStateAsync()
    {
        var appName = Assembly.GetExecutingAssembly().GetName().Name;
        var pageName = NavigationManager.Uri.Substring(NavigationManager.BaseUri.Length);
        await LocalStorageService.SetItemAsync<DataGridPageSetting>($"{appName}_{TenantInfo!.TenantId}_{pageName.ToUpper()}_SettingsLoadData", DataGridPageSetting);
    }

    public virtual async Task LoadStateAsync()
    {
        var appName = Assembly.GetExecutingAssembly().GetName().Name;
        var pageName = NavigationManager.Uri.Substring(NavigationManager.BaseUri.Length);

        var pageSettings = await LocalStorageService.GetItemAsync<DataGridPageSetting>($"{appName}_{TenantInfo!.TenantId}_{pageName.ToUpper()}_SettingsLoadData");
        if (pageSettings != null)
        {
            DataGridPageSetting = pageSettings;
        }
    }

    /// <remarks>
    /// The below syntax is used to create the string for custom filtering, see AuditLogIndex for example
    ///  var oDataParameterizer = new ODataParameterizer();
    ///var customFilter = oDataParameterizer.Parameterize("RequestDateTime ge @StartDate and RequestDateTime le @EndDate",
    ///    new
    ///    {
    ///        StartDate = dataGridPageSetting.AdditionalSettings.StartDate,
    ///        EndDate = dataGridPageSetting.AdditionalSettings.EndDate.AddDays(1).AddMilliseconds(-1)
    ///    });
    /// </remarks>
    public string BuildODataFilter(string? customFilter = null)
    {
        var filter = "";

        if (!string.IsNullOrWhiteSpace(customFilter))
        {
            filter = $"({customFilter})";
        }
        if (!string.IsNullOrWhiteSpace(DataGridPageSetting.DataGridSearchFilter.SearchFilter))
        {
            if (!string.IsNullOrWhiteSpace(filter))
            {
                filter += " and ";
            }
            filter += $"({DataGridPageSetting.DataGridSearchFilter.SearchFilter})";
        }
        if (!string.IsNullOrWhiteSpace(GridFilter))
        {
            if (!string.IsNullOrWhiteSpace(filter))
            {
                filter += " and ";
            }
            filter += $"({GridFilter})";
        }

        return string.IsNullOrWhiteSpace(filter) ? "" : $"({filter})";
    }

    public async Task FilterSubmitHandler(string filterText)
    {
        DataGridPageSetting.DataGridSearchFilter.SearchValue = Filter?.SearchTerm ?? "";
        DataGridPageSetting.DataGridSearchFilter.SearchFilter = filterText;

        await Grid.FirstPage(forceReload: true);
        await InvokeAsync(SaveStateAsync);
    }
}

This is what I need, but won't work...

public class IndexBase<T, TPageSetting> : ComponentBase
{
    [CascadingParameter]
    public TenantInfo? TenantInfo { get; set; } = default;

    [Inject]
    public NavigationManager NavigationManager { get; set; } = default!;

    [Inject]
    public ILocalStorageService LocalStorageService { get; set; } = default!;

   
    //DataGrid Search Filter
    public SpDataGridSearchFilter<T>? Filter { get; set; } = new SpDataGridSearchFilter<T>();

    public SpExcelExport<T>? Export { get; set; } = new SpExcelExport<T>();


    public DataGridPageSetting<TPageSetting> DataGridPageSetting { get; set; } = new DataGridPageSetting<TPageSetting>();

   
    public DataGridSettings? Settings
    {
        get
        {
            return DataGridPageSetting.DataGridSettings;
        }
        set
        {
            if (DataGridPageSetting.DataGridSettings != value)
            {
                DataGridPageSetting.DataGridSettings = value;
                InvokeAsync(SaveStateAsync);
            }
        }
    }

    

    public virtual async Task SaveStateAsync()
    {
        var appName = Assembly.GetExecutingAssembly().GetName().Name;
        var pageName = NavigationManager.Uri.Substring(NavigationManager.BaseUri.Length);
        await LocalStorageService.SetItemAsync<DataGridPageSetting<TPageSetting>>($"{appName}_{TenantInfo!.TenantId}_{pageName.ToUpper()}_SettingsLoadData", DataGridPageSetting);
    }

    public virtual async Task LoadStateAsync()
    {
        var appName = Assembly.GetExecutingAssembly().GetName().Name;
        var pageName = NavigationManager.Uri.Substring(NavigationManager.BaseUri.Length);

        var pageSettings = await LocalStorageService.GetItemAsync<DataGridPageSetting<TPageSetting>>($"{appName}_{TenantInfo!.TenantId}_{pageName.ToUpper()}_SettingsLoadData");
        if (pageSettings != null)
        {
            DataGridPageSetting = pageSettings;
        }
    }

Any suggestions?

  • A lot of code, and your intention is not clear. Try to keep only the relevant parts. – Artur Jun 09 '23 at 17:29
  • 1
    What do you mean "won't work"? What happens when you try it. I assume you don't get any compiler or runtime errors, since you surely would have included them in your question, right? – David Conrad Jun 09 '23 at 17:43
  • Also while probably not a problem yet but your `DataGridPageSetting` should inherit `DataGridPageSetting` (and you don't need to repeat the 2 properties from `DataGridPageSetting`). And like other said, can you give us a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? – Luke Vo Jun 09 '23 at 17:44
  • 1
    And does this work? `public DataGridPageSetting DataGridPageSetting { get; set; } = new DataGridPageSetting();` (you need to add the same constraint to your `IndexBase` though) – Luke Vo Jun 09 '23 at 17:47
  • "This is what I need, but won't work..." - what part, what line of code, what are you trying to accomplish? – NetMage Jun 09 '23 at 18:18
  • "I can't do that because I am inheriting from ComponentBase" Do you mean you can't constraint `` (so it has the same constraints as the type variable that appears later on the inner class) because you're inheriting from `ComponentBase`? Does [this answer](https://stackoverflow.com/a/2007441/636009) help you? Also, if the one generic class is actually defined inside the other, I think you will have to use distinct type variables. – David Conrad Jun 09 '23 at 18:30
  • 1
    You must use the same generic type constraints for TPageSetting like for the T of DataGridPageSetting: `public class IndexBase : ComponentBase where TPageSetting : class, new()`. It ís generally helpful if you can explain what exactly is not working. Compiler errors and exceptions etc. also provide important details about your problem. –  Jun 09 '23 at 20:40

0 Answers0