SqlModelMapper Class
.NET Standard 2.x
SqlModelMapper is a transaction-oriented data manipulation component compliant with .NET Standard. It can build models with data from multiple tables, support nested levels of relationships, and provide explicit and implicit transaction management for business entities (i.e. a top-level object and all of its children).
When SqlModelMapper is used to save data, all database operations that have been tracked are automatically included in the transaction scope to ensure data integrity, it can automatically commit all tracked data operations in one transaction to achieve data integrity.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Implements SnapObjects.Data.ISqlModelMapper
Syntax
public class SqlModelMapper : ISqlModelMapper;
Constructors
Name | Description |
---|---|
SqlModelMapper(DataContext context) | Initializes a new instance of the SqlModelMapper class by a DataContext object. |
Methods
Name | Return Type | Description |
---|---|---|
Avg<TModel>(string expression,params object[] parameters) | object | Computes the average value using the specified SQL expression for the data retrieved according to the criteria specified in a TModel class. |
ClearTracked() | void | Clears all tracked information. |
Count<TModel>(params object[] parameters) | int | Gets the number of rows in the result set retrieved according to the criteria specified in a TModel class. |
DistinctCount<TModel>(string expression,params object[] parameters) | int | Retrieves a count of all distinct values specified by the SQL expression, according to the criteria specified in a TModel class. |
Exists<TModel>(params object[] parameters) | bool | Checks whether any record exists according to the retrieval criteria specified in a TModel class. |
KeyExists<TModel>(params object[] parameters) | bool | Checks whether data exists when retrieved according to the primary key defined in a TModel class. |
Load<TModel>(params object[] parameters) | ILoadable<TModel> | Retrieves data according to the SQL query defined in a TModel class. |
Load<TModel>(ISqlQueryBuilder queryBuilder, params object[] parameters) | ILoadable<TModel> | Retrieves data according to the SQL query defined in the SqlQueryBuilder instance. |
LoadAll<TModel>() | ILoadable<TModel> | Retrieves data from the database according to the SQL query (without Where Clause) defined by the primary table in a TModel class. |
LoadAllByPage<TModel>(int currentIndex, int pageSize) | ILoadable<TModel> | Retrieves data for the specified page according to SQL query (without Where Clause) defined by the primary table in a TModel class. |
LoadByKey<TModel>(params object[] parameters) | ILoadable<TModel> | Retrieves data according to the primary key defined in a TModel class. |
LoadByPage<TModel>(int currentIndex, int pageSize, params object[] parameters) | ILoadable<TModel> | Retrieves data for the specified page according to SQL query defined in a TModel class. |
LoadEmbedded<TModel>(TModel model, params object[] parameters) | IEmbeddedLoadable<TModel> | Obtains the IEmbeddedLoadable<TModel> interface which can be used to load the embedded property of a TModel instance. |
Max<TModel>(string expression, params object[] parameters) | object | Gets the maximum value for the specified SQL expression according to the retrieval criteria specified in a TModel class. |
Min<TModel>(string expression, params object[] parameters) | object | Gets the minimum value for the specified SQL expression according to the retrieval criteria specified in a Model class. |
RemoveTrackedModel<TModel>(TModel model) | bool | Clears all tracked information from a model. |
SaveChanges() | IDbResult | Updates the database by executing the data changes tracked by a SqlModelMapper instance. |
Scalar<TModel, TValue>(string expression, params object[] parameters) | TValue | Retrieves data of the first column in the first row for the specified SQL expression according to the criteria specified in a TModel class. |
ScalarByKey<TModel, TValue>(string expression, params object[] parameters) | TValue | Retrieves data of the first column in the first row for the specified SQL expression according to the primary key in a TModel class. |
Sum | object | Computes the sum of the value using the specified SQL expression for the data retrieved according to the criteria specified in a TModel class. |
Track(Action<ISaveContext> saveAction) | ISqlModelMapper | Tracks an Action<ISaveContext> object. |
Track<TModel>(IModelEntry<TModel> modelEntry) | TModel | Tracks a ModelEntry<TModel> instance including data and status. |
Track<TModel>(IModelEntry<TModel> modelEntry, Action<ISaveContext> afterSaveAction) | TModel | Tracks a ModelEntry<TModel> instance including data and status, and calls an Action<ISaveContext> object after updating the specified database successfully. |
TrackCreate<TModel>(TModel model) | ISqlModelMapper | Tracks a TModel instance. Used for adding a record to the database. |
TrackCreate<TModel>(TModel model, Action<ISaveContext> afterSaveAction) | ISqlModelMapper | Tracks a TModel instance, used for adding a record to the database and calling an Action<ISaveContext> object after adding the record. |
TrackCreateRange<TModel>(IEnumerable<TModel> models) | ISqlModelMapper | Tracks a collection of the specified TModel type. Used for adding one or multiple records to a database. |
TrackDelete<TModel>(TModel model) | ISqlModelMapper | Tracks a TModel instance. Used for deleting the database record that corresponds to the TModel object. |
TrackDelete<TModel>(TModel model, Action<ISaveContext> afterSaveAction) | ISqlModelMapper | Tracks a TModel instance. Used for deleting the database record corresponding to the TModel object and calling an Action<ISaveContext> object after saving the data and before committing to the database. |
TrackDeleteByKey<TModel>(params object[] parameters) | ISqlModelMapper | Tracks the data that is deleted according to the primary key in TModel . Used for finally deleting the database records. |
TrackDeleteRange<TModel>(IEnumerable<TModel> models) | ISqlModelMapper | Tracks a collection of the specified TModel type. Used for deleting one or multiple records in a database. |
TrackMaster<TModel>(IModelEntry<TModel> modelEntry) | IDetailTracker<TModel> | Tracks a ModelEntry<TModel> instance which includes the data and status of the master Model, when working with multiple Models which have master-detail relationships. Used for updating the data of the master Model in the database. |
TrackMaster<TModel>(IModelEntry<TModel> modelEntry, Action<ISaveContext> afterSaveAction) | IDetailTracker<TModel> | Tracks a ModelEntry<TModel> instance which includes the data and status of the master Model, when working with multiple Models which have master-detail relationships. Used for updating the data of the master Model in the database and calling an Action<ISaveContext> object after saving the data and before committing to the database. |
TrackRange<TModel>(IEnumerable<IModelEntry<TModel>> modelEntries) | IList<TModel> | Tracks a collection of ModelEntries with the specified TModel type. |
TrackSqlBuilder(ISqlInsertBuilder insertBuilder, params object[] parameters) | ISqlModelMapper | Tracks an instance which has implemented the ISqlInsertBuilder interface and passes in the parameter(s) required by the instance for the constructed SQL. Used for finally inserting the record into the database. |
TrackSqlBuilder(ISqlDeleteBuilder deleteBuilder, params object[] parameters) | ISqlModelMapper | Tracks an instance which has implemented the ISqlDeleteBuilder interface and passes in the parameter(s) required by the instance for the constructed SQL. Used for finally deleting the record in the database. |
TrackSqlBuilder<TModel>(ISqlUpdateBuilder updateBuilder, params object[] parameters) | ISqlModelMapper | Tracks an instance which has implemented the ISqlUpdateBuilder interface and passes in the parameter(s) required by the instance for the constructed SQL. Used for finally updating the record in the database. |
TrackSqlCUD(string sqlText, params object[] parameters) | ISqlModelMapper | Tracks an INSERT, UPDATE, or DELETE SQL statement. |
TrackUpdate<TModel>(TModel model) | ISqlModelMapper | Tracks a TModel instance. Used for updating the database record corresponding to the TModel instance. |
TrackUpdate<TModel>(TModel model, Action<ISaveContext> afterSaveAction) | ISqlModelMapper | Tracks a TModel instance. Used for updating the database record corresponding to the TModel instance and calling an Action<ISaveContext> object after saving the data and before committing to the database. |
TrackUpdate<TModel>(TModel originalModel, TModel modifiedModel) | ISqlModelMapper | Tracks a database table update operation. The data that will be updated to the database table is cached in the TModel object. |
TrackUpdate<TModel>(TModel originalModel, TModel modifiedModel, Action<ISaveContext> afterSaveAction) | ISqlModelMapper | Tracks a database table update operation. The data that will be updated to the database table is cached in the TModel object. |
TrackUpdateRange<TModel>(IEnumerable<TModel> models) | ISqlModelMapper | Tracks a collection of the specified TModel type. Used for updating one or multiple records to a database. |
ValidateTrackedModels() | bool | Performs validity check to all of the data tracked by SqlModelMapper. |
ValidateTrackedModels(out List<ValidationResult> ValidationResults) | bool | Performs validity check to all of the data tracked by SqlModelMapper and gets the validation result. |
Remarks
SqlModelMapper is an easy-to-use data manipulation component, which purpose is mapping objects between the database tables and the C# classes. It provides flexible interfaces for manipulating the database. Developer can easily manage the object relationships and cascading operations with simple coding, and perform various operations to the database with almost no need to write SQL statements.
Tracking
SqlModelMapper provides a variety of Track methods. When the Track method is executed, the database operations to be executed are recorded, but not actually performed against the database. When calling multiple methods in SqlModelMapper for tracking, the order of tracking determines the order in which the IModeMapper.SaveChanges
method performs database operations, all these operations will be in the same transaction scope. When subsequent database operations depend on the execution result of a proceeding operation, the Track method can be used, the developer can use the Action<ISaveAction>
parameter to receive and pass along the context when the data is saved. After the IModeMapper.SaveChanges
method executes the database operations, all of the tracked operations will be restored to the initial state.