IObjectDataSourceCustomFillService Interface
Namespace: DevExpress.DashboardCommon
Assembly: DevExpress.Dashboard.v18.2.Core.dll
Declaration
public interface IObjectDataSourceCustomFillService
Public Interface IObjectDataSourceCustomFillService
The following members return IObjectDataSourceCustomFillService objects:
To provide a custom fill service for the DashboardObjectDataSource, implement the IObjectDataSourceCustomFillService.GetData method, which accepts the data source and specified parameters used to fill this data source.
Example
This example shows how to bind the ASP.NET MVC Dashboard extension to the Object Data Source and supply it with data using a custom fill service by implementing a IObjectDataSourceCustomFillService
interface.
You can use the custom fill service (IObjectDataSourceCustomFillService
to get access to the DashboardObjectDataSource data (data fields used in a dashboard, the filter expression, parameters and their actual values) and use it in the current data query.
The IObjectDataSourceCustomFillService
is executed if you use typeof() to specify the ObjectDataSource.DataSource property.
In this example, the ObjectDataSourceFillParameters.DataFields parameter is used to load data only for required fields.
using System.Web.Mvc;
namespace MVCxDashboard_CustomFillService.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
}
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DashboardWeb.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Web.Routing;
public class DashboardConfig {
public static void RegisterService(RouteCollection routes) {
routes.MapDashboardRoute();
DashboardConfigurator.Default.SetDashboardStorage(new DashboardFileStorage(@"~/App_Data/"));
var dataSourceStorage = new DataSourceInMemoryStorage();
DashboardConfigurator.Default.SetDataSourceStorage(dataSourceStorage);
DashboardObjectDataSource objDataSource = new DashboardObjectDataSource("Object Data Source", typeof(SalesPersonData));
objDataSource.DataSource = typeof(SalesPersonData);
dataSourceStorage.RegisterDataSource("objDataSource", objDataSource.SaveToXml());
DashboardConfigurator.Default.SetObjectDataSourceCustomFillService(new CustomObjectDataSourceCustomFillService());
}
}
public class SalesPersonData {
public string SalesPerson { get; set; }
public int Quantity { get; set; }
}
public class CustomObjectDataSourceCustomFillService : IObjectDataSourceCustomFillService {
public object GetData(DashboardObjectDataSource dataSource, ObjectDataSourceFillParameters fillParameters) {
if (fillParameters.DataFields == null || fillParameters.DataFields.Length == 0) {
return null;
}
List<SalesPersonData> data = DataGenerator.CreateSourceData();
DataTable table = new DataTable();
foreach(string field in fillParameters.DataFields) {
table.Columns.Add(field);
}
for (int i = 0; i < data.Count; i++) {
object[] row = new object[fillParameters.DataFields.Length];
for (int j = 0; j < fillParameters.DataFields.Length; j++) {
row[j] = GetPropertyValue(data[i], fillParameters.DataFields[j]);
}
table.Rows.Add(row);
}
return table.DefaultView;
}
object GetPropertyValue(SalesPersonData obj, string propName) {
return propName == "SalesPerson" ? (object)obj.SalesPerson : (object)obj.Quantity;
}
}
public static class DataGenerator {
public static List<SalesPersonData> CreateSourceData() {
List<SalesPersonData> data = new List<SalesPersonData>();
string[] salesPersons = { "Andrew Fuller", "Michael Suyama",
"Robert King", "Nancy Davolio",
"Margaret Peacock", "Laura Callahan",
"Steven Buchanan", "Janet Leverling" };
for (int i = 0; i < 100; i++) {
SalesPersonData record = new SalesPersonData();
int seed = (int)DateTime.Now.Ticks & 0x0000FFFF;
record.SalesPerson = salesPersons[new Random(seed).Next(0, salesPersons.Length)];
record.Quantity = new Random(seed).Next(0, 100);
data.Add(record);
Thread.Sleep(3);
}
return data;
}
}
using System;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
namespace MVCxDashboard_CustomFillService {
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication {
protected void Application_Start() {
DashboardConfig.RegisterService(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();
DevExpress.Web.ASPxWebControl.CallbackError += Application_Error;
}
protected void Application_Error(object sender, EventArgs e) {
Exception exception = System.Web.HttpContext.Current.Server.GetLastError();
//TODO: Handle Exception
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MVCxDashboard_CustomFillService {
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
routes.MapRoute(
name: "Default", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
}
}
using System.Web;
using System.Web.Mvc;
namespace MVCxDashboard_CustomFillService {
public class FilterConfig {
public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
filters.Add(new HandleErrorAttribute());
}
}
}
Imports System.Web
Imports System.Web.Mvc
Namespace MVCxDashboard_CustomFillService
Public Class FilterConfig
Public Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
filters.Add(New HandleErrorAttribute())
End Sub
End Class
End Namespace
Imports System.Web.Mvc
Namespace MVCxDashboard_CustomFillService.Controllers
Public Class HomeController
Inherits Controller
' GET: Home
Public Function Index() As ActionResult
Return View()
End Function
End Class
End Namespace
Imports System
Imports System.Web.Http
Imports System.Web.Mvc
Imports System.Web.Routing
Namespace MVCxDashboard_CustomFillService
' Note: For instructions on enabling IIS6 or IIS7 classic mode,
' visit http://go.microsoft.com/?LinkId=9394801
Public Class MvcApplication
Inherits System.Web.HttpApplication
Protected Sub Application_Start()
DashboardConfig.RegisterService(RouteTable.Routes)
AreaRegistration.RegisterAllAreas()
GlobalConfiguration.Configure(AddressOf WebApiConfig.Register)
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
RouteConfig.RegisterRoutes(RouteTable.Routes)
ModelBinders.Binders.DefaultBinder = New DevExpress.Web.Mvc.DevExpressEditorsBinder()
AddHandler DevExpress.Web.ASPxWebControl.CallbackError, AddressOf Application_Error
End Sub
Protected Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim exception As Exception = System.Web.HttpContext.Current.Server.GetLastError()
'TODO: Handle Exception
End Sub
End Class
End Namespace
Imports System.Threading
Imports System.Web.Routing
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports DevExpress.DashboardWeb.Mvc
Public Class DashboardConfig
Public Shared Sub RegisterService(ByVal routes As RouteCollection)
routes.MapDashboardRoute()
DashboardConfigurator.Default.SetDashboardStorage(New DashboardFileStorage("~/App_Data/"))
Dim dataSourceStorage = New DataSourceInMemoryStorage()
DashboardConfigurator.Default.SetDataSourceStorage(dataSourceStorage)
Dim objDataSource As New DashboardObjectDataSource("Object Data Source", GetType(SalesPersonData))
objDataSource.DataSource = GetType(SalesPersonData)
dataSourceStorage.RegisterDataSource("objDataSource", objDataSource.SaveToXml())
DashboardConfigurator.Default.SetObjectDataSourceCustomFillService(New CustomObjectDataSourceCustomFillService())
End Sub
End Class
Public Class SalesPersonData
Public Property SalesPerson() As String
Public Property Quantity() As Integer
End Class
Public Class CustomObjectDataSourceCustomFillService
Implements IObjectDataSourceCustomFillService
Public Function GetData(ByVal dataSource As DashboardObjectDataSource, ByVal fillParameters As ObjectDataSourceFillParameters) As Object _
Implements IObjectDataSourceCustomFillService.GetData
If fillParameters.DataFields Is Nothing OrElse fillParameters.DataFields.Length = 0 Then
Return Nothing
End If
Dim data As List(Of SalesPersonData) = DataGenerator.CreateSourceData()
Dim table As New DataTable()
For Each field As String In fillParameters.DataFields
table.Columns.Add(field)
Next field
For i As Integer = 0 To data.Count - 1
Dim row(fillParameters.DataFields.Length - 1) As Object
For j As Integer = 0 To fillParameters.DataFields.Length - 1
row(j) = GetPropertyValue(data(i), fillParameters.DataFields(j))
Next j
table.Rows.Add(row)
Next i
Return table.DefaultView
End Function
Private Function GetPropertyValue(ByVal obj As SalesPersonData, ByVal propName As String) As Object
Return If(propName = "SalesPerson", DirectCast(obj.SalesPerson, Object), DirectCast(obj.Quantity, Object))
End Function
End Class
Public NotInheritable Class DataGenerator
Private Sub New()
End Sub
Public Shared Function CreateSourceData() As List(Of SalesPersonData)
Dim data As New List(Of SalesPersonData)()
Dim salesPersons() As String = {"Andrew Fuller", "Michael Suyama",
"Robert King", "Nancy Davolio",
"Margaret Peacock", "Laura Callahan",
"Steven Buchanan", "Janet Leverling"}
For i As Integer = 0 To 99
Dim record As New SalesPersonData()
Dim seed As Long = CLng(Date.Now.Ticks) And &HFFFF
record.SalesPerson = salesPersons((New Random(seed)).Next(0, salesPersons.Length))
record.Quantity = (New Random(seed)).Next(0, 100)
data.Add(record)
Thread.Sleep(3)
Next i
Return data
End Function
End Class
Imports System.Web.Mvc
Imports System.Web.Routing
Namespace MVCxDashboard_CustomFillService
Public Class RouteConfig
Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.IgnoreRoute("{resource}.ashx/{*pathInfo}")
routes.MapRoute(name:="Default", url:="{controller}/{action}/{id}", defaults:=New With {
Key .controller = "Home",
Key .action = "Index",
Key .id = UrlParameter.Optional
})
End Sub
End Class
End Namespace
See Also