Using Dapper ORM with ComponentOne Blazor
What is Dapper?
Micro-ORMs are lightweight object-relational mappers (ORM), and Dapper, also known as the King of Micro ORM, is the most popular micro-ORM. It is a simple object mapping tool that lets you work with data using business objects in a .NET application without writing code to map query results from ADO.NET data readers to instances of objects. It is an open-source, lightweight ORM developed by the Stack Overflow team and is very fast compared to other ORMs primarily because of its lightweight. It provides support for both static and dynamic object binding using SQL Query, transactions, and stored procedures.
How Dapper Works
- Install Dapper using Nuget Package Manager.
- Create the POCO classes for database tables.
- Create an IDbConnection Object.
- Write a SQL query to perform either of the CRUD operations.
- Passe Query as Parameter in the Execute Method to fetch the desired results.
ComponentOne Blazor Edition is a set of blazor UI controls provided by ComponentOne. For details, refer to demos and documentation.
This article will demonstrate how to perform CRUD operations on FlexGrid control from the ComponentOne Blazor Edition using Dapper.
Now, let's integrate the above steps to create a Blazor application that would use Dapper to perform CRUD operations in FlexGrid.
Step 1: Configure Blazor Application
- In Visual Studio 2019, create a new project, selecting the Blazor App template.
- In the Configure your new project window, provide the project name and location. Click Create.
- In the Create a new Blazor app dialog, select Blazor Server App template and click Create. A new server-side Blazor app is created.
Note: Blazor Client-side app or WebAssembly app can be created using the Blazor WebAssembly App template. For details, check the Blazor WebAssembly documentation topic here.
Step 2: Install Nuget Packages
We would need to install the following three packages to work with Dapper, SQL Database, and ComponentOne Blazor FlexGrid:
- Dapper
- System.Data.SqlClient
- C1.Blazor.Grid
Step 3: Add Database & Client-side Resources
- We will be working with SQL database using SQL database file, namely C1Nwind.mdf. Download C1NWind.mdf database file and add it to the project's Data folder.
- To work with ComponentOne Blazor controls, you must register client side resources for these controls in the application. Navigate to the Pages folder, open _Host.cshtml file and register the client resources by adding the following lines of code to the head tag.
Step 4: Configure Connection String
We will save the connection string for connecting to SQL database file, by adding the connection string in appsettings.json file using following code:
Create a new class “SqlConnectionConfiguration" under "Data" folder to fetch SQL connection string from appsettings.json configuration file by using the following code:
Step 5: Add POCO Classes
A POCO entity is a class that doesn't depend on any framework-specific base class. It is like any other normal. NET CLR class, which is why it is called "Plain Old CLR Objects."
To map database tables to .Net business objects, we must create a POCO class for each database table. For this article, we will be creating a POCO class each for the Products table and Employees table. We will be using the Employees table to showcase all the CRUD operations being performed on ComponentOne FlexGrid and Products table to showcase invoking of a stored procedure using Dapper.
A POCO class is defined by adding a property corresponding to each column/field in the database table.
Create and add Employee class for Employees table in the project's Data folder using the following code:
Similarly, create and add Product class for Products table in the project's Data folder using the following code:
You can define classes for all the other database tables you would like to use Dapper.
Step 6: Define Data Access Interfaces
Dapper API consists of various versions of the Query and Execute method, which are used to execute SQL commands and return appropriate results depending on the method's return type.
We would utilize these methods by defining an interface that would contain all the methods that we intend to implement to perform CRUD operations using Dapper.
Create and add IEmployeeService interface in the project's Data folder to define the methods used to perform CRUD operations on the Employees table:
Similarly, create and add IProductService interface in the project's Data folder to define the methods used to perform CRUD operations on the Products table:
Step 7: Create Classes Implementing CRUD Operations
The interfaces defined above must be implemented through classes to add expected definitions to the methods. The method definitions would define SQL queries for Create, Update, Read, Delete operations, and execute them using appropriate methods from Dapper API.
Create and add EmployeeService class to project's Data folder. This class implements the IEmployeeService interface.
In the code below, the QueryAsync, QueryFirstOrDefaultAsync methods from Dapper API have been used to perform the Read operation, and the ExecuteAsync method has been used to perform other operations, including Create, Update and Delete on the Employees table.
Similarly, create and add ProductService class to the project's Data folder. This class implements IProductService Interface.
In the code below, the QueryAsync method of Dapper API has been used to execute the stored procedure 'Alphabetical List of Products and get the resulting records from the Products table.
Step 8: Register Data Access Classes
To inject and access the interfaces and their methods defined above in razor components, we must register them in the startup.cs file by adding the following code to the ConfigureServices method:
Step 9: Creating Razor Components to Perform CRUD Operations
Read Operation:
Create and add a razor component named ListEmployees in the Pages folder.
This razor component will perform the Read operation by specifying the appropriate SQL query and invoking the GetEmployees method from the EmployeeService class. The code also initializes the ComponentOne Blazor FlexGrid control and binds the fetched data to FlexGrid. The page will render three hyperlinks at the top to perform the Create, Update and Delete operations on FlexGrid by navigating to the designated Razor pages for performing the specified operations.
A navigation route to this page must be added to the Navigation menu of the Blazor application to let the user explore the CRUD operations in action.
Here is a quick view of ListEmployees razor component, showcasing FlexGrid displaying fetched data along with three links to perform other CRUD operations:
Create Operation:
Create and add a razor component named AddEmployee in the Pages folder.
This razor component will perform the Create operation by specifying the appropriate SQL query and invoking the CreateEmployee method from the EmployeeService class. This code will append a new record in the database and navigate to the ListEmployees. It will be a razor page to display the updated data fetched from the database.
Here is a quick view of the AddEmployee razor component:
Update Operation:
Create and add razor component named EditEmployee in the Pages folder.
This razor component will perform the Update operation by specifying the appropriate SQL query and invoking the EditEmployee method from the EmployeeService class. This code will update the selected record in the database and navigate back to ListEmployees.razor page to display the updated data fetched from database.
Delete Operation:
Create and add razor component named DeleteEmployee in the Pages folder.
This razor component will perform the Delete operation by specifying the appropriate SQL query and invoking the DeleteEmployee method from the EmployeeService class. This code will delete the selected record from the database and navigate back to ListEmployees.razor page to display the updated data fetched from database.
Executing Stored Procedure:
Create and add razor component named ListProducts in the Pages folder.
This razor component will execute the stored procedure "Alphabetical List of Products" by invoking the GetProducts method from the ProductService class. This code will display the alphabetical list of Products along with other columns from the Products table.
A navigation route to this page must be added to the Navigation menu of the Blazor application to let user observe the Stored Procedure execution in action.
Step 10: Updating Application's Navigation menu
Append the following code to NavMenu.razor file to add routes for ListEmployees.razor and ListProducts.razor pages:
Step 11: Dapper in Action
We are done implementing the integration of Dapper with ComponentOne Blazor FlexGrid. Execute the application to perform CRUD operations and see Dapper in action, the GIF below depicts the same:
Download the complete sample implementing the above steps here.