Пример #1
0
        /// <summary>
        /// Creates a new mapping description for a type of business object.
        /// </summary>
        public OrmEntityMap()
        {
            var staticFieldsInfo = Utilities.GetStaticFields <T>();

            var identityFieldInfo = staticFieldsInfo.FirstOrDefault(f => typeof(OrmIdentityMapBase <T>).IsAssignableFrom(f.FieldType));

            if (identityFieldInfo.IsNull())
            {
                throw new NotSupportedException(string.Format(
                                                    Properties.Resources.MicroOrmIsNotSupportedByTypeException, typeof(T).FullName));
            }
            _identity = (OrmIdentityMapBase <T>)identityFieldInfo.GetValue(null);

            _fields = staticFieldsInfo.Where(f => typeof(OrmFieldMapBase <T>).IsAssignableFrom(f.FieldType))
                      .Select(f => (OrmFieldMapBase <T>)f.GetValue(null)).ToList();

            if (null == _fields || _fields.Count < 1)
            {
                throw new NotSupportedException(
                          string.Format(Properties.Resources.NoFieldMapsForMicroOrmException, typeof(T).FullName));
            }

            _insertedAt = _fields.FirstOrDefault(f => f.GetType() == typeof(OrmFieldMapInsertedAt <T>)) as OrmFieldMapInsertedAt <T>;
            _insertedBy = _fields.FirstOrDefault(f => f.GetType() == typeof(OrmFieldMapInsertedBy <T>)) as OrmFieldMapInsertedBy <T>;
            _updatedAt  = _fields.FirstOrDefault(f => f.GetType() == typeof(OrmFieldMapUpdatedAt <T>)) as OrmFieldMapUpdatedAt <T>;
            _updatedBy  = _fields.FirstOrDefault(f => f.GetType() == typeof(OrmFieldMapUpdatedBy <T>)) as OrmFieldMapUpdatedBy <T>;

            _updateStatements = new ConcurrentDictionary <int, string>(Environment.ProcessorCount * 2, 10);

            if (_identity.PrimaryKeyAutoIncrement || !_identity.PrimaryKeyUpdatable)
            {
                PrimaryKeyUpdateWhereParamName = _identity.PrimaryKeyFieldName;
            }
            else
            {
                PrimaryKeyUpdateWhereParamName = null;
                for (int i = 0; i < _paramNameCandidats.Length; i++)
                {
                    if (!_fields.Any(f => f.DbFieldName.EqualsByConvention(_paramNameCandidats[i])))
                    {
                        PrimaryKeyUpdateWhereParamName = _paramNameCandidats[i];
                        break;
                    }
                }
                if (PrimaryKeyUpdateWhereParamName.IsNullOrWhiteSpace())
                {
                    throw new NotSupportedException(
                              "Failed to resolve UPDATE statement WHERE clause parameter name.");
                }
            }
        }
        internal void InitValue(T instance, string userId, OrmFieldMapUpdatedBy <T> updatedByFieldMap)
        {
            if (updatedByFieldMap.IsNull())
            {
                throw new ArgumentNullException(nameof(updatedByFieldMap));
            }
            if (userId.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException(nameof(userId));
            }

            ValueSetter(instance, userId);
            updatedByFieldMap.ValueSetter(instance, userId);
        }