Skip to main content

Read

info

This documentation page is heavily under construction. Various information may be missing. Please report any feedback here.

Cognibase offers a comprehensive set of methods allowing users to seamlessly fetch collections of DataItems through the Client Object Manager (COM) from the Server Object Manager (SOM). These methods are designed to accommodate various requirements and scenarios, ranging from fetching specific item types to advanced use cases that include loading references and using validators.

Read a single Data Item

Fetch a single item after creating a DataItemKey instance.

DataItemKey key = DataItemKey.Get<YourDataItemType>("primary-key-value");
var item = COM.ReadDataItem(key) as YourDataItemType;

Reading Collections Based on Type

Fetch all DataItems based on their type.

ICollection<DataItem> items = COM.ReadDataItems(typeof(YourDataItemType));

Including Subclasses in the Fetch

Read DataItems of a particular type, along with those of its subclasses.

ICollection<DataItem> items = COM.ReadDataItems(typeof(YourDataItemType), true);

Force Loading the DataItems

Read items and force loading all properties.

ICollection<DataItem> forceLoadedItems = COM.ReadDataItems(typeof(YourDataItemType), true, true);

Loading with References

Load DataItems alongside their references for a more comprehensive data fetch.

ICollection<DataItem> itemsWithReferences = COM.ReadDataItems(typeof(YourDataItemType), true, true, true);

Reading with Query Validators

In many traditional systems, querying data often revolves around string-based SQL queries. In Cognibase, this paradigm shifts towards an object-oriented approach. Validators in Cognibase serve as encapsulations of these queries, allowing for targeted data retrieval based on specific conditions, presented in an object-oriented manner.

Consider you want to extract items from a generic DataItemType where a TargetField equals a specific value. Here's a generalized routine to formulate an object-oriented query using a Validator:

// Step 1: Identify and access the target field.
var targetField = DataItem.GetClassInfo(typeof(DataItemType)).GetField(nameof(DataItemType.TargetField));

// Step 2: Set the condition for the target field.
var whereExpression = new WhereComparisonExpression(targetField, WhereOperator.Equal, value);

// Step 3: Initialize the validator with the condition.
var customValidator = new DataItemCollectionValidator<DataItemType>(true) as IDataItemCollectionValidator<DataItemType>;
customValidator.QueryExpression = whereExpression;
customValidator.IsForceLoader = true;

// Step 4: Read the data using the validator.
ICollection<YourDataItemType> validatedItems = COM.ReadDataItems<YourDataItemType>(customValidator);