-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathDefaultRepositoryConventions.cs
More file actions
41 lines (33 loc) · 2.14 KB
/
Copy pathDefaultRepositoryConventions.cs
File metadata and controls
41 lines (33 loc) · 2.14 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SharpRepository.Repository.Helpers;
namespace SharpRepository.Repository
{
public static class DefaultRepositoryConventions
{
public static string PrimaryKeySuffix = "Id";
public static string CachePrefix = "#Repo";
public static Func<Type, string> GetPrimaryKeyName = entityType =>
{
var propInfo = entityType.GetRuntimeProperties().FirstOrDefault(x => x.HasAttribute<RepositoryPrimaryKeyAttribute>());
if (propInfo != null) return propInfo.Name;
foreach (var propertyName in GetPrimaryKeyNameChecks(entityType))
{
propInfo = GetPropertyCaseInsensitive(entityType, propertyName);
if (propInfo != null) return propInfo.Name;
}
return null;
};
private static readonly Func<Type, IEnumerable<string>> GetPrimaryKeyNameChecks = type =>
{
var suffix = PrimaryKeySuffix;
return new[] {suffix, type.Name + suffix};
};
private static PropertyInfo GetPropertyCaseInsensitive(Type type, string propertyName)
{
return type.GetRuntimeProperties().Where(pi => pi.Name.ToLowerInvariant() == propertyName.ToLowerInvariant()).FirstOrDefault();
}
}
}