Skip to content

Commit 292f204

Browse files
Merge pull request #94 from johelvisguzman/factory
Changed the repository factory options to a dictionary
2 parents 7c4dbb6 + 2d512f4 commit 292f204

File tree

10 files changed

+358
-196
lines changed

10 files changed

+358
-196
lines changed

src/DotNetToolkit.Repository.AdoNet/AdoNetRepositoryFactory.cs

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace 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+
}

src/DotNetToolkit.Repository.Csv/CsvRepositoryFactory.cs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
namespace DotNetToolkit.Repository.Csv
22
{
3+
using Logging;
34
using System;
4-
using System.Linq;
5+
using System.Collections.Generic;
56

67
/// <summary>
78
/// An implementation of <see cref="IRepositoryFactory" />.
@@ -10,7 +11,10 @@ public class CsvRepositoryFactory : IRepositoryFactory
1011
{
1112
#region Fields
1213

13-
private readonly IRepositoryFactoryOptions _options;
14+
private const string FilePathOptionKey = "path";
15+
private const string LoggerKey = "logger";
16+
17+
private readonly Dictionary<string, object> _options;
1418

1519
#endregion
1620

@@ -27,7 +31,7 @@ public CsvRepositoryFactory()
2731
/// Initializes a new instance of the <see cref="CsvRepositoryFactory"/> class.
2832
/// </summary>
2933
/// <param name="options">The options.</param>
30-
public CsvRepositoryFactory(IRepositoryFactoryOptions options)
34+
public CsvRepositoryFactory(Dictionary<string, object> options)
3135
{
3236
if (options == null)
3337
throw new ArgumentNullException(nameof(options));
@@ -39,18 +43,39 @@ public CsvRepositoryFactory(IRepositoryFactoryOptions options)
3943

4044
#region Private Methods
4145

42-
private string GetFilePath(IRepositoryFactoryOptions options)
46+
private static void GetOptions(Dictionary<string, object> options, out string path, out ILogger logger)
4347
{
4448
if (options == null)
4549
throw new ArgumentNullException(nameof(options));
4650

47-
var arg = options.DbContextArgs.FirstOrDefault();
48-
var databaseName = arg as string;
49-
50-
if (arg != null && databaseName == null)
51-
throw new ArgumentException($"The provided '{nameof(options.DbContextArgs)}' must be a valid string argument.");
52-
53-
return databaseName;
51+
if (options.Count == 0)
52+
throw new InvalidOperationException("The options dictionary does not contain any items.");
53+
54+
object value = null;
55+
path = null;
56+
logger = null;
57+
58+
if (options.ContainsKey(FilePathOptionKey))
59+
{
60+
value = options[FilePathOptionKey];
61+
path = value as string;
62+
63+
if (value != null && path == null)
64+
throw new ArgumentException($"The option value for the specified '{FilePathOptionKey}' key must be a valid '{typeof(string).Name}' type.");
65+
}
66+
else
67+
{
68+
throw new InvalidOperationException($"The '{FilePathOptionKey}' option is missing from the options dictionary.");
69+
}
70+
71+
if (options.ContainsKey(LoggerKey))
72+
{
73+
value = options[LoggerKey];
74+
logger = value as ILogger;
75+
76+
if (value != null && logger == null)
77+
throw new ArgumentException($"The option value for the specified '{LoggerKey}' key must be a valid '{typeof(ILogger).Name}' type.");
78+
}
5479
}
5580

5681
#endregion
@@ -90,9 +115,11 @@ public IRepository<TEntity, TKey> Create<TEntity, TKey>() where TEntity : class
90115
/// <typeparam name="TEntity">The type of the entity.</typeparam>
91116
/// <param name="options">The options.</param>
92117
/// <returns>The new repository.</returns>
93-
public IRepository<TEntity> Create<TEntity>(IRepositoryFactoryOptions options) where TEntity : class
118+
public IRepository<TEntity> Create<TEntity>(Dictionary<string, object> options) where TEntity : class
94119
{
95-
return new CsvRepository<TEntity>(GetFilePath(options), options.Logger);
120+
GetOptions(options, out string path, out ILogger logger);
121+
122+
return new CsvRepository<TEntity>(path, logger);
96123
}
97124

98125
/// <summary>
@@ -102,9 +129,11 @@ public IRepository<TEntity> Create<TEntity>(IRepositoryFactoryOptions options) w
102129
/// <typeparam name="TKey">The type of the key primary key value.</typeparam>
103130
/// <param name="options">The options.</param>
104131
/// <returns>The new repository.</returns>
105-
public IRepository<TEntity, TKey> Create<TEntity, TKey>(IRepositoryFactoryOptions options) where TEntity : class
132+
public IRepository<TEntity, TKey> Create<TEntity, TKey>(Dictionary<string, object> options) where TEntity : class
106133
{
107-
return new CsvRepository<TEntity, TKey>(GetFilePath(options), options.Logger);
134+
GetOptions(options, out string path, out ILogger logger);
135+
136+
return new CsvRepository<TEntity, TKey>(path, logger);
108137
}
109138

110139
#endregion

src/DotNetToolkit.Repository.EntityFramework/EfRepositoryFactory.cs

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
namespace DotNetToolkit.Repository.EntityFramework
22
{
3+
using Logging;
34
using System;
5+
using System.Collections.Generic;
46
using System.Data.Entity;
7+
using System.Data.Entity.Infrastructure;
58

69
/// <summary>
710
/// An implementation of <see cref="IRepositoryFactory" />.
@@ -10,7 +13,12 @@ public class EfRepositoryFactory : IRepositoryFactory
1013
{
1114
#region Fields
1215

13-
private readonly IRepositoryFactoryOptions _options;
16+
private const string DbContextTypeKey = "dbContextType";
17+
private const string DbCompiledModelKey = "dbCompiledModel";
18+
private const string ConnectionStringKey = "connectionString";
19+
private const string LoggerKey = "logger";
20+
21+
private readonly Dictionary<string, object> _options;
1422

1523
#endregion
1624

@@ -27,7 +35,7 @@ public EfRepositoryFactory()
2735
/// Initializes a new instance of the <see cref="EfRepositoryFactory"/> class.
2836
/// </summary>
2937
/// <param name="options">The options.</param>
30-
public EfRepositoryFactory(IRepositoryFactoryOptions options)
38+
public EfRepositoryFactory(Dictionary<string, object> options)
3139
{
3240
if (options == null)
3341
throw new ArgumentNullException(nameof(options));
@@ -39,22 +47,69 @@ public EfRepositoryFactory(IRepositoryFactoryOptions options)
3947

4048
#region Private Methods
4149

42-
private static DbContext GetDbContext(IRepositoryFactoryOptions options)
50+
private static void GetOptions(Dictionary<string, object> options, out DbContext context, out ILogger logger)
4351
{
4452
if (options == null)
4553
throw new ArgumentNullException(nameof(options));
4654

47-
if (options.DbContextType == null)
48-
throw new InvalidOperationException($"The repository options must provide a '{nameof(options.DbContextType)}'.");
55+
if (options.Count == 0)
56+
throw new InvalidOperationException("The options dictionary does not contain any items.");
4957

50-
DbContext context;
58+
object value = null;
59+
string connectionString = null;
60+
Type contextType = null;
61+
DbCompiledModel model = null;
62+
context = null;
63+
logger = null;
5164

52-
if (options.DbContextArgs == null)
53-
context = (DbContext)Activator.CreateInstance(options.DbContextType);
54-
else
55-
context = (DbContext)Activator.CreateInstance(options.DbContextType, options.DbContextArgs);
65+
if (options.ContainsKey(DbContextTypeKey))
66+
{
67+
value = options[DbContextTypeKey];
68+
contextType = value as Type;
5669

57-
return context;
70+
if (value != null && contextType == null)
71+
throw new ArgumentException($"The option value for the specified '{DbContextTypeKey}' key must be a valid '{typeof(Type).Name}' type.");
72+
}
73+
else
74+
{
75+
throw new InvalidOperationException($"The '{DbContextTypeKey}' option is missing from the options dictionary.");
76+
}
77+
78+
if (options.ContainsKey(ConnectionStringKey))
79+
{
80+
value = options[ConnectionStringKey];
81+
connectionString = value as string;
82+
83+
if (value != null && connectionString == null)
84+
throw new ArgumentException($"The option value for the specified '{ConnectionStringKey}' key must be a valid '{typeof(string).Name}' type.");
85+
}
86+
else
87+
{
88+
throw new InvalidOperationException($"The '{ConnectionStringKey}' option is missing from the options dictionary.");
89+
}
90+
91+
if (options.ContainsKey(DbCompiledModelKey))
92+
{
93+
value = options[DbCompiledModelKey];
94+
model = value as DbCompiledModel;
95+
96+
if (value != null && logger == null)
97+
throw new ArgumentException($"The option value for the specified '{DbCompiledModelKey}' key must be a valid '{typeof(DbCompiledModel).Name}' type.");
98+
}
99+
100+
if (options.ContainsKey(LoggerKey))
101+
{
102+
value = options[LoggerKey];
103+
logger = value as ILogger;
104+
105+
if (value != null && logger == null)
106+
throw new ArgumentException($"The option value for the specified '{LoggerKey}' key must be a valid '{typeof(ILogger).Name}' type.");
107+
}
108+
109+
if (model == null)
110+
context = (DbContext)Activator.CreateInstance(contextType, connectionString);
111+
else
112+
context = (DbContext)Activator.CreateInstance(contextType, connectionString, model);
58113
}
59114

60115
#endregion
@@ -94,9 +149,11 @@ public IRepository<TEntity, TKey> Create<TEntity, TKey>() where TEntity : class
94149
/// <typeparam name="TEntity">The type of the entity.</typeparam>
95150
/// <param name="options">The options.</param>
96151
/// <returns>The new repository.</returns>
97-
public IRepository<TEntity> Create<TEntity>(IRepositoryFactoryOptions options) where TEntity : class
152+
public IRepository<TEntity> Create<TEntity>(Dictionary<string, object> options) where TEntity : class
98153
{
99-
return new EfRepository<TEntity>(GetDbContext(options), options.Logger);
154+
GetOptions(options, out DbContext context, out ILogger logger);
155+
156+
return new EfRepository<TEntity>(context, logger);
100157
}
101158

102159
/// <summary>
@@ -106,9 +163,11 @@ public IRepository<TEntity> Create<TEntity>(IRepositoryFactoryOptions options) w
106163
/// <typeparam name="TKey">The type of the key primary key value.</typeparam>
107164
/// <param name="options">The options.</param>
108165
/// <returns>The new repository.</returns>
109-
public IRepository<TEntity, TKey> Create<TEntity, TKey>(IRepositoryFactoryOptions options) where TEntity : class
166+
public IRepository<TEntity, TKey> Create<TEntity, TKey>(Dictionary<string, object> options) where TEntity : class
110167
{
111-
return new EfRepository<TEntity, TKey>(GetDbContext(options), options.Logger);
168+
GetOptions(options, out DbContext context, out ILogger logger);
169+
170+
return new EfRepository<TEntity, TKey>(context, logger);
112171
}
113172

114173
#endregion

0 commit comments

Comments
 (0)