Manually use DataManager

I would like to be able to use DataManager in the code behind, configure it and send my CRUD requests to a ODataV4 API using a supplied ODataV4Adapter. I have injected the Scoped HttpClient instance, I have provided the same URL that I use when I use the SfDataManager in a DataGrid. I am calling DataGrid.InsertAsync<Item>(item) but it never makes the call. I get no errors, my modal dialog closes like everything worked, but I have no network traffic on the client side and receive no requests on the server side. 


3 Replies

RN Rahul Narayanasamy Syncfusion Team April 6, 2022 03:26 PM UTC

Hi Josh,


Greetings from Syncfusion.


We suspect that you want to manually send requests to your service using HttpClient. SfDataManager uses the HttpClient instance to make HTTP requests to data services. SfDataManager checks whether a HttpClient is already registered in the service container. If it’s found, the SfDataManager will use HttpClient from the service container else it will create and add HttpClient to the service container and use that instance for making requests to the server.


When registering your HttpClient, the registration should be done before calling AddSyncfusionBlazor() method in Startup.cs/Program.cs, so that SfDataManager will not create its own HttpClient and uses the pre-configured HttpClient.


Note: Using Typed HttpClient with SfDataManager is not supported.


Reference: https://blazor.syncfusion.com/documentation/datagrid/data-binding#configuring-httpclient


If you want to perform the custom operation, then can you use the CustomBinding feature at your end and ensure the reported case from your end?


Reference:

https://blazor.syncfusion.com/documentation/datagrid/custom-binding


If it does not meet your requirement, then could you please share more details(more details/ sample) about your case?


Regards,

Rahul



JO Josh April 7, 2022 09:24 PM UTC

This is my use case. I have created a class that inherits from DataManager and configured it to have most of what I need pre-configured. I inject a HttpClient into it and set the DataManager.HttpClientInstance equal to the injected HttpClient as well as set the Adaptor to a Adaptors.OdataV4Adaptor. I then inject the CustomDataManager class into my SfDialog component. 


@using Syncfusion.Blazor.Popups;

@using Syncfusion.Blazor.Inputs;

@inject DataManager DataManager;


<SfDialog Visible=@IsVisible>

    <DialogTemplates>

        <Header>@((CabinetKey.Id==Guid.Empty)?"New":"Edit") Key</Header>

        <Content>

            <SfTextBox Type="InputType.Text" @bind-Value=CabinetKey.Name></SfTextBox>

        </Content>

    </DialogTemplates>

    <DialogButtons>

        <DialogButton Content="Cancel" OnClick="@OnCancel"/>

        <DialogButton Content="Save" IsPrimary=true OnClick="@OnSave"/>

    </DialogButtons>

</SfDialog>


@code {

    [Parameter] public bool IsVisible { get; set; }

    [Parameter] public CabinetKey CabinetKey { get; set; }


    protected override void OnInitialized()

    {

        base.OnInitialized();

        DataManager.Url += "CabinetKeys";

        DataManager.Key = "Id";

    }


    private async Task OnSave()

    {

        if (IsNew())

        {

           await DataManager.InsertAsync<CabinetKey>(CabinetKey);

        } 

        else 

        {

            await DataManager.UpdateAsync<CabinetKey>("Id", CabinetKey);

        }

        IsVisible = false;

    }


    private void OnCancel()

    {

        IsVisible = false;

    }


    private bool IsNew()

    {

        return CabinetKey.Id == Guid.Empty;

    }

}




RN Rahul Narayanasamy Syncfusion Team April 8, 2022 04:01 PM UTC

Hi Josh,


Thanks for the update.


From your shared details, you have injected DataManager like a service. The SfDataManager is a component used to fetch the data. But here, you used it like a service. So it is not possible to use Datamanager like a service as you have done in your example. Please find the details SfDataManager from below links.


Reference:

https://blazor.syncfusion.com/documentation/data/getting-started

https://blazor.syncfusion.com/documentation/data/adaptors

https://blazor.syncfusion.com/documentation/data/custom-binding


Also, share the details about your custom data adaptor component details. Share the details about how you are implementing the custom data manager.


Please let us know if you have any concerns.


Regards,

Rahul



Loader.
Up arrow icon