Search Results for

    Show / Hide Table of Contents
    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
    object
    ObjectModel
    Inherited Members
    object.GetType()
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    Namespace: VeloxDB.ObjectInterface
    Assembly: vlxdb.dll
    Syntax
    public sealed class ObjectModel

    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
    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
    ObjectDisposedException

    If ObjectModel has been disposed.

    ArgumentException

    If requested type is abstract (IsAbstract is true)
    or
    If type T is not marked with the DatabaseClassAttribute.

    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
    IEnumerable<T>

    IEnumerable of all objects of a given type in the database.

    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
    ObjectDisposedException

    If ObjectModel has been disposed.

    ArgumentException

    If type T is not marked with theDatabaseClassAttribute

    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
    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
    ArgumentException

    If type T is not marked with the DatabaseClassAttribute
    or
    if requested hash index is not found.

    ObjectDisposedException

    If ObjectModel has been disposed.

    See Also
    HashIndexAttribute
    HashIndexReader<T, TKey1>

    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
    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
    ArgumentException

    If type T is not marked with the DatabaseClassAttribute
    or
    if requested hash index is not found.

    ObjectDisposedException

    If ObjectModel has been disposed.

    See Also
    HashIndexAttribute
    HashIndexReader<T, TKey1, TKey2>

    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
    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
    ArgumentException

    If type T is not marked with the DatabaseClassAttribute
    or
    if requested hash index is not found.

    ObjectDisposedException

    If ObjectModel has been disposed.

    See Also
    HashIndexAttribute
    HashIndexReader<T, TKey1, TKey2, TKey3>

    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
    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
    ArgumentException

    If type T is not marked with the DatabaseClassAttribute
    or
    if requested hash index is not found.

    ObjectDisposedException

    If ObjectModel has been disposed.

    See Also
    HashIndexAttribute
    HashIndexReader<T, TKey1, TKey2, TKey3, TKey4>

    GetObjectStrict<T>(long)

    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
    long 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
    ObjectDisposedException

    If ObjectModel has been disposed.

    ArgumentException

    If object with given id is not found in the database.
    or
    If type T is not marked with the DatabaseClassAttribute.

    GetObject<T>(long)

    Get object by id.

    Declaration
    public T GetObject<T>(long id) where T : DatabaseObject
    Parameters
    Type Name Description
    long 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
    ObjectDisposedException

    If ObjectModel has been disposed.

    ArgumentException

    If type T is not marked with the DatabaseClassAttribute.

    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
    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
    ArgumentException

    If type T is not marked with the DatabaseClassAttribute
    or
    if requested sorted index is not found.

    ObjectDisposedException

    If ObjectModel has been disposed.

    See Also
    SortedIndexAttribute
    SortedIndexReader<T, TKey1>

    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
    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
    ArgumentException

    If type T is not marked with the DatabaseClassAttribute
    or
    if requested sorted index is not found.

    ObjectDisposedException

    If ObjectModel has been disposed.

    See Also
    SortedIndexAttribute
    SortedIndexReader<T, TKey1, TKey2>

    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
    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
    ArgumentException

    If type T is not marked with the DatabaseClassAttribute
    or
    if requested sorted index is not found.

    ObjectDisposedException

    If ObjectModel has been disposed.

    See Also
    SortedIndexAttribute
    SortedIndexReader<T, TKey1, TKey2, TKey3>

    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
    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
    ArgumentException

    If type T is not marked with the DatabaseClassAttribute
    or
    if requested sorted index is not found.

    ObjectDisposedException

    If ObjectModel has been disposed.

    See Also
    SortedIndexAttribute
    SortedIndexReader<T, TKey1, TKey2, TKey3, TKey4>

    Rollback()

    Rollbacks the transaction associated with this instance of ObjectModel class. ObjectModel instance is no longer usable after this.

    Declaration
    public void Rollback()
    In this article
    © 2025 Copyright: VeloxDB