メリット
シンプルなコーディング
高度な機能のコーディングが簡単で、データアクセスはメンテナンスコストを最小限に抑えるため、モデルに含まれています。
タイプセーフなクエリ
.NETオブジェクトは、タイプセーフでクエリ条件として使用され、SQLを検証するテスト用APIも提供されています。
柔軟なSQL生成
SQLの生成は、モデル属性の包括的なセットを介して制御しますが、必要に応じてSQLを直接コーディングできます。
柔軟なリレーション
リレーションシップは事前定義ではなくコーディングで定義するため、特定クエリ内でのみ有効です。
トランザクション指向
クエリ、アップデート、アクションを追跡でき、トランザクション管理が自動で適用されます。
超高速パフォーマンス
ADO.NET上のオーバーヘッドはほとんどなく、クエリ、アップデート、アクションは一括で実行されます。
コードスニペット
- Model
- Retrieve
- Delete
- Update
- Transaction
複雑なSQLクエリの生成は、モデルに適用される直感的な属性によって制御されます。
namespace Appeon.SqlModelMapperDemo.Models { //Generates the underlying SQL query according to the attributes [Top(1000)] [SqlParameter("custId", typeof(int))] [SqlParameter("stratOrderDate", typeof(DateTime))] [SqlParameter("endOrderDate", typeof(DateTime))] [Table("SalesOrderHeader",Schema = "Sales")] [SqlWhere("(CustomerId = :custId) and (Orderdate Between :stratOrderDate and :endOrderDate)")] public class SalesOrder { [Key] [Identity] public int SalesOrderID { get; set; } [Required] public DateTime? OrderDate { get; set; } [Required] public byte? Status { get; set; } [Required] public bool? OnlineOrderFlag { get; set; } [SqlCompute("(isnull(N'SO'+CONVERT([nvarchar](23),[SalesOrderID]),N'*** ERROR ***'))")] public string SalesOrderNumber { get; set; } [Required] public int? CustomerID { get; set; } [SqlCompute("(isnull(([SubTotal]+[TaxAmt])+[Freight],(0)))")] public decimal? TotalDue { get; set; } public DateTime? ModifiedDate { get; set; } //Nests a model and applies various attributes [JsonIgnore] [SetValue("$SalesOrderID", "$SalesOrderID", SetValueStrategy.Always)] [ModelEmbedded(typeof(SalesOrderDetail), ParamValue = "$SalesOrderID", CascadeCreate =true, CascadeDelete = true)] public IListOrderDetails { get; set; } } }
モデルへのデータ取得とロードには、パラメーターのみが必要です。非生産的なクエリ言語は必要ありません。
//Retrieves multiple rows of data, loads it into the model, and then puts it into a list _context.SqlModelMapper.Load(startDate, endDate, customerId) .ToList(); //Retrieves a single row of data and loads it into the model _context.SqlModelMapper.LoadByKey (orderId) .FirstOrDefault();
データ(主従関係データを含む)を削除するには、キーまたはモデルのみが必要です。
//Deletes data corresponding with the specified key – either a single row of data or if a nested model then the cascaded data _context.SqlModelMapper.TrackDeleteByKey(orderId) .SaveChanges() // Deletes data corresponding with the specified model – either a single row of data or if a nested model then the cascaded data _context.SqlModelMapper.TrackDelete(salesOrder) .SaveChanges()
データの保存は直感的なトラッキング機能によって制御され、主従関係のリレーションシップ全体または(適切なTrackDetailを含める/除外することにより)その一部の保存を詳細に制御できます。
//Saves data with a master-detail-detail relationship _context.SqlModelMapper.TrackMaster(header) .TrackDetails(o => o.SalesReasons, reasons) .TrackDetails(o => o.Details, details); _context.SqlModelMapper.SaveChanges();
トランザクション管理は、強力な暗黙的な機能で簡単になります。包括的なトラッキング機能(モデルだけの追跡ではありません)が、対象を決定します。
//Tracks various models, raw SQL, and actions _context.SqlModelMapper.TrackCreate(salesOrder) .Track((saveContext) => { //C# code block …… }) //The C# code block will be executed when SaveChanges is called .TrackUpdate(salesPerson) .TrackSqlCUD(rawSql) .SaveChanges() //Executes all tracked items in one transaction and automatically commits or rolls back
機能
Model
Modelは、データベースのカラムをマッピングし、関連するSQLを含んでいます。 ModelのSQLは、さまざまな属性に基づいて生成されるため、開発者はSQLを制御することができます。 また、ModelはModel自体をネストすることにより、親-子-孫などの複雑な関係を表すことができます。
SQLModelMapper
SQLModelMapperは、トランザクション指向のデータ操作コンポーネントです。 データベースのCRUD操作をシンプルにするオブジェクトとメソッドを提供し、トラッキングする項目に自動トランザクション管理を適用します。 また、トラッキングアイテムはパフォーマンスを向上させるために一括で実行します。
クエリ
クエリを実行し、結果をテンポラリオブジェクトにロードして処理するか、計算された結果を返します。
集計&スカラーロード
モデルで定義されたクエリを実行し、集計またはスカラー計算とともに結果を返します。
トラッキング
トランザクション管理の目的として、モデルの変更、SQL、アクションをトラッキングします。
実行
モデル、SQL、アクションでトラッキングしたすべてのデータベース操作をデータベースへ一括送信し、ModelMapperにトランザクションを管理させます。
変更の保存
トラッキングアイテム(モデルの変更、SQL、アクション)の実行により、データベースへのすべての変更を保存します。トラッキングアイテムは、パフォーマンスを向上させるために一括で実行します。
非同期
CRUD操作を非同期で実行します。