note
VeloxDB is still in beta and APIs are subject to change. We are in the process of completing the documentation, so some sections may be incomplete or empty at this time.
Class ObjectModel
ObjectModel provides methods for querying the database and creating new objects.
Inheritance
Namespace: VeloxDB.ObjectInterface
Assembly: vlxdb.dll
Syntax
public sealed class ObjectModel : Object
Remarks
ObjectModel provides interface for working with the database. You can use it to fetch objects, create new objects and query indexes. The data provided by the ObjectModel always reflects changes made during the transaction. For example, if you were to create a new object, and then iterate through all objects you would find your newly created object.
For references IsNullable
is not enforced during the transaction. That is VeloxDB will allow allow reference with IsNullable
set to false
to be null during the execution of the database operation. This is done intentionally because sometimes you need a model to be temporarily in invalid state, for example that's the only way to create a circular reference. This check is enforced at the end of database operation, if there is reference that is not nullable with null value at the end of operation, database will rollback transaction and report an error.
VeloxDB stores data in an unmanaged memory. DatabaseObject is just a thin wrapper around this unmanaged memory structure. When you access an object, the database allocates new DatabaseObject and keeps it around for the duration of the transaction. These objects are also used to store changes to the database done during the transaction. This enables VeloxDB to always provide you with the same object no matter how you access it. This also has certain drawbacks, for example when iterating through large amounts of objects (either using GetAllObjects<T>() or by following references) you can end up allocating a lot of objects that VeloxDB will keep around until the transaction is done. If you know that you wont be applying changes to these objects, you can call Abandon() on them. This tells VeloxDB that it doesn't have to keep the reference to the object any more.
This example illustrates how to use Abandon() method.
int count = 0;
// Get cities enumerable.
IEnumerable<City> cities = om.GetAllObjects<City>();
// Iterate over cities
foreach(City city in cities)
{
// Do your business logic with city.
if(city.Name.Contains("New"))
{
count++;
}
// Abandon the object, since we wont be changing it.
city.Abandon();
}
return count;
Changes done to the objects are kept within ObjectModel. In case of large transactions, these changes can accumulate and negatively affect performance. You can use ApplyChanges() method to apply changes to the database and clear ObjectModel's internal cache's.
Methods
ApplyChanges()
Applies changes accumulated in ObjectModel to the database.
Declaration
public void ApplyChanges()
Remarks
Changes made during the transaction are kept in ObjectModel until the operation ends. These changes as they accumulate can negatively impact ObjectModel performance. ApplyChanges() allows you to apply these changes early. This will clear ObjectModel's caches and can improve performance.
note
Database must be in consistent state when ApplyChanges() is called. If there are any non nullable references that are set to null ApplyChanges() will throw an exception and transaction will be rolled back.
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | If ObjectModel has been disposed. |
CreateObject<T>()
Create a new object.
Declaration
public T CreateObject<T>()
where T : DatabaseObject
Returns
Type | Description |
---|---|
T | A new instance of requested type. |
Type Parameters
Name | Description |
---|---|
T | Type of object to create. |
Remarks
The method creates a new object, which will be committed to the database after the database operation completes.
The object is created with all properties set to default values. References are set to null
, while simple types are set to
DefaultValue if specified, and 0 if not. The Id
property is initialized and set
to object's Id. This Id
can be used to fetch the object from the database.
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | If ObjectModel has been disposed. |
System.ArgumentException | If requested type is abstract (IsAbstract is |
GetAllObjects<T>()
Retrieves all the objects of a given type (or any derived type) from the database.
Declaration
public IEnumerable<T> GetAllObjects<T>()
where T : DatabaseObject
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> |
|
Type Parameters
Name | Description |
---|---|
T | Type of objects to get. |
Remarks
When used with a base type, GetAllObjects
will return all subtypes as well.
There is no guarantee for the order of returned objects.
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | If ObjectModel has been disposed. |
System.ArgumentException | If type |
GetHashIndex<T, TKey1>(String)
Get a hash index by name.
Declaration
public HashIndexReader<T, TKey1> GetHashIndex<T, TKey1>(string name)
where T : DatabaseObject
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the hash index. |
Returns
Type | Description |
---|---|
HashIndexReader<T, TKey1> | Requested hash index |
Type Parameters
Name | Description |
---|---|
T | Type for which the hash index is requested. |
TKey1 | Type of hash index's key. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If type |
System.ObjectDisposedException | If ObjectModel has been disposed. |
See Also
GetHashIndex<T, TKey1, TKey2>(String)
Get hash index with composite key by name.
Declaration
public HashIndexReader<T, TKey1, TKey2> GetHashIndex<T, TKey1, TKey2>(string name)
where T : DatabaseObject
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the hash index. |
Returns
Type | Description |
---|---|
HashIndexReader<T, TKey1, TKey2> | Requested hash index |
Type Parameters
Name | Description |
---|---|
T | Type for which the hash index is requested. |
TKey1 | Type of hash index's first key. |
TKey2 | Type of hash index's second key. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If type |
System.ObjectDisposedException | If ObjectModel has been disposed. |
See Also
GetHashIndex<T, TKey1, TKey2, TKey3>(String)
Get hash index with composite key by name.
Declaration
public HashIndexReader<T, TKey1, TKey2, TKey3> GetHashIndex<T, TKey1, TKey2, TKey3>(string name)
where T : DatabaseObject
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the hash index. |
Returns
Type | Description |
---|---|
HashIndexReader<T, TKey1, TKey2, TKey3> | Requested hash index |
Type Parameters
Name | Description |
---|---|
T | Type for which the hash index is requested. |
TKey1 | Type of hash index's first key. |
TKey2 | Type of hash index's second key. |
TKey3 | Type of hash index's third key. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If type |
System.ObjectDisposedException | If ObjectModel has been disposed. |
See Also
GetHashIndex<T, TKey1, TKey2, TKey3, TKey4>(String)
Get hash index with composite key by name.
Declaration
public HashIndexReader<T, TKey1, TKey2, TKey3, TKey4> GetHashIndex<T, TKey1, TKey2, TKey3, TKey4>(string name)
where T : DatabaseObject
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the hash index. |
Returns
Type | Description |
---|---|
HashIndexReader<T, TKey1, TKey2, TKey3, TKey4> | Requested hash index |
Type Parameters
Name | Description |
---|---|
T | Type for which the hash index is requested. |
TKey1 | Type of hash index's first key. |
TKey2 | Type of hash index's second key. |
TKey3 | Type of hash index's third key. |
TKey4 | Type of hash index's fourth key. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If type |
System.ObjectDisposedException | If ObjectModel has been disposed. |
See Also
GetObject<T>(Int64)
Get object by id.
Declaration
public T GetObject<T>(long id)
where T : DatabaseObject
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | id | Object's Id. |
Returns
Type | Description |
---|---|
T | Returns the queried object if found, otherwise returns null. |
Type Parameters
Name | Description |
---|---|
T | The type of object to get. |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | If ObjectModel has been disposed. |
System.ArgumentException | If type |
GetObjectStrict<T>(Int64)
Get object by id. This is strict version of VeloxDB.ObjectInterface.ObjectModel.GetObject(System.Int64), the difference being that strict version throws an exception if object is not found.
Declaration
public T GetObjectStrict<T>(long id)
where T : DatabaseObject
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | id | Object's Id. |
Returns
Type | Description |
---|---|
T | Returns the queried object if found. |
Type Parameters
Name | Description |
---|---|
T | The type of object to get. |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | If ObjectModel has been disposed. |
System.ArgumentException | If object with given |
GetSortedIndex<T, TKey1>(String)
Get a sorted index by name.
Declaration
public SortedIndexReader<T, TKey1> GetSortedIndex<T, TKey1>(string name)
where T : DatabaseObject
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the sorted index. |
Returns
Type | Description |
---|---|
SortedIndexReader<T, TKey1> | Requested sorted index |
Type Parameters
Name | Description |
---|---|
T | Type for which the sorted index is requested. |
TKey1 | Type of sorted index's key. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If type |
System.ObjectDisposedException | If ObjectModel has been disposed. |
See Also
GetSortedIndex<T, TKey1, TKey2>(String)
Get sorted index with composite key by name.
Declaration
public SortedIndexReader<T, TKey1, TKey2> GetSortedIndex<T, TKey1, TKey2>(string name)
where T : DatabaseObject
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the sorted index. |
Returns
Type | Description |
---|---|
SortedIndexReader<T, TKey1, TKey2> | Requested sorted index |
Type Parameters
Name | Description |
---|---|
T | Type for which the sorted index is requested. |
TKey1 | Type of sorted index's first key. |
TKey2 | Type of sorted index's second key. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If type |
System.ObjectDisposedException | If ObjectModel has been disposed. |
See Also
GetSortedIndex<T, TKey1, TKey2, TKey3>(String)
Get sorted index with composite key by name.
Declaration
public SortedIndexReader<T, TKey1, TKey2, TKey3> GetSortedIndex<T, TKey1, TKey2, TKey3>(string name)
where T : DatabaseObject
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the sorted index. |
Returns
Type | Description |
---|---|
SortedIndexReader<T, TKey1, TKey2, TKey3> | Requested sorted index |
Type Parameters
Name | Description |
---|---|
T | Type for which the sorted index is requested. |
TKey1 | Type of sorted index's first key. |
TKey2 | Type of sorted index's second key. |
TKey3 | Type of sorted index's third key. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If type |
System.ObjectDisposedException | If ObjectModel has been disposed. |
See Also
GetSortedIndex<T, TKey1, TKey2, TKey3, TKey4>(String)
Get sorted index with composite key by name.
Declaration
public SortedIndexReader<T, TKey1, TKey2, TKey3, TKey4> GetSortedIndex<T, TKey1, TKey2, TKey3, TKey4>(string name)
where T : DatabaseObject
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the sorted index. |
Returns
Type | Description |
---|---|
SortedIndexReader<T, TKey1, TKey2, TKey3, TKey4> | Requested sorted index |
Type Parameters
Name | Description |
---|---|
T | Type for which the sorted index is requested. |
TKey1 | Type of sorted index's first key. |
TKey2 | Type of sorted index's second key. |
TKey3 | Type of sorted index's third key. |
TKey4 | Type of sorted index's fourth key. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If type |
System.ObjectDisposedException | If ObjectModel has been disposed. |
See Also
Rollback()
Rollbacks the transaction associated with this instance of ObjectModel class. ObjectModel instance is no longer usable after this.
Declaration
public void Rollback()