Temporary properties on an in memory Umbraco document.
Hi All,
I have some current code that currently creates a list of Umbraco document objects, what I would like to do it to set a flag on some of these objects before they are added to the list so that I can access that flag later in my code.
I do not want to update the actual documents, this is just on a per-request basis.
var resources = new List<Document>();
while (sqlReader.Read())
{
var resourceItem = new Document(sqlReader.GetInt("id"));
resources.Add(resourceItem);
}
You can see from this code snippet currently I have a data set with a list of ID's and I am cycling through that list grabbing the document and adding it to the resource List.
I am trying to add a "viewed" flag to this document ( which is also in the sqlReader data. )
This code is already quite deeply embedded in a live solution so a complete re-write in the timescale I have (2 days) is out of the question!
You wont be able to add another property to the actual document object, without modifying the Document class in the Umbraco source.
You could possibly change the List<T> data structure to a Dictionary<TKey, TValue>, and store the flag in the second position. Maybe not ideal, but could possibly help with what you are trying to do.
I was wondering if the way to do it would be to create a new object that inherits from a document but with an additional parameter or am I just tired as it's 1am over here.
What I'd do is create a class which inherits from Document and IDictionary<string, object> so that you can have it work like a key/ value store. Then you can push in as many properties which only exist as in-memory representations on the instance
So my final solution was as mentioned earlier, I inherited the document object and added two additional properties to my new object, it works like a charm, thanks both for replying to the post.
Temporary properties on an in memory Umbraco document.
Hi All,
I have some current code that currently creates a list of Umbraco document objects, what I would like to do it to set a flag on some of these objects before they are added to the list so that I can access that flag later in my code.
I do not want to update the actual documents, this is just on a per-request basis.
You can see from this code snippet currently I have a data set with a list of ID's and I am cycling through that list grabbing the document and adding it to the resource List.
I am trying to add a "viewed" flag to this document ( which is also in the sqlReader data. )
This code is already quite deeply embedded in a live solution so a complete re-write in the timescale I have (2 days) is out of the question!
I hope someone can help :)
Cheers,
Chris
Hey Chris,
You wont be able to add another property to the actual document object, without modifying the Document class in the Umbraco source.
You could possibly change the List<T> data structure to a Dictionary<TKey, TValue>, and store the flag in the second position. Maybe not ideal, but could possibly help with what you are trying to do.
Hope this helps.
Cheers,
Neil
Hi Neil,
Hope you are doing well down under?
I was wondering if the way to do it would be to create a new object that inherits from a document but with an additional parameter or am I just tired as it's 1am over here.
Cheers,
Chris
Yep i'm doing well as usual :)
Actually yes, that could work! I would give that a try.
Cheers,
Neil
Sounds like someone is trying to duck type C# :P
What I'd do is create a class which inherits from Document and IDictionary<string, object> so that you can have it work like a key/ value store. Then you can push in as many properties which only exist as in-memory representations on the instance
Hi Slace,
Thanks for your reply, that's basically where I had got to in my thought process at 1am last night ;-)
I'll give it a go now and we'll see how it goes :)
Cheers,
Chris
So my final solution was as mentioned earlier, I inherited the document object and added two additional properties to my new object, it works like a charm, thanks both for replying to the post.
Chris
is working on a reply...