-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassMapping.cs
91 lines (83 loc) · 3.33 KB
/
ClassMapping.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
namespace NaiveORM
{
/*
* entre autres, on va implémenter les OneToMany, ManyToOne, OneToOne etc.
*/
partial class ClassMapping<T> : IClassMap<T>, IAttributeMap //partial
{
public void AddAttribute(Attribute _Property, Expression<Func<T, object>> expression)
{
string[] _SplitName=null;
if (expression.Body.NodeType == ExpressionType.Convert)
{
_Property.Name = expression.Body.ToString().Replace("Convert(", "").Replace(")", "");
string[] _Splitter = { "." };
_SplitName = _Property.Name.Split(_Splitter, StringSplitOptions.None);
_Property.Name = _SplitName[_SplitName.Length - 1]; //last?
UnaryExpression _TempUnaryExp = (UnaryExpression)expression.Body;
_Property.Type = _TempUnaryExp.Operand.Type;
}
else
{
_Property.Name = expression.Body.ToString();
string[] _Splitter = {"."};
_SplitName = _Property.Name.Split(_Splitter, StringSplitOptions.None);
_Property.Name = _SplitName[_Splitter.Length-1];
_Property.Type = expression.Body.Type;
}
_Properties.Add(_Property);
}
public void ID(Expression<Func<T, object>> expression)
{
Attribute _ID = new Attribute();
_ID.AttributeType = AttributeType.ID;
AddAttribute(_ID, expression); //from 'ClassMapping<T>', juste au dessus
}
public void Reference(Expression<Func<T, object>> expression) //donc on crée un nouveau attribut qui est une référence ici
{
Attribute _ID = new Attribute();
_ID.AttributeType = AttributeType.ID;
AddAttribute(_ID, expression);
}
// Mapping
public void Map(Expression<Func<T,object>> expression,
string _MappedProperty, bool _Cascade)
{
Attribute _ID = new Attribute();
_ID.AttributeType = AttributeType.Map;
_ID.MappedProperty = _MappedProperty;
_ID.Cascade = _Cascade;
AddAttribute(_ID, expression);
}
public void ManyToOne(Expression<Func<T,object>> expression,
string _MappedProperty, bool _Cascade)
{
Attribute _ID = new Attribute();
_ID.AttributeType = AttributeType.ManyToOne;
_ID.MappedProperty = _MappedProperty;
_ID.Cascade = _Cascade;
AddAttribute(_ID, expression);
}
public void ManyToMany(Expression<Func<T, object>> expression,
string _MappedProperty, bool _Cascade)
{
Attribute _ID = new Attribute();
_ID.AttributeType = AttributeType.ManyToMany;
_ID.MappedProperty = _MappedProperty;
_ID.Cascade = _Cascade;
AddAttribute(_ID, expression);
}
#region IAttributeMap Members
public List<Attribute> Properties
{
get { return _Properties; }
set { _Properties = value; }
}
#endregion
}
}