Struggling with List<int>

Oct 4, 2010 at 11:32 PM

One of my SimpleDB classes has the following property:

public List<int> Reference { get; set; }

It appears that only unique int values get added as "Reference" attributes. Are duplicate attributes out of the question?

 

Never mind I thought, I'll try a CustomFormat provider.

The problem is that the "ToString" method only gets passed the first element in the collection. Any other options?

public class CustomReferenceFormatter : ITypeFormatter
{
    public string ToString(object value)
    {
        // value = first element in array
    }
    ....
}

Coordinator
Oct 5, 2010 at 12:52 AM
Edited Oct 5, 2010 at 4:12 AM

Your problem on the first issue is that SimpleDB doesn't support duplicate values under the same attribute name. Even if you drop down and use the Amazon SimpleDB API (bypassing Savant) you'll find the same behavior.

I can't reproduce your second issue. In fact, I have code and tests for my own production applications that use custom formatters with lists and don't exhibit this problem. If you still think it's a Savant issue and not a problem with your application code please provide a complete test case and I'll investigate further.

 

Oct 5, 2010 at 9:57 AM

I was trying to get round the unique attribute problem by formatting my array as "20,23,24,25,23,23,23". I was hoping that the custom formatter would be passed List<int> which I could then convert to string. The custom formatter is actually passed each element separately, so I can't use it as I had intended.

 I have however managed to get it working like this:

        [SavantExclude]
        public List<int> Reference { get; set; }

        [AttributeName("Reference")]
        public string ReferenceString
        {
            get 
            {
                string output = String.Empty;
                foreach (var each in Reference)
                    output += each.ToString() + ",";
                return output.Trim(new char[] { ',' });
            }
            set
            {
                List<int> output = new List<int>();
                string[] parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var each in parts)
                    output.Add(Convert.ToInt32(each));
                Reference = output;
            }
        }

Coordinator
Oct 5, 2010 at 12:08 PM

Yep, that looks like a good solution. Another solution if you don't want two properties on your data class is to wrap your List<int>in a custom class. That way Savant will treat the property as a scalar value and your custom formatter will work as you originally tried to make it work.

Oct 5, 2010 at 2:11 PM

Cheers Ashley. First I tried inheriting List<int> which obviously didn't work, but wrapping works well! 

I suppose I could write a formatter which saves other SimpleDB classes as their ItemNames and fetches them from other domains on load. I guess it would be pretty slow, and not really what SimpleDB was designed for...