Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to implement the ICustomFunctionOperatorFormattable interface

$
0
0

This example is a modified version of the E207 example. Starting from version 9.2 we implemented the ICustomFunctionOperatorFormattable interface. Now, there is no necessity to create a custom connection provider to implement custom functions. All that you need is to create a class implementing the ICustomFunctionOperatorFormattable interface.

The ICustomFunctionOperatorFormattable interface defines following members:

Name - should return the name of the custom function. This name should be passed as a first operand to a FunctionOperator constructor:

[C#]
FunctionOperatorop=newFunctionOperator(FunctionOperatorType.Custom,"MyFunction",operandA,operandB);// orCriteriaOperatorop=CriteriaOperator.Parse("custom('MyFunction', ?, ?)",operandA,operandB);
[VB.NET]
Dim op AsNew FunctionOperator(FunctionOperatorType.Custom, "MyFunction", operandA, operandB)' orDim op As CriteriaOperator = CriteriaOperator.Parse("custom('MyFunction', ?, ?)", operandA, operandB)

ResultType - should return the type of the result value.

Format - this method should return the SQL command. The providerType parameter allows you to provide the different implementation for each SQL provider, if they use different SQL syntax for the operation you want to implement.

Evaluate - this method allows you to provide the client side implementation of the custom function.

Please note that you should register the custom function operator using the ConnectionProviderSql.RegisterCustomFunction method, and add it to the XPDictionary.CustomFunctionOperators collection.

Remark:
The code above demonstrates the old syntax: "custom('MyFunction', ?, ?)"
Starting from version 10.2, a more intuitive syntax can be used: "MyFunction(?, ?)"

In addition to the ICustomFunctionOperatorFormattable interface, the custom function can implement the ICustomFunctionOperatorQueryable interface. This function can be used in LINQ to XPO queries.

The ICustomFunctionOperatorQueryable interface defines one method: GetMethodInfo. This method should return the MethodInfo instance that will be used to find the custom function in the dictionary. The method name should match the custom function name, because LINQ to XPO will search the custom function using the method name. Use of the custom function in the LINQ to XPO query is shown below:

[C#]
varlist=fromoinnewXPQuery<Order>(session)whereo.OrderName=="Chai"selectnew{Month=GetMonthFunction.GetMonth(o.OrderDate)};
[VB.NET]
'This code snippet uses implicit typing. You will need to set 'Option Infer On' in the VB file or set 'Option Infer' at the project level:Dim list = _From o InNew XPQuery(OfOrder)(session) _Where o.OrderName = "Chai" _SelectNewWith {Key .Month = GetMonthFunction.GetMonth(o.OrderDate)}

It is necessary to register custom functions using the ConnectionProviderSql.RegisterCustomFunctionOperator method, and add them to the XPDictionary.CustomFunctionOperators collection.

See Also:
How to: Implement a Custom Criteria Language Operator 
How to: Implement Custom Functions and Criteria in LINQ to XPO



Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>