views:

93

answers:

1

Hi,

We have an interface which will be implemented by classes that take care of data transport across the network or com ports for example.

Let's call it IDataTransporter for now and we have 2 implementations:

ComPortDataTransporter and TcpDataTransporter.

Obviously these two require very different configuration.

The first has settings such as buadrate, start/stop bits etc... the second has settings such as ip address and port.

Are there any best practices to deal with this? In the end we want the user to be able to load a "part" and configure it once.

Thnx.

+3  A: 

Perhaps you could export some sort of configuration part along with your transporter part.

Let's say your host application defines an interface:

public interface IDataTransporterSettings 
{
    // any common settings are defined here
}

... and assumes that any IDataTrasporter has a public property of that type:

public interface IDataTransporter
{
    IDataTransporterSettings Settings { get; }
}

... then your "settings" could be a class with a bunch of public properties matching each of the settings:

public class TcpDataTransporterSettings : IDataTransporterSettings
{
    public string Address { get; set; }
    public int Port { get; set; }
}

Your TcpDataTransporter class, then, would always read from its Settings property to determine its address/port/whatever.

Now your host application could use reflection to inspect the settings on each of its imported transporter parts and display an appropriate control for each public settable property (a TextBox for strings, a NumericUpDown for ints etc).

I guess with a bit of work you could add human-readable descriptions to each property via Attributes, so you could define the label for each control in the UI.

I haven't fully thought this through, and I certainly haven't implemented it anywhere, but it sounds like it could be a way to provide flexible settings for each imported part.

Matt Hamilton
Sounds like a good idea, won't mark it as the answer just yet, but I might. What I have now is a Dictionary<string, object> as part of the IDataTransporter interface. I'll look into this solution as well.I'm wondering if assemblies can have their own .xml configuration file....
TimothyP