11namespace DotNetToolkit . Repository . AdoNet
22{
3+ using Logging ;
34 using System ;
5+ using System . Collections . Generic ;
46
57 /// <summary>
68 /// An implementation of <see cref="IRepositoryFactory" />.
@@ -9,7 +11,11 @@ public class AdoNetRepositoryFactory : IRepositoryFactory
911 {
1012 #region Fields
1113
12- private readonly IRepositoryFactoryOptions _options ;
14+ private const string ProviderNameKey = "providerName" ;
15+ private const string ConnectionStringKey = "connectionString" ;
16+ private const string LoggerKey = "logger" ;
17+
18+ private readonly Dictionary < string , object > _options ;
1319
1420 #endregion
1521
@@ -26,7 +32,7 @@ public AdoNetRepositoryFactory()
2632 /// Initializes a new instance of the <see cref="AdoNetRepositoryFactory"/> class.
2733 /// </summary>
2834 /// <param name="options">The options.</param>
29- public AdoNetRepositoryFactory ( IRepositoryFactoryOptions options )
35+ public AdoNetRepositoryFactory ( Dictionary < string , object > options )
3036 {
3137 if ( options == null )
3238 throw new ArgumentNullException ( nameof ( options ) ) ;
@@ -38,37 +44,48 @@ public AdoNetRepositoryFactory(IRepositoryFactoryOptions options)
3844
3945 #region Private Methods
4046
41- private void GetProviderAndConnectionString ( IRepositoryFactoryOptions options , out string providerName , out string connectionString )
47+ private static void GetOptions ( Dictionary < string , object > options , out string providerName , out string connectionString , out ILogger logger )
4248 {
4349 if ( options == null )
4450 throw new ArgumentNullException ( nameof ( options ) ) ;
4551
46- if ( options . DbContextArgs == null || options . DbContextArgs . Length == 0 )
47- throw new InvalidOperationException ( $ "The repository options must provide a '{ nameof ( options . DbContextArgs ) } '.") ;
52+ if ( options . Count == 0 )
53+ throw new InvalidOperationException ( "The options dictionary does not contain any items." ) ;
54+
55+ object value = null ;
56+ providerName = null ;
57+ connectionString = null ;
58+ logger = null ;
4859
49- if ( options . DbContextArgs . Length == 1 )
60+ if ( options . ContainsKey ( ProviderNameKey ) )
5061 {
51- var arg1 = options . DbContextArgs [ 0 ] ;
52- connectionString = arg1 as string ;
62+ value = options [ ProviderNameKey ] ;
63+ providerName = value as string ;
64+
65+ if ( value != null && providerName == null )
66+ throw new ArgumentException ( $ "The option value for the specified '{ ProviderNameKey } ' key must be a valid '{ typeof ( string ) . Name } ' type.") ;
67+ }
5368
54- if ( arg1 != null && connectionString == null )
55- throw new ArgumentException ( $ "The provided '{ nameof ( options . DbContextArgs ) } ' must be a valid string argument to be used as a connection string.") ;
69+ if ( options . ContainsKey ( ConnectionStringKey ) )
70+ {
71+ value = options [ ConnectionStringKey ] ;
72+ connectionString = value as string ;
5673
57- providerName = null ;
74+ if ( value != null && connectionString == null )
75+ throw new ArgumentException ( $ "The option value for the specified '{ ConnectionStringKey } ' key must be a valid '{ typeof ( string ) . Name } ' type.") ;
5876 }
5977 else
6078 {
61- var arg1 = options . DbContextArgs [ 0 ] ;
62- providerName = arg1 as string ;
63-
64- if ( arg1 != null && providerName == null )
65- throw new ArgumentException ( $ "The provided '{ nameof ( options . DbContextArgs ) } ' must be a valid string argument to be used as a provider name.") ;
79+ throw new InvalidOperationException ( $ "The '{ ConnectionStringKey } ' option is missing from the options dictionary.") ;
80+ }
6681
67- var arg2 = options . DbContextArgs [ 1 ] ;
68- connectionString = arg1 as string ;
82+ if ( options . ContainsKey ( LoggerKey ) )
83+ {
84+ value = options [ LoggerKey ] ;
85+ logger = value as ILogger ;
6986
70- if ( arg2 != null && connectionString == null )
71- throw new ArgumentException ( $ "The provided ' { nameof ( options . DbContextArgs ) } ' must be a valid string argument to be used as a connection string .") ;
87+ if ( value != null && logger == null )
88+ throw new ArgumentException ( $ "The option value for the specified ' { LoggerKey } ' key must be a valid ' { typeof ( ILogger ) . Name } ' type .") ;
7289 }
7390 }
7491
@@ -109,13 +126,13 @@ public IRepository<TEntity, TKey> Create<TEntity, TKey>() where TEntity : class
109126 /// <typeparam name="TEntity">The type of the entity.</typeparam>
110127 /// <param name="options">The options.</param>
111128 /// <returns>The new repository.</returns>
112- public IRepository < TEntity > Create < TEntity > ( IRepositoryFactoryOptions options ) where TEntity : class
129+ public IRepository < TEntity > Create < TEntity > ( Dictionary < string , object > options ) where TEntity : class
113130 {
114- GetProviderAndConnectionString ( options , out string providerName , out string connectionString ) ;
131+ GetOptions ( options , out string providerName , out string connectionString , out ILogger logger ) ;
115132
116133 return string . IsNullOrEmpty ( providerName )
117- ? new AdoNetRepository < TEntity > ( connectionString , options . Logger )
118- : new AdoNetRepository < TEntity > ( providerName , connectionString , options . Logger ) ;
134+ ? new AdoNetRepository < TEntity > ( connectionString , logger )
135+ : new AdoNetRepository < TEntity > ( providerName , connectionString , logger ) ;
119136 }
120137
121138 /// <summary>
@@ -125,15 +142,15 @@ public IRepository<TEntity> Create<TEntity>(IRepositoryFactoryOptions options) w
125142 /// <typeparam name="TKey">The type of the key primary key value.</typeparam>
126143 /// <param name="options">The options.</param>
127144 /// <returns>The new repository.</returns>
128- public IRepository < TEntity , TKey > Create < TEntity , TKey > ( IRepositoryFactoryOptions options ) where TEntity : class
145+ public IRepository < TEntity , TKey > Create < TEntity , TKey > ( Dictionary < string , object > options ) where TEntity : class
129146 {
130- GetProviderAndConnectionString ( options , out string providerName , out string connectionString ) ;
147+ GetOptions ( options , out string providerName , out string connectionString , out ILogger logger ) ;
131148
132149 return string . IsNullOrEmpty ( providerName )
133- ? new AdoNetRepository < TEntity , TKey > ( connectionString , options . Logger )
134- : new AdoNetRepository < TEntity , TKey > ( providerName , connectionString , options . Logger ) ;
150+ ? new AdoNetRepository < TEntity , TKey > ( connectionString , logger )
151+ : new AdoNetRepository < TEntity , TKey > ( providerName , connectionString , logger ) ;
135152 }
136153
137154 #endregion
138155 }
139- }
156+ }
0 commit comments