I have an interface for holding the connection configuration info for web service access:
public interface IServiceConnectionConfiguration
{
string Username { get; }
string Password { get; }
string ChannelEndpointUrl { get; }
string MediaEndpointUrl { get; }
string PlayerlEndpointUrl { get; }
string PlaylistEndpointUrl { get; }
}
I have a factory class that returns the service instance specific to the type of the service requested.
public static class ServiceClientFactory
{
public static void Configure(IServiceConnectionConfiguration config)
{
_config = config;
}
public static T GetService<T>() where T : class, IServiceClient
{
}
}
The factory is called as
Channel channelService = factory.GetService<Channel>();
What I am trying to figure out is an elegant way for the Factory code to resolve the endpoint urls for the passed in types itself based on the config object passed during initialization. eg. If the type parameter passed is channel, it should take the ChannelEndpointUrl while constructing the ChannelService.
I thought about using attributes on the config class to decorate the endpoint urls with the service type that they correspond to but it seems like a bad design.
Any ideas.
Well, one way to approach it would be to have the Factory have a private static Dictionary containing your initialization logic, indexed by “Type”. Similar to a strategy pattern.
for example:
EDIT: Now, as you mentioned, you cannot instantiate explicitly in your factory since you’d cause a circular reference, maybe you can force a new() constraint, and construct the instance in the GetService method, and only use the dictionary for endpoint configuration, such as: