-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonDataSourceConfigurator.cs
43 lines (37 loc) · 2.63 KB
/
JsonDataSourceConfigurator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.Json;
using System;
using System.Web.Hosting;
namespace MvcDashboardDataSources.Configuration {
public class JsonDataSourceConfigurator {
public static void ConfigureDataSource(DashboardConfigurator configurator, DataSourceInMemoryStorage storage) {
// Registers a JSON data source from URL.
DashboardJsonDataSource jsonDataSourceUrl = new DashboardJsonDataSource("JSON Data Source (URL)");
jsonDataSourceUrl.JsonSource = new UriJsonSource(new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"));
jsonDataSourceUrl.RootElement = "Customers";
jsonDataSourceUrl.Fill();
storage.RegisterDataSource("jsonDataSourceUrl", jsonDataSourceUrl.SaveToXml());
// Registers a JSON data source from a JSON file.
DashboardJsonDataSource jsonDataSourceFile = new DashboardJsonDataSource("JSON Data Source (File)");
jsonDataSourceFile.ConnectionName = "jsonConnection";
jsonDataSourceFile.RootElement = "Customers";
storage.RegisterDataSource("jsonDataSourceFile", jsonDataSourceFile.SaveToXml());
// Registers a JSON data source from JSON string.
DashboardJsonDataSource jsonDataSourceString = new DashboardJsonDataSource("JSON Data Source (String)");
string json = "{\"Customers\":[{\"Id\":\"ALFKI\",\"CompanyName\":\"Alfreds Futterkiste\",\"ContactName\":\"Maria Anders\",\"ContactTitle\":\"Sales Representative\",\"Address\":\"Obere Str. 57\",\"City\":\"Berlin\",\"PostalCode\":\"12209\",\"Country\":\"Germany\",\"Phone\":\"030-0074321\",\"Fax\":\"030-0076545\"}],\"ResponseStatus\":{}}";
jsonDataSourceString.JsonSource = new CustomJsonSource(json);
jsonDataSourceString.RootElement = "Customers";
storage.RegisterDataSource("jsonDataSourceString", jsonDataSourceString.SaveToXml());
configurator.ConfigureDataConnection += Configurator_ConfigureDataConnection;
}
private static void Configurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
if (e.ConnectionName == "jsonConnection") {
Uri fileUri = new Uri(HostingEnvironment.MapPath(@"~/App_Data/customers.json"), UriKind.RelativeOrAbsolute);
JsonSourceConnectionParameters jsonParams = new JsonSourceConnectionParameters();
jsonParams.JsonSource = new UriJsonSource(fileUri);
e.ConnectionParameters = jsonParams;
}
}
}
}