Updating PropertyList item values

PLACE FOR BLOG

Updating PropertyList item values

Updating PropertyList item values

marija

A few days ago a colleague of mine was banging his head over why a very simple code that updates the value of an item in a PropertyList doesn't work. The symptoms were annoying: no errors, but no update either. Cache wasn't an issue either. This is what we came up with together to solve it.

For the purpose of this blogpost, I've installed an AlloyTech and upgraded it to latest library versions. I've also used a very simple PropertyList I copy pasted from here. (directly inheriting from PropertyList).

The code that doesn't work is the following (part of ContentInitialization class that subscribes to ContentPublishing event):

        if (e.Content is StartPage startPage)
        {
            startPage.Contacts.First().Age = 222; // well, maybe his code wasn't doing exactly this.
        }

Instead, we figured a list of new contacts should be created from the old list and assigned to startPage.Contacts.

Not sure what my colleague ended up with after refactoring, but now I've played around a bit and created a very simple extension for cloning:

    public static class CloningExtensions
    {
        public static IList Clone(this IList list) where T : ICloneable
        {
            return list.Select(i => (T)i.Clone()).ToList();
        }
    }

And implemented ICloneable on a Contact model:

    public class Contact : ICloneable
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int PhoneNumber { get; set; }
        public object Clone()
        {
            return new Contact
            {
                Age = Age,
                Name = Name,
                PhoneNumber = PhoneNumber
            };
        }
    }

The ContentInitialization code now becomes:

        if (e.Content is StartPage startPage)
        {
            var contacts = startPage.Contacts.Clone();
            contacts.First().Age = 222;
            startPage.Contacts = contacts;
        }

Please note that this code hasn't been tested well, clearly it would suffer from null reference exceptions.

When comparing IList with a ContentArea, one would not have the same situation with a ContentArea, since an area either adds or removes items, but updating is not an issue (since then one would update those content items instead, not the page containing the area). I would still expect this to work, but there may be something that I haven't thought of that makes this a feature, not a bug ¯_(ツ)_/¯.

Comments

no avatar

Henrik Nystrom SAYS:

Jan 07, 2019 at 05.10 am

So while PropertyList doesn't officially support complex objects due to time constraints in combination with what we would expect such support would include (Import/Export/Data resilience/etc), it does have some support for the scenario you are describing. They way this is supported is similar to how you have done, but instead of using the ICloneable interface, you can implement IReadOnly and/or IModifiedTrackable on the list item class. In this case you can update the item directly when the list and items are in writable mode or create a writable clone of your item and replace the item you are in read-only mode.

LEAVE A COMMENT