CRUD to unit of work from code-behind

Hi, 
the setup I have is to use unit of work from my access layer. From the datagid, I'd like to perform the CRUD calls (using the toolbar actions and dialog edit) when the action is confirmed by save and validation. The call to the unit of work in this example is made from code-behind. Any advice?  Thank you

<SfGrid TValue="Projet_General" DataSource="projets"
Toolbar="@(new List<string>() {"Add", "Edit", "Delete", "Cancel", "Update", "Excel Export", "PDF Export"})"
AllowExcelExport="true"
AllowPdfExport="true">
<GridEvents RowSelected="OnSelectedRowChanged" TValue="Projet_General" OnToolbarClick="TypologieToolbarClick"/>
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Dialog"/>
<GridColumns>
<GridColumn Field="@nameof(Projet_General.Id)" IsPrimaryKey="true" Visible="false"></GridColumn>
<GridColumn Field="@nameof(Projet_General.Name)"></GridColumn>
<GridColumn Field="@nameof(Projet_General.Description)"></GridColumn>
<GridColumn Field="@nameof(Projet_General.SuperficieTerrain)"></GridColumn>
</GridColumns>
</SfGrid>
Ex:
protected void Add(Projet_General projetGeneral)
{
_unitOfWork.ProjetGeneral.Add(projetGeneral);
_unitOfWork.Save();
}
protected void Update(Projet_General projetGeneral)
{
_unitOfWork.ProjetGeneral.Update(projetGeneral);
_unitOfWork.Save();
}
protected void Remove(Projet_General projetGeneral)
{
_unitOfWork.ProjetGeneral.Remove(projetGeneral);
_unitOfWork.Save();
}

2 Replies 1 reply marked as answer

YA Yannick May 26, 2021 03:39 PM UTC

I dug a bit deeper and here's my solution using OnActionCompleted. It does not seem optimal. If anybody has a better way, please let me know. 

protected async Task ActionCompleted(ActionEventArgs<Projet_General> arg)
{
if (arg.RequestType == Action.Delete)
{
_unitOfWork.ProjetGeneral.Remove(arg.Data);
await _unitOfWork.Save();
}


if (arg.Action == null)
return;

if (arg.Action == "Add")
_unitOfWork.ProjetGeneral.Add(arg.Data);
else if (arg.Action == "Edit")
await _unitOfWork.ProjetGeneral.UpdateAsync(arg.Data);

await _unitOfWork.Save();
}


VN Vignesh Natarajan Syncfusion Team May 27, 2021 05:24 AM UTC

Hi Yannick,  
 
Thanks for contacting Syncfusion support.  
 
Query: “From the datagid, I'd like to perform the CRUD calls (using the toolbar actions and dialog edit) when the action is confirmed by save and validation” && “here's my solution using OnActionCompleted. It does not seem optimal. If anybody has a better way, please let me know 
 
We have analyzed your query and we would like to inform you that CRUD operation needs to be handled in Grid Action events to save the changes properly in the database. So we suggest you to achieve your requirement using OnActionBegin event of Grid instead of OnActionComplete event. Because OnActionBegin will be triggered when certain action is initiated. In that event we can save the changes in your database when RequestType is Save.  
 
Refer the below code example.  
 
<SfGrid @ref="Grid" DataSource="@GridData" Toolbar="@(new List<string> { "Add""Edit""Delete""Cancel""Update" })" AllowFiltering="true" AllowSorting="true" AllowPaging="true"> 
    <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Dialog"></GridEditSettings> 
    <GridEvents OnActionBegin="OnBegin" OnActionComplete="OnComplete" TValue="Order"></GridEvents> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" Visible="false" IsIdentity="true" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn> 
        <GridColumn Field=@nameof(Order.EmployeeID) HeaderText="Id" Width="150"></GridColumn> 
    </GridColumns> 
</SfGrid> 
@code{ 
    SfGrid<Order> Grid { getset; } 
    public IEnumerable<Order> GridData { getset; } 
    protected override void OnInitialized() 
    { 
        GridData = OrderData.GetAllOrders().ToList(); 
    } 
    public void OnComplete(ActionEventArgs<Order> Args) 
    { 
        if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save || Args.RequestType == Syncfusion.Blazor.Grids.Action.Cancel) 
        { 
            // fetch updated data from service and bind to grid datasource property 
            GridData = OrderData.GetAllOrders().ToList(); 
        } 
    } 
    public void OnBegin(ActionEventArgs<Order> Args) 
    { 
        if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save// update the changes in Actionbegine event 
        { 
            if (Args.Action == "Add") 
            { 
                //insert the record into database 
                OrderData.AddOrder(Args.Data); 
            } 
            else 
            { 
                //update the existing record 
                OrderData.UpdateOrder(Args.Data); 
            } 
        } 
        else if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Delete) 
        { 
            // delete the record from your database 
            OrderData.DeleteOrder(Args.Data.OrderID); 
        } 
    } 
 
 
For your convenience we have prepared a sample which can be downloaded from below  
 
 
Refer our UG documentation for your reference 
 
 
Please get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan 


Marked as answer
Loader.
Up arrow icon