aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/Serialization/EntitySerializationStrategy.cs
blob: 728238b6b90f5433de904de8685411846e9c991d (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Tango.BL.Serialization
{
    public class EntitySerializationStrategy
    {
        public List<MemberInfo> IgnoreProperties { get; private set; }
        public List<MemberInfo> IncludeProperties { get; private set; }

        public EntitySerializationStrategy()
        {
            IgnoreProperties = new List<MemberInfo>();
            IncludeProperties = new List<MemberInfo>();
        }

        public EntitySerializationStrategy Ignore<T>(Expression<Func<T>> propertyLambda)
        {
            var me = propertyLambda.Body as MemberExpression;

            if (me == null)
            {
                throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'");
            }

            return Ignore(me.Member);
        }

        public EntitySerializationStrategy Ignore(MemberInfo memberInfo)
        {
            IgnoreProperties.Add(memberInfo);
            return this;
        }

        public EntitySerializationStrategy Include<T>(Expression<Func<T>> propertyLambda)
        {
            var me = propertyLambda.Body as MemberExpression;

            if (me == null)
            {
                throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'");
            }

            return Include(me.Member);
        }

        public EntitySerializationStrategy Include(MemberInfo memberInfo)
        {
            IncludeProperties.Add(memberInfo);
            IgnoreProperties.Remove(memberInfo);
            return this;
        }
    }
}