-
Notifications
You must be signed in to change notification settings - Fork 6
Conventions
Most of your model binding can be done with convention based mapping. Automapping is an easy way to get started with Vault, as it's easy to map your properties to your objects. It's purely by convention.
[UmbracoEntity(AutoMap = true)]
public class BlogEntryViewModel
{
public string Title { get; set; }
public DateTime PostDate { get; set; }
public string Content { get; set; } //Will not render macros
public Image PostImage { get; set; } //Will not get mapped since alias doesn't match
public bool Featured { get; set; }
}
Note: AutoMap is defaulted to false, so it is important to set the UmbracoEntity(AutoMap=true)
setting if you wish to use convention based configuration
Property names in an Umbraco view model object should typically map to the alias of the property. For example, consider the following Document Type for a blog post:
[UmbracoEntity(AutoMap = true)]
public class BlogEntryViewModel
{
public string Title { get; set; }
public DateTime PostDate { get; set; }
public string Content { get; set; } //Will not render macros
public Image PostImage { get; set; } //Will not get mapped since alias doesn't match
public bool Featured { get; set; }
}
It doesn't matter if the casing of your properties don't match the alias exaclty. Vault performs a case insensitive search when it looks for properties to map to.
If you need to map to a specific property name (for example, PostImage
above) you can do this by explicitly declaring the Umbraco property. You may also want to set this as a standard in your projects to explicitly map all properties such that you can create properties that don't get mapped.
To do this, decorate specific properties with the UmbracoVault.Attributes.UmbracoProperty
attribute.
[UmbracoEntity(AutoMap = true)]
public class BlogEntryViewModel
{
...
[UmbracoProperty(Alias = "image")]
public Image PostImage { get; set; } //Will get mapped since alias doesn't match
...
}
If you are using [UmbracoEntity(AutoMap = true)]
, you may wish to Ignore specific properties from being set. You can do this by setting [UmbracoIgnoreProperty]
attribute on specific properties you don't want Vault to touch.
[UmbracoEntity(AutoMap = true)]
public class BlogEntryViewModel
{
...
//This property won't be touched by Vault
[UmbracoIgnoreProperty]
public string CustomPropertySetByCustomCode { get; set; }
...
}
Many of Vault's conventions can be circumvented as needed. As mentioned in the code comments above, we need to apply some special functionality to the Content block in order to ensure the Umbraco macros render correctly. TO do this, we need to give Vault some hints that this property uses the TinyMCE rich text editor. We do this by applying the UmbracoRichTextProperty
attribute.
[UmbracoEntity(AutoMap = true)]
public class BlogEntryViewModel
{
...
[UmbracoRichTextProperty]
public string Content { get; set; } //Will CORRECTLY render macros
...
}
Since Media items are treated differently than documents in Umbraco, we need to inform vault to treat them differently as well. First, we need to create our Media objects to map to:
[UmbracoMediaEntity(AutoMap = true)]
public class UmbracoImage
{
[UmbracoProperty(Alias = "umbracoFile")]
public string UmbracoFile { get; set; }
public string Alt { get; set; }
}
Now we just have to set the property:
[UmbracoEntity(AutoMap = true)]
public class BlogEntryViewModel
{
...
[UmbracoProperty(Alias = "image")]
public Image PostImage { get; set; } //W get mapped since alias doesn't match
...
}