-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathEfCoreConfigRepositoryFactory.cs
More file actions
99 lines (81 loc) · 3.54 KB
/
Copy pathEfCoreConfigRepositoryFactory.cs
File metadata and controls
99 lines (81 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using Microsoft.EntityFrameworkCore;
using SharpRepository.Repository;
using SharpRepository.Repository.Configuration;
using SharpRepository.Repository.Ioc;
using System;
namespace SharpRepository.EfCoreRepository
{
public class EfCoreConfigRepositoryFactory : ConfigRepositoryFactory
{
public EfCoreConfigRepositoryFactory(IRepositoryConfiguration config)
: base(config)
{
}
public EfCoreConfigRepositoryFactory(IRepositoryConfiguration config, DbContext dbContext)
: base(config)
{
DbContext = dbContext;
}
protected DbContext DbContext { get; set; }
public override IRepository<T> GetInstance<T>()
{
return new EfCoreRepository<T>(GetDbContext());
}
public override IRepository<T, TKey> GetInstance<T, TKey>()
{
return new EfCoreRepository<T, TKey>(GetDbContext());
}
public override ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>()
{
return new EfCoreRepository<T, TKey, TKey2>(GetDbContext());
}
public override ICompoundKeyRepository<T, TKey, TKey2, TKey3> GetInstance<T, TKey, TKey2, TKey3>()
{
return new EfCoreRepository<T, TKey, TKey2, TKey3>(GetDbContext());
}
public override ICompoundKeyRepository<T> GetCompoundKeyInstance<T>()
{
return new EfCoreCompoundKeyRepository<T>(GetDbContext());
}
protected DbContext GetDbContext()
{
// check for required parameters
if (RepositoryDependencyResolver.Current == null && DbContext == null)
{
throw new ConfigurationErrorsException("The EfCore repository Factory gets DbContext or DbContextOptionBuilder from RepositoryDependencyResolver containing the Ioc container passing directly DbContextOptions");
}
if (DbContext != null)
{
return DbContext;
}
Type dbContextType = null;
var tmpDbContextType = RepositoryConfiguration["dbContextType"];
if (!String.IsNullOrEmpty(tmpDbContextType))
{
dbContextType = Type.GetType(tmpDbContextType);
if (dbContextType == null)
{
throw new NotImplementedException("Unable to find " + tmpDbContextType + " class");
}
}
// TODO: look at dbContextType (from Enyim.Caching configuration bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
DbContext dbContext = dbContextType == null
? RepositoryDependencyResolver.Current?.GetService<DbContext>()
: (DbContext)RepositoryDependencyResolver.Current?.GetService(dbContextType);
// if the Ioc container doesn't throw an error but still returns null we need to alert the consumer
if (dbContext != null)
{
return dbContext;
}
var options = RepositoryDependencyResolver.Current.GetService<DbContextOptions>();
if (options == null)
{
throw new RepositoryDependencyResolverException(typeof(DbContext));
}
dbContext = dbContextType == null
? new DbContext(options)
: (DbContext)Activator.CreateInstance(dbContextType, options);
return dbContext;
}
}
}