2 using System.Collections.Generic;
3 using System.Linq.Expressions;
6 namespace Deveel.Data.Mapping {
12 TableName = DiscoverTableName();
13 memberMappings = DiscoverMembers();
16 private string TableName {
get; set; }
18 private string UniqueKeyMember {
get; set; }
21 get {
return typeof (T); }
25 get {
return TableName; }
29 get {
return UniqueKeyMember; }
33 get {
return memberMappings; }
37 var dictionary =
new Dictionary<string, IMemberMappingConfiguration>();
39 var members = typeof (T).FindMembers(MemberTypes.Field | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Public, null, null);
40 foreach (var memberInfo
in members) {
41 if (!Attribute.IsDefined(memberInfo, typeof (
IgnoreAttribute),
false)) {
44 Ignore(memberInfo.Name);
52 var tableNameAttr = Attribute.GetCustomAttribute(typeof(T), typeof(
TableAttribute),
false)
54 if (tableNameAttr == null)
59 private MemberInfo GetMemberInfo<TProperty>(Expression<Func<T, TProperty>> memberSelector) {
62 var member = memberSelector.Body as MemberExpression;
64 throw new ArgumentException(
string.Format(
"Expression '{0}' refers to a method, not a property.", memberSelector));
66 var memberInfo = member.Member;
67 if (memberInfo == null)
68 throw new ArgumentException(
string.Format(
"Expression '{0}' not refers to a field or a property.", memberSelector));
70 if (type != memberInfo.ReflectedType &&
71 !type.IsSubclassOf(memberInfo.ReflectedType))
72 throw new ArgumentException(
string.Format(
"Expresion '{0}' refers to a member that is not from type {1}.", memberSelector, type));
83 if (!String.IsNullOrEmpty(UniqueKeyMember))
84 throw new InvalidOperationException(
"A unique key member is already configured.");
86 var memberInfo = GetMemberInfo(member);
87 UniqueKeyMember = memberInfo.Name;
92 var memberInfo = GetMemberInfo(member);
93 var memberName = memberInfo.Name;
95 if (ignoredMembers != null &&
96 ignoredMembers.Contains(memberName))
97 throw new InvalidOperationException(String.Format(
"Member '{0}' is ignored and cannot be configured.", memberName));
100 if (!memberMappings.TryGetValue(memberName, out configuration)) {
102 memberMappings[memberName] = configuration;
108 public void Ignore<TMember>(Expression<Func<T, TMember>> member) {
109 var memberInfo = GetMemberInfo(member);
110 var memberName = memberInfo.Name;
115 internal void Ignore(
string memberName) {
116 if (ignoredMembers == null)
117 ignoredMembers =
new List<string>();
119 if (!ignoredMembers.Contains(memberName))
120 ignoredMembers.Add(memberName);
string DiscoverTableName()
TypeMappingConfiguration< T > ToTable(string name)
IEnumerable< KeyValuePair< string, IMemberMappingConfiguration > > Members
void Ignore(string memberName)
Dictionary< string, IMemberMappingConfiguration > DiscoverMembers()
TypeMappingConfiguration()
List< string > ignoredMembers
Dictionary< string, IMemberMappingConfiguration > memberMappings