WCF RIA Services は、作成時にこの状況を念頭に置いていました。1 つのリクエストと 1 つのデータベース トランザクションですべてを簡単に実行できSubmitChangesます (DB や ORM によって異なります)。ただし、オブジェクト (POCO、EF など) に関する詳細情報を提供すると、より適切な回答が得られます。
とは言っても、サーバー上で定義されているオブジェクトについて大まかな推測を行います。
public class Product
{
[Key]
public int? ProductID { get; set; }
// ... more properties ...
[Association("Product-ProductDollars", "ProductID", "ProductID", IsForeignKey = false)]
[Include]
[Composition]
public ICollection<ProductDollar> ProductDollars { get; set; }
}
public class ProductDollar
{
[Key]
public int? ProductDollarID { get; set; }
public int? ProductID { get; set; }
// ... more properties ...
[Association("Product-ProductDollars", "ProductID", "ProductID", IsForeignKey = true)]
[Include]
public Product Product { get; set; }
}
そして、あなたの DomainService は次のようになります
public class ProductDomainService : DomainService
{
public IQueryable<Product> GetProducts()
{
// Get data from the DB
}
public void InsertProduct(Product product)
{
// Insert the Product into the database
// Depending on how your objects get in the DB, the ProductID will be set
// and later returned to the client
}
public void InsertProductDollar(ProductDollar productDollar)
{
// Insert the ProductDollar in the DB
}
// Leaving out the Update and Delete methods
}
これで、クライアントに、これらのエンティティを作成および追加するコードができました。
var context = new ProductDomainContext();
var product = new Product();
context.Products.Add(product);
product.ProductDollars.Add(new ProductDollar());
product.ProductDollars.Add(new ProductDollar());
context.SubmitChanges();
これにより、1 つの要求が に送信されDomainServiceます。ただし、WCF RIAChangeSetは、3 つの挿入を含むこれをDomainServiceメソッドへの 3 つの呼び出しに分割します。
InsertProduct(Product product)
InsertProductDollar(ProductDollar productDollar)
InsertProductDollar(ProductDollar productDollar)
1 回のトランザクションですべての挿入をDomainService実行すると、ORM で ProductID を正しく管理できます。