Skip to main content

Business Object - Register Types

  • 4 minutes to read

This topic describes how to make object types available in the Report Wizard and Data Source Wizard, and allow users to create an Object Data Source.

View Example: Register Types for the Report Wizard and Data Source Wizard

Declare Data Types

  • Create a class for use as a data source.

    • Declare data fields in the class. These fields fetch data when users select Entire Object in the Report Wizard and Data Source Wizard.
    • Declare methods that return IEnumerable<T> values. Users can select one of these methods in the Report Wizard and Data Source Wizard to fetch data for reports.

The Report Wizard and Data Source Wizard use class namespace and name to list types. Apply the DisplayName attribute to a class to specify a custom display name.

DisplayName is Not Applied DisplayName is Applied
DisplayName - Before DisplayName - After
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace ASPNetCore.SampleObjectTypes {
    [DisplayName("My Data Source")]
    public class DataSource {
        List<DataItem> data = new List<DataItem>();
        public DataSource() : this(100) { }
        public DataSource(int count) : this(count, 1000) { }
        public DataSource(int count, int maxValue) {
            var random = new Random();
            for(var i = 0; i < count; i++) {
                data.Add(new DataItem() {
                    Random = random.Next(0, maxValue)
                });
            }
        }
        public List<DataItem> GetData() {
            return data;
        }

        public List<DataItem> GetData(int count) {
            return data.GetRange(0, count);
        }
    }

    public class DataSource2 : DataSource {
        public DataSource2() : base(1000, 1000) { }
    }
    public class DataItem {
        public int Random { get; set; }
    }
}

Register the Declared Types

Implement the IObjectDataSourceWizardTypeProvider interface. Return the type list in the GetAvailableTypes(String) method.

using System;
using System.Collections.Generic;
using DevExpress.DataAccess.Web;
// ...
public class CustomObjectDataSourceWizardTypeProvider : IObjectDataSourceWizardTypeProvider {
    public IEnumerable<Type> GetAvailableTypes(string context) {
        return new[] {
            typeof(ASPNetCore.SampleObjectTypes.DataSource),
            typeof(ASPNetCore.SampleObjectTypes.DataSource2)
        };
    }
}

Call the RegisterObjectDataSourceWizardTypeProvider<T>() method at the application startup to register the class that implements the interface. This applies the type list in the Report Wizard and Data Source Wizard.

using DevExpress.DataAccess.Web;
using ASPNetCore.Services;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);

builder.Services.ConfigureReportingServices(configurator => {
    configurator.ConfigureReportDesigner(designerConfigurator => {
        designerConfigurator.RegisterObjectDataSourceWizardTypeProvider<CustomObjectDataSourceWizardTypeProvider>();
    });
});

var app = builder.Build();

list-types

Filter Type Constructors

The Report Wizard and Data Source Wizard list all constructors declared in the registered types and allow users to choose any constructor to create an object data source. Implement the IObjectDataSourceConstructorFilterService interface to filter constructors.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DevExpress.DataAccess.Web;
// ...
public class CustomObjectDataSourceConstructorFilterService : IObjectDataSourceConstructorFilterService {
    public IEnumerable<ConstructorInfo> Filter(Type dataSourceType, IEnumerable<ConstructorInfo> constructors) {
        if(dataSourceType == typeof(ASPNetCore.SampleObjectTypes.DataSource2)) {
            return constructors;
        }
        return constructors.Where(x => x.GetParameters().Length > 0);
    }
}

Call the RegisterObjectDataSourceConstructorFilterService<T>() method at the application startup file to register the class that filters constructors.

using DevExpress.DataAccess.Web;
using ASPNetCore.Services;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);

builder.Services.ConfigureReportingServices(configurator => {
    configurator.ConfigureReportDesigner(designerConfigurator => {
        designerConfigurator.RegisterObjectDataSourceConstructorFilterService<CustomObjectDataSourceConstructorFilterService>();
    });
});

var app = builder.Build();
Constructors are Not Filtered Constructors are Filtered
Filter-Constructors-Before Filter-Constructors-After

Filter Type Members

The Report Wizard and Data Source Wizard list all IEnumerable<T> members declared in the registered types and allow users to choose any member to supply data. Implement the IObjectDataSourceMemberFilterService interface to filter members.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DevExpress.DataAccess.Web;
// ...
public class CustomObjectDataSourceMemberFilterService : IObjectDataSourceMemberFilterService {
    public IEnumerable<MemberInfo> Filter(Type dataSourceType, IEnumerable<MemberInfo> members) {
        if(dataSourceType == typeof(ASPNetCore.SampleObjectTypes.DataSource2)) {
            return members;
        }
        return members.Where(x => {
            var method = x as MethodInfo;
            if(method != null) return method.GetParameters().Length > 0;
            return false;
        });
    }
}

Call the RegisterObjectDataSourceMemberFilterService<T>() method at the application’s startup to register the class that filters members.

using DevExpress.DataAccess.Web;
using ASPNetCore.Services;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);

builder.Services.ConfigureReportingServices(configurator => {
    configurator.ConfigureReportDesigner(designerConfigurator => {
        designerConfigurator.RegisterObjectDataSourceMemberFilterService<CustomObjectDataSourceMemberFilterService>();
    });
});

var app = builder.Build();
Members are Not Filtered Members are Filtered
Filter-Members-Before Filter-Members-After

Next Steps

Bind Reports to Data - Business Object

You can also review the following example:

View Example: How to Create a Report Bound to the ObjectDataSource with Constructor Parameters Linked to Report Parameters