fluent-nhibernate /src/FluentNHibernate/Mapping/NaturalIdPart.cs

Language C# Lines 124
MD5 Hash cab76c94b12770f0117fad033af5f4b4
Repository https://github.com/barriault/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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using FluentNHibernate.Mapping.Providers;
using FluentNHibernate.MappingModel;
using FluentNHibernate.Utils;

namespace FluentNHibernate.Mapping
{
    public class NaturalIdPart<T> : INaturalIdMappingProvider
    {
        private readonly AttributeStore<NaturalIdMapping> attributes = new AttributeStore<NaturalIdMapping>();
        private readonly IList<PropertyMapping> properties = new List<PropertyMapping>();
        private readonly IList<ManyToOneMapping> manyToOnes = new List<ManyToOneMapping>();
        private bool nextBool = true;

        public NaturalIdPart() { }

        /// <summary>
        /// Defines a property to be used for this natural-id.
        /// </summary>
        /// <param name="expression">A member access lambda expression for the property</param>
        /// <returns>The natural id part fluent interface</returns>
        public NaturalIdPart<T> Property(Expression<Func<T, object>> expression)
        {
            var member = expression.ToMember();
            return Property(expression, member.Name);
        }

        /// <summary>
        /// Defines a property to be used for this natural-id with an explicit column name.
        /// </summary>
        /// <param name="expression">A member access lambda expression for the property</param>
        /// <param name="columnName">The column name in the database to use for this natural id, or null to use the property name</param>
        /// <returns>The natural id part fluent interface</returns>
        public NaturalIdPart<T> Property(Expression<Func<T, object>> expression, string columnName)
        {
            var member = expression.ToMember();
            return Property(member, columnName);
        }

        protected virtual NaturalIdPart<T> Property(Member member, string columnName)
        {
            var key = new PropertyMapping
            {
                Name = member.Name,
                Type = new TypeReference(member.PropertyType)
            };
            key.AddColumn(new ColumnMapping { Name = columnName });

            properties.Add(key);

            return this;
        }

        /// <summary>
        /// Defines a reference to be used as a many-to-one key for this natural-id with an explicit column name.
        /// </summary>
        /// <param name="expression">A member access lambda expression for the property</param>
        /// <returns>The natural ID part fluent interface</returns>
        public NaturalIdPart<T> Reference(Expression<Func<T, object>> expression)
        {
            var member = expression.ToMember();
            return Reference(expression, member.Name);
        }

        /// <summary>
        /// Defines a reference to be used as a many-to-one key for this natural-id with an explicit column name.
        /// </summary>
        /// <param name="expression">A member access lambda expression for the property</param>
        /// <param name="columnName">The column name in the database to use for this key, or null to use the property name</param>
        /// <returns>The natural id part fluent interface</returns>
        public NaturalIdPart<T> Reference(Expression<Func<T, object>> expression, string columnName)
        {
            var member = expression.ToMember();
            return Reference(expression, columnName);
        }

        protected virtual NaturalIdPart<T> Reference(Member member, string columnName)
        {
            var key = new ManyToOneMapping
            {
                Name = member.Name,
                Class = new TypeReference(member.PropertyType),
                ContainingEntityType = typeof(T)
            };
            key.AddColumn(new ColumnMapping { Name = columnName });

            manyToOnes.Add(key);

            return this;
        }

        public NaturalIdPart<T> ReadOnly()
        {
            attributes.Set(x => x.Mutable, nextBool);
            nextBool = true;
            return this;
        }

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public NaturalIdPart<T> Not
        {
            get
            {
                nextBool = false;
                return this;
            }
        }

        NaturalIdMapping INaturalIdMappingProvider.GetNaturalIdMapping()
        {
            var mapping = new NaturalIdMapping(attributes.CloneInner());

            properties.Each(mapping.AddProperty);
            manyToOnes.Each(mapping.AddReference);

            return mapping;
        }
    }
}
Back to Top