IConnectionProviderFactory Interface
Provides methods for creating a provider to restore a data connection on deserializing a report.
Namespace: DevExpress.DataAccess.Web
Assembly: DevExpress.DataAccess.v22.1.dll
NuGet Package: DevExpress.DataAccess
Declaration
Remarks
The following code implements the IConnectionProviderFactory
and IConnectionProviderService
services that translate a connection name (serialized with the report) to a data connection.
using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.Data.Entity;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Native;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.Web;
using DevExpress.DataAccess.Wizard.Services;
using AspNetCoreSample.Data;
// ...
public class CustomSqlDataConnectionProviderFactory : IConnectionProviderFactory {
private readonly ReportDbContext reportDbContext;
public CustomSqlDataConnectionProviderFactory(ReportDbContext reportDbContext) {
this.reportDbContext = reportDbContext;
}
public IConnectionProviderService Create() {
return new CustomSqlConnectionProviderService(reportDbContext.SqlDataConnections.ToList());
}
}
public class CustomSqlConnectionProviderService : IConnectionProviderService {
readonly IEnumerable<DataConnection> sqlDataConnections;
public CustomSqlConnectionProviderService(IEnumerable<DataConnection> sqlDataConnections) {
this.sqlDataConnections = sqlDataConnections;
}
public SqlDataConnection LoadConnection(string connectionName) {
var sqlDataConnectionData = sqlDataConnections.FirstOrDefault(x => x.Name == connectionName);
if(sqlDataConnectionData == null)
throw new InvalidOperationException();
var connectionStringInfo = new ConnectionStringInfo { RunTimeConnectionString = sqlDataConnectionData.ConnectionString, ProviderName = "SQLite" };
if(string.IsNullOrEmpty(sqlDataConnectionData.ConnectionString)
|| !AppConfigHelper.TryCreateSqlConnectionParameters(connectionStringInfo, out DataConnectionParametersBase connectionParameters)
|| connectionParameters == null) {
throw new KeyNotFoundException($"Connection string '{connectionName}' not found.");
}
return new SqlDataConnection(connectionName, connectionParameters);
}
}
The ReportDbContext
class contains connection strings:
using System.Linq;
using Microsoft.EntityFrameworkCore;
// ...
public class SqlDataConnectionDescription : DataConnection { }
public class JsonDataConnectionDescription : DataConnection { }
public abstract class DataConnection {
public int Id { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public string ConnectionString { get; set; }
}
// ...
public class ReportDbContext : DbContext {
public DbSet<JsonDataConnectionDescription> JsonDataConnections { get; set; }
public DbSet<SqlDataConnectionDescription> SqlDataConnections { get; set; }
// ...
public ReportDbContext(DbContextOptions<ReportDbContext> options) : base(options) {
}
public void InitializeDatabase() {
Database.EnsureCreated();
var nwindJsonDataConnectionName = "NWindProductsJson";
if(!JsonDataConnections.Any(x => x.Name == nwindJsonDataConnectionName)) {
var newData = new JsonDataConnectionDescription {
Name = nwindJsonDataConnectionName,
DisplayName = "Northwind Products (JSON)",
ConnectionString = "Uri=Data/nwind.json"
};
JsonDataConnections.Add(newData);
}
var nwindSqlDataConnectionName = "NWindConnectionString";
if(!SqlDataConnections.Any(x => x.Name == nwindSqlDataConnectionName)) {
var newData = new SqlDataConnectionDescription {
Name = nwindSqlDataConnectionName,
DisplayName = "Northwind Data Connection",
ConnectionString = "XpoProvider=SQLite;Data Source=|DataDirectory|/Data/nwind.db"
};
SqlDataConnections.Add(newData);
}
var reportsDataConnectionName = "ReportsDataSqlite";
if(!SqlDataConnections.Any(x => x.Name == reportsDataConnectionName)) {
var newData = new SqlDataConnectionDescription {
Name = reportsDataConnectionName,
DisplayName = "Reports Data (Demo)",
ConnectionString = "XpoProvider=SQLite;Data Source=|DataDirectory|/Data/reportsData.db"
};
SqlDataConnections.Add(newData);
}
SaveChanges();
}
}
See the following help topic for a working example of an application created from a DevExpress template: Create an ASP.NET Core Application with a Report Designer.
See Also