views:

232

answers:

2

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?

A: 

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
`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.
Tabernakli
+3  A: 

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;
    }
    ...
}

And it works.

Tabernakli