3

I am interested, if it is possible to have collection with same elements in .Net configuration. Like this, for example:

                <RetrySettings>
                <RetryTurn PeriodBeforeRetry="0:05:00"/>
                <RetryTurn PeriodBeforeRetry="0:10:00"/>
                <RetryTurn PeriodBeforeRetry="0:30:00"/>
                <RetryTurn PeriodBeforeRetry="1:00:00"/>
                <RetryTurn PeriodBeforeRetry="4:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
            </RetrySettings>

without adding annoying id="someUniqueId" attributes to each RetryTurn member?

I don't see how to make this, using custom collection, derived from ConfigurationElementCollection... Any possible solution for this?

Alexander Bortnik
  • 1,219
  • 14
  • 24

3 Answers3

10

Finally I found the workaround. In RetryTurn class define internal Guid property UniqueId and initialize it with new Guid value in default constructor:

public class RetryTurnElement : ConfigurationElement
{
    public RetryTurnElement()
    {
        UniqueId = Guid.NewGuid();
    }

    internal Guid UniqueId { get; set; }

    ...
}

In RetryTurnCollection class override GetElementKey method like this:

    public class RetryTurnCollection : ConfigurationElementCollection
{
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((RetryTurnElement)element).UniqueId;
    }
    ...
}
Alexander Bortnik
  • 1,219
  • 14
  • 24
  • 1
    I'd imagine you could also use `static int nextId = 0;` with a `lock` object for safety, then in the constructor set `UnqiueId = nextId++;` So then your Ids would be ordered int's instead of Guids of a random order. – Thymine Jan 10 '12 at 19:44
4

Have you tried

public class RetryTurnCollection : ConfigurationElementCollection
{
    protected override object GetElementKey(ConfigurationElement element)
    {
        return element;
    }
    ...
}
Crab
  • 41
  • 1
0

Couldn't you use the PeriodBeforeRetry attribute as your unique identifier? GetElementKey() returns an object, so that shouldn't be a problem.

Unless I've misunderstood the question.

Pike65
  • 562
  • 2
  • 6
  • `PeriodBeforeRetry` can't be used as unique identifier because it's value is not required to be unique, i.e. several `RetryTurn` elements with same values of `PeriodBeforeRetry` can exist in one collection. – Alexander Bortnik Aug 28 '09 at 13:23