This project is read-only.

Saving a GUID Property of a ContentItem to the DB

Topics: Developer Forum, Project Management Forum, User Forum
Feb 10, 2009 at 8:38 PM
I have an RSS Content Item which represents an RSS article. Each article needs a GUID assigned when it is created, but that same GUID needs to be retrieved everytime the article is accessed. In other words I need to store this GUID in the N2 DB.

Currently I have an RSSId property in the RSSContentItem which looks like this:

        public virtual string RSSId 
        {
            get { return (string)(GetDetail("RSSId") ?? Guid.NewGuid().ToString()); }
            set { SetDetail("RSSId", value, Guid.NewGuid().ToString()); }
        }

I have also tried

        public virtual string RSSId 
        {
            get
            {
                string result = (string)GetDetail("RSSId");
                if (string.IsNullOrEmpty(result))
                {
                    result = Guid.NewGuid().ToString();
                    SetDetail("RSSId", result);
                }
                return result;
            }
        }

Every time I access an article the GUID is different. It doens't appear to be storing the RSSId in the database, how can I acheive this?

Thanks
Daniel
Feb 10, 2009 at 9:46 PM
Can't see why that fails. I added some tests near the end of this file. Can you spot anything we're doing differently?
Feb 10, 2009 at 9:59 PM
I am not really sure I understand your tests, and how they apply to my RSS Content Item.

Although, I did find a solution that worked for me.

Here is how I changed the Accessor to save to the database.

        public virtual string RSSId
        {
            get
            {
                string result = (string)GetDetail("RSSId");
                if (string.IsNullOrEmpty(result))
                {
                    result = Guid.NewGuid().ToString();
                    this["RSSId"] = result;
                    N2.Context.Persister.Save(this);
                }
                return result;
            }
        }

There are 2 main differences. First and most noticable is that I make a call to Persister.Save(this). That is really the trick. I figured out that I needed to call this, because the UpdateItem procedure in the AbstractEditableAttribute absract class returns a bool. I found all the places where this function is called in the source, and tracked it to the EditManager. Here the return value of UpdateItem is checked, if it succeeded then Persister.Save is called. Since my RSSId property doesn't use any Editor decorators the UpdateItem call was never being made in the EditManager, therefor never being saved to the database. So this call to the persister saves it.

The second difference is that I use this["RSSId"] to set the value.

I hope this helps anyone who might want to do something similar.

Thanks
Daniel