fluent-nhibernate /src/FluentNHibernate/Testing/PersistenceSpecificationExtensions.cs

Language C# Lines 163
MD5 Hash 7e745473dc6f4b202a86e46cc8cf0464
Repository https://github.com/dotnetchris/fluent-nhibernate.git View Raw File View Project SPDX
  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
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using FluentNHibernate.Testing.Values;
using FluentNHibernate.Utils;

namespace FluentNHibernate.Testing
{
    public static class PersistenceSpecificationExtensions
    {
        public static PersistenceSpecification<T> CheckProperty<T>(this PersistenceSpecification<T> spec,
                                                                   Expression<Func<T, object>> expression, object propertyValue)
        {
            PropertyInfo property = ReflectionHelper.GetProperty(expression);

            return spec.RegisterCheckedProperty(new Property<T, object>(property, propertyValue));
        }

        public static PersistenceSpecification<T> CheckProperty<T, TListElement>(this PersistenceSpecification<T> spec,
                                                                                 Expression<Func<T, Array>> expression,
                                                                                 IEnumerable<TListElement> propertyValue)
        {
            PropertyInfo property = ReflectionHelper.GetProperty(expression);

            return spec.RegisterCheckedProperty(new List<T, TListElement>(property, propertyValue));
        }

        public static PersistenceSpecification<T> CheckProperty<T, TProperty>(this PersistenceSpecification<T> spec,
                                                                              Expression<Func<T, TProperty>> expression,
                                                                              TProperty propertyValue,
                                                                              Action<T, TProperty> propertySetter)
        {
            PropertyInfo propertyInfoFromExpression = ReflectionHelper.GetProperty(expression);

            var property = new Property<T, TProperty>(propertyInfoFromExpression, propertyValue);
            property.ValueSetter = (target, propertyInfo, value) => propertySetter(target, value);

            return spec.RegisterCheckedProperty(property);
        }

        public static PersistenceSpecification<T> CheckReference<T>(this PersistenceSpecification<T> spec,
                                                                    Expression<Func<T, object>> expression,
                                                                    object propertyValue)
        {
            PropertyInfo property = ReflectionHelper.GetProperty(expression);

            return spec.RegisterCheckedProperty(new ReferenceProperty<T, object>(property, propertyValue));
        }

        public static PersistenceSpecification<T> CheckReference<T, TProperty>(this PersistenceSpecification<T> spec,
                                                                               Expression<Func<T, TProperty>> expression,
                                                                               TProperty propertyValue,
                                                                               Action<T, TProperty> propertySetter)
        {
            PropertyInfo propertyInfoFromExpression = ReflectionHelper.GetProperty(expression);

            var property = new ReferenceProperty<T, TProperty>(propertyInfoFromExpression, propertyValue);
            property.ValueSetter = (target, propertyInfo, value) => propertySetter(target, value);

            return spec.RegisterCheckedProperty(property);
        }

        public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
                                                                             Expression<Func<T, IEnumerable<TListElement>>> expression,
                                                                             IEnumerable<TListElement> propertyValue)
        {
            PropertyInfo property = ReflectionHelper.GetProperty(expression);

            return spec.RegisterCheckedProperty(new ReferenceList<T, TListElement>(property, propertyValue));
        }

        public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
                                                                             Expression<Func<T, IEnumerable<TListElement>>> expression,
                                                                             IEnumerable<TListElement> propertyValue,
                                                                             Action<T, TListElement> listItemSetter)
        {
            PropertyInfo property = ReflectionHelper.GetProperty(expression);

            var list = new ReferenceList<T, TListElement>(property, propertyValue);
            list.ValueSetter = (target, propertyInfo, value) =>
            {
                foreach (var item in value)
                {
                    listItemSetter(target, item);
                }
            };

            return spec.RegisterCheckedProperty(list);
        }

        public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
                                                                             Expression<Func<T, IEnumerable<TListElement>>> expression,
                                                                             IEnumerable<TListElement> propertyValue,
                                                                             Action<T, IEnumerable<TListElement>> listSetter)

        {
            PropertyInfo property = ReflectionHelper.GetProperty(expression);

            var list = new ReferenceList<T, TListElement>(property, propertyValue);
            list.ValueSetter = (target, propertyInfo, value) => listSetter(target, value);

            return spec.RegisterCheckedProperty(list);
        }

        /// <summary>
        /// Checks a list of components for validity.
        /// </summary>
        /// <typeparam name="TListElement">Type of list element</typeparam>
        /// <param name="expression">Property</param>
        /// <param name="propertyValue">Value to save</param>
        public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
                                                                                      Expression<Func<T, object>> expression,
                                                                                      IEnumerable<TListElement> propertyValue)
        {
            PropertyInfo property = ReflectionHelper.GetProperty(expression);

            return spec.RegisterCheckedProperty(new List<T, TListElement>(property, propertyValue));
        }

        public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
                                                                                      Expression<Func<T, IEnumerable<TListElement>>> expression,
                                                                                      IEnumerable<TListElement> propertyValue,
                                                                                      Action<T, TListElement> listItemSetter)
        {
            PropertyInfo property = ReflectionHelper.GetProperty(expression);

            var list = new List<T, TListElement>(property, propertyValue);
            list.ValueSetter = (target, propertyInfo, value) =>
            {
                foreach (var item in value)
                {
                    listItemSetter(target, item);
                }
            };

            return spec.RegisterCheckedProperty(list);
        }

        public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
                                                                                      Expression<Func<T, IEnumerable<TListElement>>> expression,
                                                                                      IEnumerable<TListElement> propertyValue,
                                                                                      Action<T, IEnumerable<TListElement>> listSetter)
        {
            PropertyInfo property = ReflectionHelper.GetProperty(expression);

            var list = new List<T, TListElement>(property, propertyValue);
            list.ValueSetter = (target, propertyInfo, value) => listSetter(target, value);

            return spec.RegisterCheckedProperty(list);
        }

        [Obsolete("CheckEnumerable has been replaced with CheckList")]
        public static PersistenceSpecification<T> CheckEnumerable<T, TItem>(this PersistenceSpecification<T> spec,
                                                                            Expression<Func<T, IEnumerable<TItem>>> expression,
                                                                            Action<T, TItem> addAction,
                                                                            IEnumerable<TItem> itemsToAdd)
        {
            return spec.CheckList(expression, itemsToAdd, addAction);
        }
    }
}
Back to Top