MongoDBDataSource.FillAsync(IEnumerable<IParameter>, CancellationToken) Method
Asynchronously loads data from a MongoDB instance to MongoDBDataSource. Allows you to specify external parameters that you can reference in a query’s filter string and a cancellation token that you can use to cancel the data loading task.
Namespace: DevExpress.DataAccess.MongoDB
Assembly: DevExpress.DataAccess.v24.2.dll
Declaration
public Task FillAsync(
IEnumerable<IParameter> sourceParameters,
CancellationToken cancellationToken
)
Parameters
Name | Type | Description |
---|---|---|
sourceParameters | IEnumerable<IParameter> | A collection of parameters. |
cancellationToken | CancellationToken | A cancellation token. |
Returns
Type | Description |
---|---|
Task | A task that loads data from a MongoDB instance to the data source. |
Remarks
A MongoDB data source stores queries to database collections of a MongoDB instance. Call the FillAsync method to execute these queries and asynchronously load data from the instance to the data source. You can also use the Fill() method if you need to load the data in the application’s main thread.
Example
The example below demonstrates how to use the MongoDBDataSource class to bind an application or component to a MongoDB instance. The example uses the MongoDBCustomConnectionParameters class to specify a connection string to the MongoDB instance and the MongoDBQuery class to specify data queries to the Products collection of the Northwind database. The example shows how to use the FillAsync method to asynchronously populate the data source with data from the instance.
using DevExpress.DataAccess;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.MongoDB;
using DevExpress.DataAccess.ObjectBinding;
using System.Threading;
// ...
// Create an object that stores a MongoDB connection string.
var connectionParameters = new MongoDBCustomConnectionParameters() {
ConnectionString = "mongodb://localhost:27017"
};
// Specify queries to MongoDB database collections.
var paramPrice = new QueryParameter() {
Name = "price",
Type = typeof(Expression),
Value = new Expression("?priceExternal")
};
var queryProducts = new MongoDBQuery() {
DatabaseName = "Northwind",
CollectionName = "Products",
FilterString = "[UnitPrice] >= ?price",
Parameters = { paramPrice }
};
// Create a MongoDBDataSource object. Assign the created connection
// string and queries to the data source's ConnectionParameters and
// Queries properties.
var mongoDBDataSource = new MongoDBDataSource() {
ConnectionParameters = connectionParameters,
Queries = { queryProducts },
};
// Create an external parameter that you want to reference in the query's filter string.
var paramPriceExternal = new Parameter() {
Name = "priceExternal",
Type = typeof(int),
Value = 50
};
// Specify an array of external parameters.
var parameters = new[] { paramPriceExternal };
// Specify an array of query names. Only the queries with these
// names are executed after the FillAsync method is called.
var queriesToFill = new[] { "Products" };
// Create a CancellationTokenSource object. Call the token's
// Cancel method if you want to cancel the data loading task.
var cancellationTokenSource = new CancellationTokenSource();
// Call the data source's FillAsync method to execute queries
// and asynchronously load data from the MongoDB instance.
await mongoDBDataSource.FillAsync(parameters, queriesToFill, cancellationTokenSource.Token);
// Use the created object as a data source in your application or component.
//...