MongoDB Data Source in ASP.NET Core App
- 3 minutes to read
This article shows how to add the DashboardMongoDBDataSource to an in-memory data source storage, and make it available to users. The tutorial uses a local MongoDB database and shows multiple connection options.
Prerequisites
Install the MongoDB.Driver NuGet package.
Create a MongoDB Data Source
Create a DashboardMongoDBDataSource instance with the specified connection name (mongoDataSourceConnection).
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardCommon;
using DevExpress.DataAccess.ConnectionParameters;
public static DataSourceInMemoryStorage CreateDataSourceStorage() {
// ...
DashboardMongoDBDataSource mongoDataSource = new DashboardMongoDBDataSource("MongoDB Data Source", "mongoDataSourceConnection");
}
Specify Connection Parameters
Handle the DashboardConfigurator.ConfigureDataConnection event to specify connection parameters. You can use a connection string or configure connection parameters manually.
Use a Connection String
- Create a MongoDBCustomConnectionParameters object with the specified connection string.
- Assign the created
MongoDBCustomConnectionParametersobject to the e.ConnectionParameters event parameter.
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.ConnectionParameters;
private void Configurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
if (e.ConnectionName == "mongoDataSourceConnection") {
e.ConnectionParameters = new MongoDBCustomConnectionParameters("mongodb://mongodb0.example.com:27017");
}
}
Configure Connection Parameters
- Create a MongoDBConnectionParameters object.
- Use Hostname and Port properties to specify the hostname and port.
- Optional. Enable the IsSRVRecord option if the hostname is an SRV record.
- Optional. Use the object’s AuthenticationInfo property to specify authentication credentials.
- Assign the created
MongoDBCustomConnectionParametersobject to the e.ConnectionParameters event parameter.
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.ConnectionParameters;
private void Configurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
if (e.ConnectionName == "mongoDataSourceConnection") {
// MongoDB without authentication credentials.
e.ConnectionParameters = new MongoDBConnectionParameters("localhost", false, 27017);
}
if (e.ConnectionName == "mongoDataSourceConnectionWithAuth") {
// MongoDB with authentication credentials.
var infoScramSHA256 = new MongoDBScramSHA256AuthenticationInfo("UserName", "Password", "Northwind");
e.ConnectionParameters = new MongoDBConnectionParameters("localhost", false, 27017, infoScramSHA256);
}
}
Load a Connection String from the Project’s Configuration File
See the following help topic for more information: ConnectionName.
Implement a Custom Connection Service
See the following help topic for more information: LoadConnection(String).
Create Data Queries
An instance of the MongoDBQuery class is a query to a MongoDB database collection. You can create multiple queries for different collections. To create a query, initialize a MongoDBQuery object and set its DatabaseName and CollectionName properties.
using DevExpress.DataAccess.MongoDB;
public static DataSourceInMemoryStorage CreateDataSourceStorage() {
// ...
MongoDBQuery queryCategories = new MongoDBQuery() {
DatabaseName = "Northwind",
CollectionName = "Categories"
};
mongoDataSource.Queries.Add(queryCategories);
MongoDBQuery queryProducts = new MongoDBQuery() {
DatabaseName = "Northwind",
CollectionName = "Products"
};
mongoDataSource.Queries.Add(queryProducts);
}
To filter collection items, set the query’s FilterString property. To apply conditional filters, add QueryParameter objects to the Parameters collection.
Each query name must be unique. The CollectionName property specifies the query name. To create multiple queries for the same collection, use the Alias property to specify different query names.
using DevExpress.DataAccess.MongoDB;
using DevExpress.DataAccess;
public static DataSourceInMemoryStorage CreateDataSourceStorage() {
// ...
MongoDBQuery queryProductsFiltered = new MongoDBQuery() {
DatabaseName = "Northwind",
CollectionName = "Products",
FilterString = "[CategoryID] = ?CategoryID and [UnitPrice] > 30",
Alias = "Filtered Products"
};
var queryParam = new DevExpress.DataAccess.MongoDB.QueryParameter("CategoryID", typeof(Expression), new Expression("?CategoryID", typeof(int)));
queryProductsFiltered.Parameters.Add(queryParam);
mongoDataSource.Queries.Add(queryProductsFiltered);
}
Register the MongoDB Data Source
Use the RegisterDataSource(String, XDocument) method to register the DataSourceInMemoryStorage object in the data source storage.
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardWeb;
public static DataSourceInMemoryStorage CreateDataSourceStorage() {
// ...
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
dataSourceStorage.RegisterDataSource("mongoDataSource", mongoDataSource.SaveToXml());
return dataSourceStorage;
}
Use the Data Source Storage
Call the DashboardConfigurator.SetDataSourceStorage method and add the data source storage you configured. Handle the DashboardConfigurator.ConfigureDataConnection event. The following code uses the in-memory data source storage:
using System;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.MongoDB;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<DashboardConfigurator>((IServiceProvider serviceProvider) => {
DashboardConfigurator configurator = new DashboardConfigurator();
configurator.SetDataSourceStorage(CreateDataSourceStorage());
configurator.ConfigureDataConnection += Configurator_ConfigureDataConnection;
return configurator;
});
var app = builder.Build();
The MongoDB data source is now available in the Web Dashboard.
Users can bind dashboard items to data in the Web Dashboard’s UI.