Skip to content

Commit 1bffba6

Browse files
Introduced a repository factory for the entity framework core repository
1 parent 9d3a4c9 commit 1bffba6

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

src/DotNetToolkit.Repository.EntityFramework/EfRepositoryFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ private static DbContext GetDbContext(IRepositoryOptions options)
1717

1818
DbContext context;
1919

20-
if (string.IsNullOrEmpty(options.ConnectionString))
20+
if (options.DbContextArgs == null)
2121
context = (DbContext)Activator.CreateInstance(options.DbContextType);
2222
else
23-
context = (DbContext)Activator.CreateInstance(options.DbContextType, options.ConnectionString);
23+
context = (DbContext)Activator.CreateInstance(options.DbContextType, options.DbContextArgs);
2424

2525
return context;
2626
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace DotNetToolkit.Repository.EntityFrameworkCore
2+
{
3+
using Microsoft.EntityFrameworkCore;
4+
using System;
5+
6+
/// <summary>
7+
/// An implementation of <see cref="IRepositoryFactory" />.
8+
/// </summary>
9+
public class EfCoreRepositoryFactory : IRepositoryFactory
10+
{
11+
#region Private Methods
12+
13+
private static DbContext GetDbContext(IRepositoryOptions options)
14+
{
15+
if (options.DbContextType == null)
16+
throw new InvalidOperationException($"The repository options must provide a {nameof(options.DbContextType)}.");
17+
18+
DbContext context;
19+
20+
if (options.DbContextArgs == null)
21+
context = (DbContext)Activator.CreateInstance(options.DbContextType);
22+
else
23+
context = (DbContext)Activator.CreateInstance(options.DbContextType, options.DbContextArgs);
24+
25+
return context;
26+
}
27+
28+
#endregion
29+
30+
#region Implementation of IRepositoryFactory
31+
32+
/// <summary>
33+
/// Creates a new repository for the specified entity type.
34+
/// </summary>
35+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
36+
/// <param name="options">The options.</param>
37+
/// <returns>The new repository.</returns>
38+
public IRepository<TEntity> Create<TEntity>(IRepositoryOptions options) where TEntity : class
39+
{
40+
return new EfCoreRepository<TEntity>(GetDbContext(options));
41+
}
42+
43+
/// <summary>
44+
/// Creates a new repository for the specified entity and primary key type.
45+
/// </summary>
46+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
47+
/// <typeparam name="TKey">The type of the key primary key value.</typeparam>
48+
/// <param name="options">The options.</param>
49+
/// <returns>The new repository.</returns>
50+
public IRepository<TEntity, TKey> Create<TEntity, TKey>(IRepositoryOptions options) where TEntity : class
51+
{
52+
return new EfCoreRepository<TEntity, TKey>(GetDbContext(options));
53+
}
54+
55+
#endregion
56+
}
57+
}

src/DotNetToolkit.Repository.InMemory/InMemoryRepositoryFactory.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
namespace DotNetToolkit.Repository.InMemory
22
{
3+
using System;
4+
using System.Linq;
5+
36
/// <summary>
47
/// An implementation of <see cref="IRepositoryFactory" />.
58
/// </summary>
69
public class InMemoryRepositoryFactory : IRepositoryFactory
710
{
11+
#region Private Methods
12+
13+
private string GetDatabaseName(IRepositoryOptions options)
14+
{
15+
var arg = options.DbContextArgs.FirstOrDefault();
16+
var databaseName = arg as string;
17+
18+
if (arg != null && databaseName == null)
19+
throw new ArgumentException($"The provided {nameof(options.DbContextArgs)} must be a valid string argument.");
20+
21+
return databaseName;
22+
}
23+
24+
#endregion
25+
826
#region Implementation of IRepositoryFactory
927

1028
/// <summary>
@@ -15,7 +33,7 @@ public class InMemoryRepositoryFactory : IRepositoryFactory
1533
/// <returns>The new repository.</returns>
1634
public IRepository<TEntity> Create<TEntity>(IRepositoryOptions options) where TEntity : class
1735
{
18-
return new InMemoryRepository<TEntity>(options.ConnectionString);
36+
return new InMemoryRepository<TEntity>(GetDatabaseName(options));
1937
}
2038

2139
/// <summary>
@@ -27,7 +45,7 @@ public IRepository<TEntity> Create<TEntity>(IRepositoryOptions options) where TE
2745
/// <returns>The new repository.</returns>
2846
public IRepository<TEntity, TKey> Create<TEntity, TKey>(IRepositoryOptions options) where TEntity : class
2947
{
30-
return new InMemoryRepository<TEntity, TKey>(options.ConnectionString);
48+
return new InMemoryRepository<TEntity, TKey>(GetDatabaseName(options));
3149
}
3250

3351
#endregion

src/DotNetToolkit.Repository/IRepositoryOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
public interface IRepositoryOptions
99
{
1010
/// <summary>
11-
/// Gets or sets the connection string.
11+
/// Gets or sets the database context arguments.
1212
/// </summary>
13-
string ConnectionString { get; set; }
13+
object[] DbContextArgs { get; set; }
1414

1515
/// <summary>
1616
/// Gets or sets the type of the database context.

0 commit comments

Comments
 (0)