Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anders Brohus 147 posts 374 karma points
    1 week ago
    Anders Brohus
    0

    Hi Our,

    Currently have an strange issue, because i can't extend my "NewsArticle" model, maybe it's me that is doing it wrong..

    Because i have an doc type named "News Article", which have inherited the doc type "News Meta", in the "News Meta" i have an heading field which i want to extend but when i regenerate my models it won't stop generating the heading field, which means i get this error,

    The type 'NewsArticle' already contains a definition for 'NewsHeading'

    The generated field looks like this,

        ///<summary>
        /// Overskrift
        ///</summary>
        [ImplementPropertyType("newsHeading")]
        public string NewsHeading
        {
            get { return ProjectName.Core.Models.NewsMeta.GetNewsHeading(this); }
        }
    

    And the code i have written looks like this,

    [ImplementPropertyType("newsHeading")]
        public string NewsHeading
        {
            get
            {
                string newsHeading = NewsMeta.GetNewsHeading(this);
                if (string.IsNullOrWhiteSpace(newsHeading))
                {
                    newsHeading = OpenGraphSharing.GetOgTitle(this);
    
                    if (string.IsNullOrWhiteSpace(newsHeading))
                    {
                        newsHeading = SEO.GetMetaTitle(this);
    
                        if (string.IsNullOrWhiteSpace(newsHeading))
                        {
                            newsHeading = this.Name;
                        }
                    }
                }
                return newsHeading;                  
            }
        }
    

    The funny part is i have done the same thing with many other thing which works, but they didn't have inherited doc types?

  • Dave Woestenborghs 3159 posts 10354 karma points MVP 3x admin c-trib
    1 week ago
    Dave Woestenborghs
    0

    Hi Andres,

    Looking at the code I think you are using a composition. Not inheritance? Is that correct.

    The news article generated class will probably implement a interface called INewsMeta.

    So if you go in the Newsmeta generated file you will see a interface INewsMeta and a class NewsMeta which implements the interface

    So what you want to do is create a partial class called NewsMeta

    In that you will add a property

     [ImplementPropertyType("newsHeading")]
        public string NewsHeading
        {
            get { return this.GetNewsHeading(this); }
        }
    

    And add a static method called GetNewsHeading

    public static string GetNewsHeading(INewsMeta that) {
    
    var newsHeading = that.GetPropertyValue<string>("newsHeading");
    
    if (string.IsNullOrWhiteSpace(newsHeading))
                {
                    newsHeading = OpenGraphSharing.GetOgTitle(that);
    
                    if (string.IsNullOrWhiteSpace(newsHeading))
                    {
                        newsHeading = SEO.GetMetaTitle(that);
    
                        if (string.IsNullOrWhiteSpace(newsHeading))
                        {
                            newsHeading = that.Name;
                        }
                    }
                }
                return newsHeading;   
    
     }
    

    After these changes regenerate your models.

    Dave

  • Anders Brohus 147 posts 374 karma points
    1 week ago
    Anders Brohus
    0

    Hi Dave :)

    Of course i'm using composition but it's friday ;)

    Will try and see if this works, thanks alot! :)

  • Anders Brohus 147 posts 374 karma points
    1 week ago
    Anders Brohus
    0

    Okay it's not working ..

    Now my NewsHeading has been removed from the generated NewsArticle .. :(

  • Dave Woestenborghs 3159 posts 10354 karma points MVP 3x admin c-trib
    1 week ago
    Dave Woestenborghs
    0

    Is the NewsArticle still implementing the INewsMeta interface ?

    What happens if you remove this from your own class

     [ImplementPropertyType("newsHeading")]
        public string NewsHeading
        {
            get { return this.GetNewsHeading(this); }
        }
    

    And only leave the GetNewsHeading

    Dave

  • Dave Woestenborghs 3159 posts 10354 karma points MVP 3x admin c-trib
    1 week ago
    Dave Woestenborghs
    100

    I just tested that on a project that is the trick.

    In your own partial class you only need to have the GetNewsHeading method.

    Than it will work.

    Dave

  • Anders Brohus 147 posts 374 karma points
    1 week ago
    Anders Brohus
    0

    Nice thanks alot, it worked! :)

Please Sign in or register to post replies

Write your reply to:

Draft