.config to create tricks?

I am working on a quick project to monitor / process data. In fact, these are just monitors, graphics, and processors. The monitor checks the data (ftp, local, imap, pop, etc.) using a schedule and sends new data to the processor. All of them have interfaces.

I am trying to find a reasonable way to use config to configure the graph / processor that each monitor uses. This is pretty easy:

<monitor type="any.class.implementing.monitor">
    <schedule type="any.class.implementing.schedule">
        ...
    </schedule>
    <processor type="any.class.implementing.processor" />
</monitor>

What I'm struggling with is the best way to set up any old monitor / graph / processor thrown into a mix. On the one hand, it was possible to implement constructor parameters or properties (let's take any syntax):

<monitor type="any.class.implementing.monitor">
    <args>
        <arg value="..." />
    </args>
    <properties>
        <property name="..." value=..." />
    </properties>
    <schedule type="any.class.implementing.schedule">
        ...
    </schedule>
    <processor type="any.class.implementing.processor" />
</monitor>

Another solution is the factory method in each interface, which takes the user configuration as a parameter:

public IMonitor Create(CustomConfigSection config);

, . ? ?

, DI . , , , , .

+5
2

, IConfigurationSectionHandler, factory, , , , ( ). IConfigurationSectionHandler , , factory - , , ( ).

setterter/getters . factory factory .

0

, :

public class MonitorElement : ConfigurationElement
{
    // ...whatever schema you prefer...
}

public class MonitorElementCollection : ConfigurationElementCollection
{
    // ...standard implementation...
}

:

public class YourSection : ConfigurationSection
{
    [ConfigurationProperty("monitors")]
    [ConfigurationCollection(typeof(MonitorElementCollection))]
    public MonitorElementCollection Monitors
    {
        get { return (MonitorElementCollection) this["monitors"]; }
    }
}

, :

public interface IMonitorRepository
{
    IEnumerable<Monitor> GetMonitors();
}

, :

public sealed class ConfiguredMonitorRepository : IMonitorRepository
{
    private readonly string _sectionName;

    public ConfiguredMonitorRepository(string sectionName)
    {
        _sectionName = sectionName;
    }

    public IEnumerable<Monitor> GetMonitors()
    {
        var section = (YourSection) ConfigurationManager.GetSection(_sectionName);

        if(section != null)
        {
            foreach(var monitor in section.Monitors)
            {
                yield return ...create and configure monitor...
            }
        }
    }
}

, , . , XML, , . , API Autofac XML.

, , , - IoC; .

0

All Articles