-
Notifications
You must be signed in to change notification settings - Fork 9
Architecture of data access
Data is one of the most important parts of an application. To leverage working with it, Kephas provides an abstract data infrastructure which allows data manipulation (CRUD), validation and support for various behaviors.
All data operations are performed through a data context. The data context is responsible for holding and managing a local cache of data and for instantiating commands. The commands are actually the performers of data operations, integrate data behaviors, and are tightly coupled to the data context that created them.
Data contexts are created by a data context factory, which is a shared application service, by providing a data store name. The factory uses configured data store information and associated services to get it and to initialize the data context.
The data context is the entry point in regard to data operations. Its main responsibilities are:
-
Query the data and retrieve the results. There are no restrictions regarding the implementation of the data querying, so it's up to the implementation to use, for example, a first or a second level cache. All the information controlling how the querying should be performed may be specified in the
queryOperationContextparameter, which is an expando that can be customized freely. -
Create the commands. As explained in the following, the strategy for creating the commands will choose the most appropriate one for the calling data context.
-
Provide entity information. This is information attached to each entity in the internal cache regarding its ID, change state, original data, and many more.
-
Attach and detach entities. These operations add/remove entities to/from the internal cache.
Apart from these, the data context:
-
is a contextual object: the consumers may access the ambient services and use the data context as an expando object, dynamically adding and accessing values to it/from it.
-
is initializable and disposable: implements the
IInitializableandIDisposableinterfaces to control its lifetime.
Data contexts are dependent on initialization data, which typically contains at least data store connection information. Due to the fact that multiple physical data stores may be served by the same data context implementation and that by design, at one time, a data context instance may be connected to a single physical data store, there must be a factory service that creates a data context for a given connection. This is the data context factory application service.
-
CreateDataContext(dataStoreName, [initializationContext]): IDataContext: This is the only method of the factory service which creates a data context instance and initializes it.
- dataStoreName: indicates the name of the data store. This identifies typically an entry in the configuration where connection information and other initialization data is provided.
- initializationContext: provides contextual information for data context initialization.
Note: The consumer of the CreateDataContext method takes the full responsibility of disposing correctly the received data context.
The data commands extend the data context functionality beyond the minimalist one defined by the IDataContext interface. Kephas provides built-in commands for the following data operations:
- Create entity
- Delete entity
- Persist changes
- Discard changes
- Find by ID
- Find by criteria
These operations provide the basic infrastructure for data manipulation, but for some scenarios this might not be enough. For example, update commands fired against all entities matching a specific criteria, calling stored procedures, or supporting all kinds of upsert variations are specific cases where there is no built-in support. With Kephas, the developer is free to define as many commands as needed, tailored for specific needs and data stores of all kinds. For a step-by-step tutorial, check the data commands page.