Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RecordConverter.cs « Internal « Update « Mapping « Data « System « System.Data.Entity « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e6dfbe7aefb075912a84d628e158bd823ab4eb26 (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
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
//---------------------------------------------------------------------
// <copyright file="RecordConverter.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------

namespace System.Data.Mapping.Update.Internal
{
    using System.Data.Entity;

    /// <summary>
    /// Converts records to new instance expressions. Assumes that all inputs come from a single data reader (because
    /// it caches record layout). If multiple readers are used, multiple converters must be constructed in case
    /// the different readers return different layouts for types.
    /// </summary>
    /// <remarks>
    /// Conventions for modifiedProperties enumeration: null means all properties are modified, empty means none,
    /// non-empty means some.
    /// </remarks>
    internal class RecordConverter
    {
        #region Constructors
        /// <summary>
        /// Initializes a new converter given a command tree context. Initializes a new record layout cache.
        /// </summary>
        /// <param name="updateTranslator">Sets <see cref="m_updateTranslator" /></param>
        internal RecordConverter(UpdateTranslator updateTranslator)
        {
            m_updateTranslator = updateTranslator;
        }
        #endregion

        #region Fields
        /// <summary>
        /// Context used to produce expressions.
        /// </summary>
        private UpdateTranslator m_updateTranslator;
        #endregion

        #region Methods
        /// <summary>
        /// Converts original values in a state entry to a DbNewInstanceExpression. The record must be either an entity or 
        /// a relationship set instance.
        /// </summary>
        /// <remarks>
        /// This method is not thread safe.
        /// </remarks>
        /// <param name="stateEntry">Gets state entry this record is associated with.</param>
        /// <param name="modifiedPropertiesBehavior">Indicates how to determine whether a property is modified.</param>
        /// <returns>New instance expression.</returns>
        internal PropagatorResult ConvertOriginalValuesToPropagatorResult(IEntityStateEntry stateEntry, ModifiedPropertiesBehavior modifiedPropertiesBehavior)
        {
            return ConvertStateEntryToPropagatorResult(stateEntry, useCurrentValues: false, modifiedPropertiesBehavior: modifiedPropertiesBehavior);
        }

        /// <summary>
        /// Converts current values in a state entry to a DbNewInstanceExpression. The record must be either an entity or 
        /// a relationship set instance.
        /// </summary>
        /// <remarks>
        /// This method is not thread safe.
        /// </remarks>
        /// <param name="stateEntry">Gets state entry this record is associated with.</param>
        /// <param name="modifiedPropertiesBehavior">Indicates how to determine whether a property is modified.</param>
        /// <returns>New instance expression.</returns>
        internal PropagatorResult ConvertCurrentValuesToPropagatorResult(IEntityStateEntry stateEntry, ModifiedPropertiesBehavior modifiedPropertiesBehavior)
        {
            return ConvertStateEntryToPropagatorResult(stateEntry, useCurrentValues: true, modifiedPropertiesBehavior: modifiedPropertiesBehavior);
        }

        private PropagatorResult ConvertStateEntryToPropagatorResult(IEntityStateEntry stateEntry, bool useCurrentValues, ModifiedPropertiesBehavior modifiedPropertiesBehavior)
        {
            try
            {
                EntityUtil.CheckArgumentNull(stateEntry, "stateEntry");
                IExtendedDataRecord record = useCurrentValues
                    ? EntityUtil.CheckArgumentNull(stateEntry.CurrentValues as IExtendedDataRecord, "stateEntry.CurrentValues")
                    : EntityUtil.CheckArgumentNull(stateEntry.OriginalValues as IExtendedDataRecord, "stateEntry.OriginalValues");

                bool isModified = false; // the root of the state entry is unchanged because the type is static
                return ExtractorMetadata.ExtractResultFromRecord(stateEntry, isModified, record, useCurrentValues, m_updateTranslator, modifiedPropertiesBehavior);
            }
            catch (Exception e)
            {
                if (UpdateTranslator.RequiresContext(e))
                {
                    throw EntityUtil.Update(Strings.Update_ErrorLoadingRecord, e, stateEntry);
                }
                throw;
            }
        }
        #endregion
    }
}