-
Notifications
You must be signed in to change notification settings - Fork 4
Description
Hi!
So, related to #3... Sometimes I customize the property for an interface. For instance, Content Pickers or MNTP by default will be returning "IPublishedContent", but I might know that it will always be of a specific ContentType . In order to be able to call the property and get the strongly-typed model in a View, I customize the composition model and interface. Example:
Customized 'CompRelatedBlogPosts.cs'
partial interface ICompRelatedBlogPosts
{
IEnumerable<BlogPost> SelectedPosts { get; }
}
partial class CompRelatedBlogPosts
{
[ImplementPropertyType("SelectedPosts")]
public IEnumerable<BlogPost> SelectedPosts => GetSelectedPosts(this);
public static IEnumerable<BlogPost> GetSelectedPosts(ICompRelatedBlogPosts that)
{
return that.Value<IEnumerable<IPublishedContent>>("SelectedPosts").ToContentModels<BlogPost>(true);
}
}
}
Then I just add an override to set the correct type in the Models which use the Composition. Example:
partial class TextPage
{
#region Implementation of ICompRelatedBlogPosts
IEnumerable<BlogPost> ICompRelatedBlogPosts.SelectedPosts => CompRelatedBlogPosts.GetSelectedPosts(this);
#endregion
}
This works fine. However, when I re-generate the Models, the generated composition file gets the "IPublishedContent" property again:
'CompRelatedBlogPosts.generated.cs'
public partial interface ICompRelatedBlogPosts : IPublishedContent {
System.Collections.Generic.IEnumerable<global::Umbraco.Cms.Core.Models.PublishedContent.IPublishedContent> SelectedPosts { get; }
}
I can solve the issue by explicitly ignoring the property:
'GetModelsNotificationHandler.cs'
public class GetModelsNotificationHandler : INotificationHandler<GetModelsNotification>
{
public void Handle(GetModelsNotification notification)
{
foreach (TypeModel model in notification.Models)
{
...
//Content-Type specific processing
if (model.Alias == CompRelatedBlogPosts.ModelTypeAlias)
{
foreach (PropertyModel property in model.Properties)
{
if (property.Alias == "SelectedPosts")
{
property.IsIgnored = true;
}
}
}
...
}
}
}
But I wonder if the generating code can check for the customized property defined in a custom interface implementation and skip it, like it does for the regular properties?