Reference
XodContext API
Every public member of XodContext, the class you use for everything in XOD, with its exact signature.
XodContext (namespace Xod) opens a database and does all reading and writing. It implements
IDisposable; dispose it to write out buffered data and release the files.
In the signatures below, include is "*" (load every relationship) or a comma-separated list of
property names; see Relationships. lazyLoad: true skips
loading relationships of the returned object.
Constructors
| Signature | |
|---|---|
XodContext(string file, string password = null, DatabaseOptions options = null) |
Opens or creates the database at file. Holds the database exclusively until disposed; if another context or process has it open, waits LockRetryTimeoutMs, then throws DatabaseLockedException. Without StorageFormat set, existing databases write in their recorded format and new ones use the extension (see Storage and Processes). |
XodContext(IXodEngine engine) |
Wraps a custom IXodEngine implementation. IXodEngine includes DeleteWhere and DropAll members. |
Properties and events
| Member | |
|---|---|
StorageFormat StorageFormat |
The active write format for this database. |
IXodEngine Engine |
The underlying storage engine. |
event EventHandler<TriggerEventArgs> BeforeAction |
Raised before each write; can cancel it. See Events. |
event EventHandler<TriggerEventArgs> AfterAction |
Raised after each write. |
Reading
| Signature | Returns |
|---|---|
T Find<T>(Expression<Func<T, bool>> query = null, string include = "*") |
The first match, or null. |
IEnumerable<T> Query<T>(Expression<Func<T, bool>> query, string include = "*") |
Every match. Uses indexes. |
IEnumerable<T> Query<T>(T example, string include = "*") |
Rows matching the example’s set properties (text exact, numbers by value, with or without an index; see Query many). |
IEnumerable<T> Query<T>(T[] examples, string include = "*") |
Rows matching any of the examples. |
IEnumerable<T> QueryDelegate<T>(Func<T, bool> query, string include = "*") |
Every match, by scanning. |
T FindDelegate<T>(Func<T, bool> query, string include = "*") |
The first match, by scanning. |
T FirstMatch<T>(Func<T, bool> query, string include = "*") |
The first match, by scanning. |
T FindLast<T>(Func<T, bool> query = null, string include = "*") |
The last match. |
T First<T>(string include = "*") |
The first row. |
T Last<T>(string include = "*") |
The last row. |
IEnumerable<T> Select<T>(string include = "*") |
Every row, read in full before it returns. |
IEnumerable<T> Select<T>(int skip, int take, bool backward = false, string include = "*") |
A page of rows. |
IEnumerable<T> SelectBackward<T>(string include = "*") |
Every row, last first. |
int Count<T>(Func<T, bool> query = null) |
The number of rows, or of matches. |
bool Any<T>(Func<T, bool> query = null) |
Whether any row, or any match, exists. |
SqlResult Sql(string sql, params object[] args) |
Runs a SQL SELECT; ? binds args in order. See Querying with SQL. |
SqlResult Sql(string sql, IReadOnlyDictionary<string, object> args) |
Runs a SQL SELECT with @name parameters. |
IReadOnlyList<T> Sql<T>(string sql, params object[] args) |
Projects each SQL row into a new T by property name (also accepts @name dictionary parameters). |
Writing
| Signature | Returns |
|---|---|
T Insert<T>(T item, bool lazyLoad = false) |
The stored object, key filled in. |
object Insert(object item, bool lazyLoad = false) |
The same, untyped. |
object Insert(Type type, object item, bool lazyLoad = false) |
The same, for a given type. |
int InsertRange<T>(IEnumerable<T> items, bool lazyLoad = false) |
The number of rows written. One atomic write; waits for Flush() in Buffered mode (see Many at once). |
bool Update<T>(T item, UpdateFilter filter = null) |
false if no row has the key. Throws MissingPrimaryKeyValueException if a non-autonumber key part is at its type’s default (see Primary keys). |
bool Update<T>(T item, T newItem, UpdateFilter filter = null) |
Replaces the row matching item with newItem. |
bool Update(object item, UpdateFilter filter = null) |
Untyped forms; Update(Type, ...) overloads also exist. |
T InsertOrUpdate<T>(T item, bool lazyLoad = false, UpdateFilter filter = null) |
Updates the row with the same key, or inserts. |
bool Delete<T>(T item) |
false if no row has the key. Also Delete(object), Delete(Type, object). Waits for Flush() in Buffered mode. |
int DeleteWhere<T>(Func<T, bool> predicate) |
The number of rows deleted. One atomic write with predicate run inside the write; waits for Flush() in Buffered mode (see Delete). |
bool Drop<T>() |
Deletes the table. Also Drop(Type). |
bool DropAll() |
Deletes every table as one atomic write (see Drop a table). |
Async
| Signature |
|---|
Task<IReadOnlyList<T>> SelectAsync<T>(string include = "*", CancellationToken cancellationToken = default) |
Task<IReadOnlyList<T>> QueryAsync<T>(Expression<Func<T, bool>> query, string include = "*", CancellationToken cancellationToken = default) |
Task<T> FindAsync<T>(Expression<Func<T, bool>> query, string include = "*", CancellationToken cancellationToken = default) |
Task<T> InsertAsync<T>(T item, bool lazyLoad = false, CancellationToken cancellationToken = default) |
Task<bool> UpdateAsync<T>(T item, UpdateFilter filter = null, CancellationToken cancellationToken = default) |
Task<bool> DeleteAsync<T>(T item, CancellationToken cancellationToken = default) |
Transactions, security and maintenance
| Signature | |
|---|---|
IXodTransaction BeginTransaction() |
Flushes buffered writes and starts a transaction (Commit(), Rollback(), IsActive, TransactionId). Belongs to the calling flow; other threads wait for it. Throws TransactionException inside a handler. See Transactions. |
void Flush() |
Writes buffered data and indexes to disk (Durability = Buffered). |
void Secure(string password) |
Adds a password. |
void ChangePassword(string currentPassword, string newPassword) |
Changes the password. |
void Loose(string password) |
Removes the password. |
DatabaseStatistics GetStatistics() |
Sizes and counts. See Statistics. |
void RegisterType<T>(), void RegisterType(Type type) |
Loads a type’s mapping before first use (needed for types XOD can’t discover statically, such as ones named by [GenericTypeProperty]). |
IEnumerable<Type> RegisteredTypes() |
The registered types. |
void ClearCache(), void ClearCaches() |
Flushes buffered data and releases cached pages and indexes (inside a transaction, keeps pending changes in memory without flushing). |
void Dispose() |
Waits for in-flight reads and writes on other threads, flushes buffered data, and closes the database. |
Writes and maintenance calls are refused with XodException from inside a read on the same thread (such as
a query predicate). Inside a BeforeAction or AfterAction handler, Dispose() and password changes throw
InvalidOperationException, Flush()/ClearCache()/GetStatistics() throw InvalidOperationException in
buffered mode or a transaction, and BeginTransaction()/Commit()/Rollback() throw
TransactionException.
UpdateFilter
| Property | |
|---|---|
string[] Properties |
The property names the filter is about. A relation and its key property are always taken together. |
UpdateFilterBehavior Behavior |
Target: write only these. Skip: write everything except these. |
Other public types
| Type | |
|---|---|
XodMigrator |
ConvertDatabase(source, target, StorageFormat, password = null) converts a database and returns the number of files converted. Refused while the source is open or when the target folder isn’t empty. See Storage. |
CryptoHelper (Xod.Helpers) |
VerifyHash, HashProperty, plus obsolete EncryptProperty and DecryptProperty. See Security. |
DatabaseOptions |
Configuration options passed to XodContext. See DatabaseOptions. |
SqlResult |
Return value of Sql: Columns (Name, Type), Rows (object[] each) and Warnings (Code, Message). |
SqlSchemaSource |
Classes or Recorded, for DatabaseOptions.SqlSchema. |
XmlExtensions.ElementMatch, XodSecurityService, InheritedAttribute, LazyLoadFilter, ReservedPropertyType |
Obsolete (warning CS0618); kept for source compatibility only. |