|
DataSmith 0.1.2
|
The DataContext is the central entry point for all database operations within the DataSmith framework.
It acts as a unit-of-work that manages database connections, provides access to generated models and queries, and coordinates data operations in a consistent and efficient manner.
A DataContext instance represents an active session with the database. All reads, writes, and transactions should be performed through this object.
The Generator Tool produces strongly-typed accessors that attach to the DataContext, allowing you to interact with your database using compile-time safe APIs instead of raw SQL.
The DataContext is responsible for:
Typically, a DataContext is created when database access begins and disposed when the operation completes.
Depending on your application architecture, it may be:
The Generator Tool creates properties on the DataContext that expose database entities and queries.
Example:
These properties provide type-safe operations such as:
The DataContext can coordinate multiple operations within a single database transaction to ensure atomicity.
If a failure occurs before commit, all changes are rolled back.
The DataContext handles opening and closing the database connection automatically.
Manual connection handling is typically unnecessary and discouraged, as improper usage can lead to resource leaks or inconsistent state.
DataContext instances are generally not thread-safe.
Each thread or task should use its own instance unless explicitly documented otherwise.
Create a new DataContext when: