CommonProvider is a simple library that gives you an easy and consistent way of loading and accessing your providers/strategies. The provider/strategy pattern promotes loose coupling and enables extensibility in your applications.
Some key features of CommonProvider are,
- Easy and consistent way of accessing providers.
- Support for configuration based providers (IProvider). Configuration is loaded from a config source.
- Support for zero configuration providers (IZeroConfigProvider). Configuration not required.
- Define provider specific settings or global settings (does not apply to IZeroConfigProvider)
- Provider settings could be simple or complex (serialized).
- Use data parsers to parse/deserialize complex settings.
- Dependency resolution via IOC containers e.g. Unity, Castle etc.
- Various extension points.
CommonProvider has various extension points but the key ones are,
- Write custom provider configuration sources.
- Write custom dependency resolvers.
- Write custom data parsers for dealing with complex settings.
Installing CommonProvider - nuget
PM> Install-Package CommonProvider
Release notes can be found here
Implement either the IProvider (requires configuration) or IZeroConfigProvider (doesn't require configuration) interface
public abstract class SmsProviderBase : IProvider
{
public string Name { get; set; }
public ISettings Settings { get; set; }
public string Group { get; set; }
public abstract string SendSms(Message message);
}
public class NexmoSmsProvider : SmsProviderBase
{
public override string SendSms(Message message)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Message sent using NEXMO Provider. See details below");
stringBuilder.AppendLine(string.Format("Text: {0}", message.Text));
stringBuilder.AppendLine(string.Format("From: {0}", message.Sender));
stringBuilder.AppendLine(string.Format("To: {0}", message.PhoneNumbers));
stringBuilder.AppendLine(string.Format("Endpoint: {0}", this.Settings.Get<string>("Endpoint")));
stringBuilder.AppendLine(string.Format("ApiKey: {0}", this.Settings.Get<string>("ApiKey")));
return stringBuilder.ToString();
}
}
Config setup for IProvider (not required for IZeroConfigProvider)
<configSections>
<section name="commonProvider" type="CommonProvider.ConfigSources.Xml.Configuration.ProviderConfigSection, CommonProvider"/>
</configSections>
<commonProvider>
<!--Define your types here-->
<types>
<add name="NexmoProvider" type="CommonProvider.Example.Lib.Providers.NexmoSmsProvider, CommonProvider.Example.Lib"/>
<add name="TwilioProvider" type="CommonProvider.Example.Lib.Providers.TwilioSmsProvider, CommonProvider.Example.Lib"/>
<add name="PipeDelimitedDataParser" type="CommonProvider.Data.PipeDelimitedDataParser, CommonProvider"/>
</types>
<!--Define provider global settings here-->
<settings dataParserType="PipeDelimitedDataParser">
<add key="Message" value="sender:007|text:Hello World!!!|phoneNumbers:1010101010,2020202020,3030303030"/>
</settings>
<!--Define providers here-->
<providers>
<provider name="Nexmo" enabled="true" type="NexmoProvider" group="">
<settings>
<add key="Endpoint" value="http://www.nexmo.test/sendsms"/>
<add key="ApiKey" value="FakeApiKey"/>
</settings>
</provider>
<provider name="Twilio" enabled="true" type="TwilioProvider" group="">
<settings>
<add key="Endpoint" value="http://www.twilio.test/sendsms"/>
<add key="ApiKey" value="FakeApiKey"/>
</settings>
</provider>
</providers>
</commonProvider>
Create Provider Manager
var providerManager = new ProviderManager(new XmlProviderConfigSource());
Get Provider
var nexmoProvider = providerManager.Providers.ByName<SmsProviderBase>("Nexmo");
Get Provider setting
var apiKey = nexmoProvider.Settings.Get<string>("ApiKey");
Get global setting
var message = providerManager.Settings.Get<Message>("Message");
More detailed examples here
CommonProvider is released under the MIT license.