forked from SharpRepository/SharpRepository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositoryFactory.cs
More file actions
189 lines (158 loc) · 8.43 KB
/
Copy pathRepositoryFactory.cs
File metadata and controls
189 lines (158 loc) · 8.43 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
//using System.Collections.Concurrent;
//using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using SharpRepository.Repository.Configuration;
using SharpRepository.Repository.Helpers;
namespace SharpRepository.Repository
{
public static class RepositoryFactory
{
// private static readonly IDictionary<string, ISharpRepositoryConfiguration> _cache;
// private static ISharpRepositoryConfiguration _configuration = null;
//
// static RepositoryFactory()
// {
// _cache = new ConcurrentDictionary<string, ISharpRepositoryConfiguration>();
// }
public static IRepository<T, int> GetInstance<T>(string repositoryName = null) where T : class, new()
{
return GetInstance<T, int>(repositoryName);
}
public static IRepository<T, int> GetInstance<T>(string configSection, string repositoryName) where T : class, new()
{
return GetInstance<T, int>(configSection, repositoryName);
}
public static IRepository<T, int> GetInstance<T>(ISharpRepositoryConfiguration configuration, string repositoryName=null) where T : class, new()
{
return GetInstance<T, int>(configuration, repositoryName);
}
public static IRepository<T, TKey> GetInstance<T, TKey>(string repositoryName = null) where T : class, new()
{
return GetInstance<T, TKey>("sharpRepository", repositoryName);
}
public static object GetInstance(Type entityType, string repositoryName = null)
{
return GetInstance(entityType, typeof(int), repositoryName);
}
public static object GetInstance(Type entityType, Type keyType, string repositoryName = null)
{
return GetInstance(entityType, keyType, "sharpRepository", repositoryName);
}
public static IRepository<T, TKey> GetInstance<T, TKey>(string configSection, string repositoryName) where T : class, new()
{
// ISharpRepositoryConfiguration configuration;
// var key = String.Format("{0}::{1}::{2}", typeof (T).FullName, typeof (TKey).Name, configSection);
// // first check cache
// if (_cache.ContainsKey(key))
// {
// configuration = _cache[key];
// }
// else
// {
// configuration = GetConfiguration(configSection);
// _cache[key] = configuration;
// }
// if (_configuration == null)
// _configuration = GetConfiguration(configSection);
return GetInstance<T, TKey>(GetConfiguration(configSection), repositoryName);
}
public static object GetInstance(Type entityType, string configSection, string repositoryName)
{
return GetInstance(entityType, typeof(int), configSection, repositoryName);
}
public static object GetInstance(Type entityType, Type keyType, string configSection, string repositoryName)
{
return GetInstance(entityType, keyType, GetConfiguration(configSection), repositoryName);
}
public static IRepository<T, TKey> GetInstance<T, TKey>(ISharpRepositoryConfiguration configuration, string repositoryName=null) where T : class, new()
{
if (String.IsNullOrEmpty(repositoryName))
{
// if no specific repository is provided then check to see if the SharpRepositoryConfigurationAttribute is used
repositoryName = GetAttributeRepositoryName(typeof (T));
}
return configuration.GetInstance<T, TKey>(repositoryName);
}
public static object GetInstance(Type entityType, ISharpRepositoryConfiguration configuration,
string repositoryName = null)
{
return GetInstance(entityType, typeof (int), configuration, repositoryName);
}
public static object GetInstance(Type entityType, Type keyType, ISharpRepositoryConfiguration configuration, string repositoryName = null)
{
if (String.IsNullOrEmpty(repositoryName))
{
// if no specific repository is provided then check to see if the SharpRepositoryConfigurationAttribute is used
repositoryName = GetAttributeRepositoryName(entityType);
}
var method = typeof(ISharpRepositoryConfiguration).GetMethods().First(m => m.Name == "GetInstance" && m.ReturnType.Name == "IRepository`2");
var genericMethod = method.MakeGenericMethod(entityType, keyType);
return genericMethod.Invoke(configuration, new object[] { repositoryName });
}
// compound key methods
public static ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>(string repositoryName = null) where T : class, new()
{
return GetInstance<T, TKey, TKey2>("sharpRepository", repositoryName);
}
public static object GetInstance(Type entityType, Type keyType, Type key2Type, string repositoryName = null)
{
return GetInstance(entityType, keyType, key2Type, "sharpRepository", repositoryName);
}
public static ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>(string configSection, string repositoryName) where T : class, new()
{
// ISharpRepositoryConfiguration configuration;
// var key = String.Format("{0}::{1}::{2}", typeof (T).FullName, typeof (TKey).Name, configSection);
// // first check cache
// if (_cache.ContainsKey(key))
// {
// configuration = _cache[key];
// }
// else
// {
// configuration = GetConfiguration(configSection);
// _cache[key] = configuration;
// }
// if (_configuration == null)
// _configuration = GetConfiguration(configSection);
return GetInstance<T, TKey, TKey2>(GetConfiguration(configSection), repositoryName);
}
public static object GetInstance(Type entityType, Type keyType, Type key2Type, string configSection, string repositoryName)
{
return GetInstance(entityType, keyType, key2Type, GetConfiguration(configSection), repositoryName);
}
public static ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>(ISharpRepositoryConfiguration configuration, string repositoryName = null) where T : class, new()
{
if (String.IsNullOrEmpty(repositoryName))
{
// if no specific repository is provided then check to see if the SharpRepositoryConfigurationAttribute is used
repositoryName = GetAttributeRepositoryName(typeof(T));
}
return configuration.GetInstance<T, TKey, TKey2>(repositoryName);
}
public static object GetInstance(Type entityType, Type keyType, Type key2Type,ISharpRepositoryConfiguration configuration, string repositoryName = null)
{
if (String.IsNullOrEmpty(repositoryName))
{
// if no specific repository is provided then check to see if the SharpRepositoryConfigurationAttribute is used
repositoryName = GetAttributeRepositoryName(entityType);
}
var method = typeof(ISharpRepositoryConfiguration).GetMethods().First(m => m.Name == "GetInstance" && m.ReturnType.Name == "ICompoundKeyRepository`3");
var genericMethod = method.MakeGenericMethod(entityType, keyType, key2Type);
return genericMethod.Invoke(configuration, new object[] { repositoryName });
}
private static SharpRepositorySection GetConfiguration(string sectionName)
{
var section = ConfigurationManager.GetSection(sectionName) as SharpRepositorySection;
if (section == null)
throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
return section;
}
private static string GetAttributeRepositoryName(Type entityType)
{
var attribute = entityType.GetOneAttribute<SharpRepositoryConfigurationAttribute>();
return attribute == null ? null : attribute.RepositoryName;
}
}
}