Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IBaseProvider
- {
- void Configure(IDictionary<string, object> configValues);
- }
- public interface ISqlProvider : IBaseProvider
- {
- ///CRUD omitted for clarity
- }
- public interface IBlobProvider : IBaseProvider
- {
- ///CRUD omitted for clarity
- }
- public interface IDocProvider : IBaseProvider
- {
- ///CRUD omitted for clarity
- }
- public class SqlDataProvider : ISqlProvider
- {
- public void Configure(IDictionary<string, object> configValues)
- {
- //Do stuff
- }
- }
- public class DocDataProvider : IDocProvider
- {
- public void Configure(IDictionary<string, object> configValues)
- {
- //Do stuff
- }
- }
- public class BlobDataProvider : IBlobProvider
- {
- public void Configure(IDictionary<string, object> configValues)
- {
- //Do stuff
- }
- }
- public interface IProviderLocator
- {
- T CreateInstance<T>(IDictionary<string, object> configValues) where T : IBaseProvider;
- }
- public sealed class ProviderLocator : IProviderLocator
- {
- protected IDictionary<string, object> configValues;
- public T CreateInstance<T>(IDictionary<string, object> configurationValues) where T : IBaseProvider
- {
- configValues = configurationValues;
- return Initialize<T>();
- }
- private T Initialize<T>() where T : IBaseProvider
- {
- //reach into the configuration system to get providerType
- var provider = (T)Activator.CreateInstance(providerType);
- provider.Configure(configValues);
- return provider;
- }
- }
- var database = new ProviderLocator().CreateInstance<ISqlProvider>(null);
- public interface IProviderFactory<T>
- {
- T CreateInstance<T>(IDictionary<string, object> configValues)
- }
- public sealed class SqlProviderFactory : IProviderFactory<ISqlProvider>
- {
- public T CreateInstance<T>(IDictionary<string, object> configurationValues)
- {
- return new SqlDataProvider(configurationValues);
- }
- }
Add Comment
Please, Sign In to add comment