Grailsは、リクエストパラメータをドメインオブジェクトとその関連付けにバインドするための非常に優れたサポートを備えています。.id
これは主に、データベースで終わるリクエストパラメータを検出し、それらをデータベースから自動的にロードすることに依存しています。
ただし、コマンドオブジェクトの関連付けを設定する方法は明確ではありません。次の例を見てください。
class ProductCommand {
String name
Collection<AttributeTypeCommand> attributeTypes
ProductTypeCommand productType
}
このオブジェクトには、とのシングルエンドの関連付けと。ProductTypeCommand
との多端の関連付けがありAttributeTypeCommand
ます。すべての属性タイプと製品タイプのリストは、このインターフェースの実装から入手できます。
interface ProductAdminService {
Collection<AttributeTypeCommand> listAttributeTypes();
Collection<ProductTypeCommand> getProductTypes();
}
このインターフェイスを使用して、GSPの製品および属性タイプの選択リストにデータを入力します。また、このインターフェイスをコマンドオブジェクトに依存性注入し、それを使用してコマンドオブジェクトのプロパティを「シミュレート」attributeTypes
します。productType
class ProductCommand {
ProductAdminService productAdminService
String name
List<Integer> attributeTypeIds = []
Integer productTypeId
void setProductType(ProductTypeCommand productType) {
this.productTypeId = productType.id
}
ProductTypeCommand getProductType() {
productAdminService.productTypes.find {it.id == productTypeId}
}
Collection<AttributeTypeCommand> getAttributeTypes() {
attributeTypeIds.collect {id ->
productAdminService.getAttributeType(id)
}
}
void setAttributeTypes(Collection<AttributeTypeCommand> attributeTypes) {
this.attributeTypeIds = attributeTypes.collect {it.id}
}
}
実際に発生するのはattributeTypeIds
、プロパティが関連するリクエストパラメータにバインドされ、ゲッター/セッターがプロパティをproductTypeId
「シミュレート」することです。コマンドオブジェクトの関連付けを設定する簡単な方法はありますか?productType
attributeTypes