Copied to clipboard

Flag this post as spam?

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


  • Damian Green 452 posts 1433 karma points
    Aug 05, 2014 @ 16:33
    Damian Green
    0

    Strange Property Initialisation Problem with Published Content Model and property delegeate

    Something completely baffling me here.

    I am using PublishedContentModel and have a doctype using it.

    On the doc type i have 2 properties. One property checks the other property and decides what to return.

    i.e. 
    ShowSubDocs (bool)
    MenuProp
     - if (ShowSubDocs)
       - return children
    
    otherwise return some other menu structure
    

    The exact same code is running in dev and live. I have cleared the cache and republished the doc in question and the whole site yet the ShowSubDocs is always returning false on the live site.

    The last piece of the jigsaw is i used a bit of code i got from Morten's gist ( https://gist.github.com/sitereactor/6487186 )to get the property names by convention via a property delegate. (ill put a code dump below)

    If i use the name of the property instead of using the delegate it works ok. But when using the delegate it seems to be accessing the property before its been initialised * WHEN ACCESSING FROM THE CLASS * I say this because if i output the property on the view the correct value of true is returned.

    here is the class in question (with mortens property delegate example) :

     public class Base : PublishedContentModel, IBase
        {
            #region Property Helpers
    
            protected readonly Func<MethodBase> Property = MethodBase.GetCurrentMethod;
    
            protected T Resolve<T>(MethodBase methodBase)
            {
                var propertyTypeAlias = methodBase.ToUmbracoAlias();
                return Resolve<T>(propertyTypeAlias);
            }
    
            protected T Resolve<T>(string propertyTypeAlias)
            {
                return this.GetPropertyValue<T>(propertyTypeAlias);
            }
    
            protected T Resolve<T>(MethodBase methodBase, T ifCannotConvert)
            {
                var propertyTypeAlias = methodBase.ToUmbracoAlias();
                return Resolve<T>(propertyTypeAlias, ifCannotConvert);
            }
    
            protected T Resolve<T>(string propertyTypeAlias, T ifCannotConvert)
            {
                return this.GetPropertyValue<T>(propertyTypeAlias, false, ifCannotConvert);
            }
    
            protected T Resolve<T>(MethodBase methodBase, bool recursive, T ifCannotConvert)
            {
                var propertyTypeAlias = methodBase.ToUmbracoAlias();
                return Resolve<T>(propertyTypeAlias, recursive, ifCannotConvert);
            }
    
            protected T Resolve<T>(string propertyTypeAlias, bool recursive, T ifCannotConvert)
            {
                return this.GetPropertyValue<T>(propertyTypeAlias, recursive, ifCannotConvert);
            }
    
            #endregion
    
            public Base(IPublishedContent content) : base(content)
            {
            }
    }
    

    And this is my class that is showing the issue. I have removed surplus code to make it easier to read. This class inherits Base.

    public class Service : StandardContentPage
        {
            public Service(IPublishedContent content) : base(content)
            {
    
            }
    
            public bool ShowAllServicesInMenu
            {
                get { return Resolve<bool>(Property()); }
            }
             public bool ShowAllServicesInMenu2
            {
                get { return Resolve<bool>("ShowAllServicesInMenu"); }
            }
    
            private bool xx = false;
    
            public bool XX
            {
                get { return xx; }
            }
    
            public List<Service> ServiceMenu
            {
                get
                {
                    xx = ShowAllServicesInMenu;
    
                    if (ShowAllServicesInMenu)
                        return this.Children<Service>().ToList();
    
                    //Get the items from the menu
                    //
                    var items = Resolve<IEnumerable<IPublishedContent>>(Property());
    
                    return items.Select(x => new Service(x)).ToList();
                }
            }
        }
    

    The var XX was added to test the variable. And this shows true on dev and false on live. However if i access the menu properyt before accessing the XX propery then it shows true.

    Definitely seems to be a concurrency issue or something.

    ShowAllServicesInMenu2 - is the same property but uses a propert yname rather than delegate and always returns the right value when used in the class as shown above.

    Before i go and rip out the delegate and go back to good old typing the field name is there something going on here that is obvious and im missing it! But even more baffling is why the exact same code is behaving differently on live and dev!

    Thanks Damian

Please Sign in or register to post replies

Write your reply to:

Draft