Filtering Data

NHibernate provides an innovative approach to handling data with "visibility" rules. A NHibernate filter is a global, named, parameterized filter that can be enabled or disabled for a particular NHibernate session.

Entity Developer for NHibernate provides the possibility to define filter criteria and assign filters to classes and collections. A filter criterion is a restriction clause very similar to the existing "where" attribute available for the class and various collection elements. Furthermore, filter conditions can be parameterized. The application can then decide at runtime whether the filters should be enabled and what their parameter values should be. Filters can be used like database views, but parameterized inside the application.

Prior to using a filter, the following steps should be performed:

The filter must be defined;
The filter must be assigned to the appropriate mapping objects.

This example illustrates the process of work with filters in Entity Developer for NHibernate.

For the purpose of this example, a model is created having two entities connected with an association:

images_FilterModel

 

ExpandedToggleIcon        Creating a Filter

To edit the filters list of the model, select the model (root) node in the Model Explorer, or click on an empty diagram area to display the properties of the model. In the Properties docking window find the Filters parameter and click the three-dotted button next to it.

For the purpose of this example, 2 filters are created.

1.DepartmentFilter for assigning it to the Department entity and filtering by the Location field when retrieving the instances of the Department entity:

images_FiltersEditor

The parameter declared for the filter is as follows:

images_FilterParametersEditor

2.EmployeesFilter for assigning it to the Employees collection navigation property of the Department entity and filtering by the Salary field when retrieving the instances of the Employee entity:

images_FiltersEditor2

The parameter declared for the filter is as follows:

images_FilterParametersEditor2

A default condition can be defined for a filter, it will work for filtering in case there is no particular condition defined for an entity or a collection. It is possible to define the default condition for this simple example here, but it will be defined further in this section when considering the means of assigning filters to certain objects of the model.

ExpandedToggleIcon        Assigning a Filter

To assign a filter to the Department entity, perform the following steps:

1.Select the required class.
2.In the Properties docking window of the entity find the Filter parameter and select the required filter from the drop-down list.
3.Set the filter condition to be applied for retrieving the instances of the Department entity when the filter is enabled:

images_FilterConditionOnClass

To assign a filter to the Employees collection navigation property of the Department entity, perform the following steps:

1.Select the required navigation property.
2.In the Properties docking window of the navigation property find the Filter parameter and select the required filter from the drop-down list.
3.Set the filter condition to be applied for retrieving the instances of the Employee entity for the Employees collection navigation property of the Department entity when the filter is enabled:

images_FilterConditionOnRelationProperty

 

 


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.