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
156 lines (128 loc) · 7.24 KB
/
RepositoryFactory.cs
File metadata and controls
156 lines (128 loc) · 7.24 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
using System;
using System.Configuration;
using System.Linq;
using SharpRepository.Repository.Configuration;
using SharpRepository.Repository.Helpers;
namespace SharpRepository.Repository
{
public static class RepositoryFactory
{
private const string DefaultConfigSection = "sharpRepository";
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>(DefaultConfigSection, repositoryName);
}
public static object GetInstance(Type entityType, string repositoryName = null)
{
return GetInstance(entityType, DefaultConfigSection, repositoryName);
}
public static object GetInstance(Type entityType, Type keyType, string repositoryName = null)
{
return GetInstance(entityType, keyType, DefaultConfigSection, repositoryName);
}
public static IRepository<T, TKey> GetInstance<T, TKey>(string configSection, string repositoryName) where T : class, new()
{
return GetInstance<T, TKey>(GetConfiguration(configSection), repositoryName);
}
public static object GetInstance(Type entityType, string configSection, string repositoryName)
{
return GetInstance(entityType, GetConfiguration(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)
{
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`1");
var genericMethod = method.MakeGenericMethod(entityType);
return genericMethod.Invoke(configuration, new object[] { 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>(DefaultConfigSection, repositoryName);
}
public static object GetInstance(Type entityType, Type keyType, Type key2Type, string repositoryName = null)
{
return GetInstance(entityType, keyType, key2Type, DefaultConfigSection, repositoryName);
}
public static ICompoundKeyRepository<T, TKey, TKey2> GetInstance<T, TKey, TKey2>(string configSection, string repositoryName) where T : class, new()
{
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;
}
}
}