Try our conversational search powered by Generative AI!

Generic collection in Dynamic Data Store

Vote:
 

Hi,

I'm trying to store a generic collection in the dynamic data store but fails to do that. I have created a Comment class consisting of three string properties and a Comments class which is a generic collection (List) of data type Comment.

First I have created a pageobjectmanager:
pom = new PageObjectManager(CurrentPage);

In the page OnLoad event handler I do this:
comments = pom.Load<Comments>("comments");

When I store the values I do this:
pom.Save("comments", comments);

Even though the comments list has a comment when I store the value, after reloading the page its value is gone. Does anyone have a clue why this happens or do you perhaps know of any other way to store a collection in the DDS?

#38706
Apr 20, 2010 8:16
Vote:
 

I do exactly what you are trying to accomplish.

Remeber to use the Identity from the IDynamicDatainterface on the objects.

someobject.ID = Identity.NewIdentity(Guid.NewGuid());

Regards,
Morten

#38707
Apr 20, 2010 8:38
Vote:
 

Thanks for your reply Morten,

unfortunately I still don't understand what I have to do to make it work, could you please try to explain it more thoroughly?

Many thanks in advance!

#38711
Apr 20, 2010 13:15
Vote:
 

I had something like this happen too.  When I reloaded the page, PageObjectManager had a record of the key, but the value was always NULL.

Is this what's happening to you too?  I have no solution, sadly.

#38727
Apr 20, 2010 15:13
Vote:
 

Hi Deane,

that is exactly what happens to me. Anyone who has a solution to this?

#38735
Apr 20, 2010 15:30
Vote:
 

Jens:

I have it partially figured out --

In the class that you're storing, you have to implement IDynamicData.  When you do this, you just have to add this to your class:

public EPiServer.Data.Identity Id { get; set; }

I did this with a custom class, and it worked perfectly.

(My problem, however, is -- how do you store a primitive?  I just want to store an int.  Since I can't implement an interface on a primitive, is it impossible to store a primitive?)

Deane

#38749
Apr 21, 2010 6:46
Vote:
 

DDS takes the approach to save an object by saving all its properties, and by default it
saves all "simple" (non-indexed) properties (not fields) that are public and has a getter and a setter.

When you pass in a List<T>, the only property that fullfills this is the Capacity property,
and the actual items in the list wont get saved.

You would need to wrap the List<T> in your own class, and then pass it to DDS save(),
something like this:

public class CommentsWrap
{
   public List<Comment> Comments { get; sst; }
}

Regards,
Johan

#38752
Edited, Apr 21, 2010 10:16
Vote:
 

Here is how I've done it:

 

   public class TheList : IDynamicData
    {
        #region IDynamicData Members

        public Identity Id { get; set; }

        #endregion
        public List<TheItem> Items { get; set; }
    }

    public class TheItem : IDynamicData
    {
        public string MyString { get; set; }
        public int MyItn { get; set; }

        #region IDynamicData Members
        public Identity Id { get; set; }
    }

 

Regards,
Morten

#38753
Apr 21, 2010 10:24
Vote:
 

Thanks all for your answers!

I don't have time to test this right now but as Morten already has a working solution I'm pretty sure this will solve my problem and therefore I will mark this thread as answered.

Thanks once again!

#38754
Apr 21, 2010 10:38
Vote:
 

All:

So, can you store primitives in DDS?  Like an int?  Or would have to wrap my int in a custom class that implements IDynamicData and exposes the int as a public property?

Deane

#38758
Apr 21, 2010 14:20
Vote:
 

No, you for the same reason as for the List<T> case, you cannot store primitives directly in DDS.

You need to encapsulate it into a type, and expose it through a property like so:

public class MyClass
{
   public int MyInt { get; set; }
}

/johan

#38759
Apr 21, 2010 14:22
Vote:
 

Content removed since I was spreading lies with regards to primitives ;-)

#38760
Edited, Apr 21, 2010 14:26
Vote:
 

There's actually a second limitation in play here. You can't store generics - at least not as a PageObjects or in "typed" stores - since their generated store names include forbidden characters.

#38890
Apr 22, 2010 8:15
Vote:
 

Not sure what you mean here Magnus,

The code I posed a few post up, with a Genering list of another class works perfect.
I might be missing your point here though :-)

//Morten

#38891
Apr 22, 2010 8:26
Vote:
 

Yeah, you're storing objects of a class called TheList. You couldn't store your List<TheClass> directly because it lacks public properties. But you could not even store a TheList<T> if you wanted that for some reason (for example to create a generic metaclass for lists which has a property storing the actual list just like in your example). This doesn't stop at lists or list-like objects of course, it applies to all generic classes.

#38893
Apr 22, 2010 8:31
Vote:
 

Ah that's what you meant :-)

//Morten

#38894
Apr 22, 2010 8:34
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.