Guest User

Untitled

a guest
Oct 27th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. public interface IBaseProvider
  2. {
  3. void Configure(IDictionary<string, object> configValues);
  4. }
  5.  
  6. public interface ISqlProvider : IBaseProvider
  7. {
  8. ///CRUD omitted for clarity
  9. }
  10.  
  11. public interface IBlobProvider : IBaseProvider
  12. {
  13. ///CRUD omitted for clarity
  14. }
  15.  
  16. public interface IDocProvider : IBaseProvider
  17. {
  18. ///CRUD omitted for clarity
  19. }
  20.  
  21. public class SqlDataProvider : ISqlProvider
  22. {
  23. public void Configure(IDictionary<string, object> configValues)
  24. {
  25. //Do stuff
  26. }
  27. }
  28.  
  29. public class DocDataProvider : IDocProvider
  30. {
  31. public void Configure(IDictionary<string, object> configValues)
  32. {
  33. //Do stuff
  34. }
  35. }
  36.  
  37. public class BlobDataProvider : IBlobProvider
  38. {
  39. public void Configure(IDictionary<string, object> configValues)
  40. {
  41. //Do stuff
  42. }
  43. }
  44.  
  45. public interface IProviderLocator
  46. {
  47. T CreateInstance<T>(IDictionary<string, object> configValues) where T : IBaseProvider;
  48.  
  49. }
  50.  
  51. public sealed class ProviderLocator : IProviderLocator
  52. {
  53. protected IDictionary<string, object> configValues;
  54. public T CreateInstance<T>(IDictionary<string, object> configurationValues) where T : IBaseProvider
  55. {
  56. configValues = configurationValues;
  57. return Initialize<T>();
  58. }
  59.  
  60. private T Initialize<T>() where T : IBaseProvider
  61. {
  62. //reach into the configuration system to get providerType
  63. var provider = (T)Activator.CreateInstance(providerType);
  64. provider.Configure(configValues);
  65.  
  66. return provider;
  67. }
  68. }
  69.  
  70. var database = new ProviderLocator().CreateInstance<ISqlProvider>(null);
  71.  
  72. public interface IProviderFactory<T>
  73. {
  74. T CreateInstance<T>(IDictionary<string, object> configValues)
  75. }
  76.  
  77. public sealed class SqlProviderFactory : IProviderFactory<ISqlProvider>
  78. {
  79. public T CreateInstance<T>(IDictionary<string, object> configurationValues)
  80. {
  81. return new SqlDataProvider(configurationValues);
  82. }
  83. }
Add Comment
Please, Sign In to add comment