Updating without trashing associations

by benl

Monorail provides a handy means of binding parameters to controllers from several different sources including: querystring, posted items etc via the DataBind attribute. However, if you’re attempting to bind an entity with associations you’ll quickly find that the associations aren’t loaded as part of the usual binding process.

To ensure your associations are loaded as part of the binding process – with the caveat that you’re using ActiveRecord based persistence you must use the ARDataBind attribute. It really is as simple as specifying your binding preference and this attribute and the associations are loaded by magic… Well not exactly magic, but by the ActiveRecordMediator. Example:

public void Save([ARDataBind("product", AutoLoad = AutoLoadBehavior.NewInstanceIfInvalidKey)] Product product)

Advertisements