Skip to content

Methods/parameters on partial classes not inherited #147

Open
@harvzor

Description

@harvzor

In Umbraco I have Page document type which inherit from an SEO document type.

My SEO document type has a field called Page Title which the user can use to overwrite the page's Name on the front end.

ModelsBuilder generates something like this:

// Autogenerated:
namespace Umbraco.Web.PublishedContentModels
{
    public partial interface ISEO : IPublishedContent
    {
        string PageTitle { get; }
    }

    public partial class SEO : PublishedContentModel, ISEO
    {
        public string PageTitle
        {
            get { return GetPageTitle(this); }
        }
    }
}

ModelsBuilder also generates some code for my Page:

// Autogenerated:
namespace Umbraco.Web.PublishedContentModels
{
    public partial class Page : PublishedContentModel, ISEO
    {
        ...
    }
}

I can use this property on a view:

<!-- Where page is of type ContentModels.Page. -->
<h1>@(page.PageTitle.IsNullOrWhiteSpace() ? page.Name : page.PageTitle)</h1>

However, I decide that doing this on multiple pages isn't super DRY. I decide to add to the SEO partial class:

// Custom:
namespace Umbraco.Web.PublishedContentModels
{
    public partial class SEO : PublishedContentModel, ISEO
    {
        public string GetPageTitle()
        {
            return PageTitle.IsNullOrWhiteSpace() ? Name : PageTitle;
        }
    }
}

However, this property isn't on the Page content model, because that model inherits from ISEO and not the SEO model.

This forces me to do this:

<!-- Where page is of type ContentModels.Page. -->
@{
    var seo = new ContentModels.SEO(page);
}
<h1>@(seo.GetPageTitle())</h1>

I'd much rather the GetPageTitle() method was by default available on my Page. I could put the method onto the ISEO interface and then create custom partial classes for all models which inherit from ISEO but this is still not very DRY.

Is there a solution for this?

I can see something similar here: #40 (comment)

But I tried implementing your example and the auto generated items did not automatically implement my method.

P.S. Thanks for the great work with this. ModelsBuilder improves my life!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions