Implementing Custom Functions
- 2 minutes to read
This document explains how you can implement custom functions for CriteriaOperator-based criteria used in filter conditions and expressions supported in many DevExpress products (including grids, pivot grids, Reporting, etc.) in addition to built-in functions (see Criteria Language Syntax and FunctionOperatorType).
Declaring Custom Functions
A custom function is a class implementing the following interfaces.
- ICustomFunctionOperator - Declares the base functionality of a custom function.
- ICustomFunctionOperatorBrowsable - Provides additional information (a function category, function description, parameter count, etc.) on a custom function for Expression Editors. Implementing this interface exposes a custom function in Expression Editors, allowing end-users to use it in expressions along with built-in functions.
- ICustomFunctionOperatorFormattable - Defines server-side processing (implementation) of a custom function. Implementing this interface allows you to use a custom function to build query criteria in our ORM tool - XPO.
The ICustomFunctionOperatorFormattable and ICustomFunctionOperatorBrowsable interfaces inherit from the ICustomFunctionOperator interface, and extend it with a specific advanced functionality. Thus, you can implement ICustomFunctionOperatorFormattable or ICustomFunctionOperatorBrowsable (or both of them, depending on the capabilities you want to offer with your custom function) and as a result provide an ICustomFunctionOperator implementation.
Refer to the How to: Implement a Custom Criteria Language Function Operator for a sample ICustomFunctionOperatorFormattable interface implementation (a custom function that returns the month of a date).
Registering Custom Functions
Once a custom function is declared, you can register it in your application using any of the following methods.
These static methods significantly simplify the registration process. With a single method call, you make custom functions available wherever CriteriaOperator-based criteria are processed - whether it is an expression or a database query built with XPO.
Pass a custom function to a data store provider's RegisterCustomFunctionOperator method and add it to a dictionary's XPDictionary.CustomFunctionOperators collection.
This is an outdated registration technique, which exposes the custom function only to XPO queries based on dictionary entries.
To unregister a custom function, call the CriteriaOperator.UnregisterCustomFunction method.
To see a code example on implementing custom functions, refer to the How to: Implement a Custom Criteria Language Function Operator topic in the XPO documentation.
To learn how to implement custom functions and criteria, and use them in LINQ expressions built with XPO, see How to: Implement Custom Functions and Criteria in LINQ to XPO.