示例#1
0
        public Mapper(IRepositoryConventions conventions)
        {
            _conventions = Guard.NotNull(conventions, nameof(conventions));

            _properties = typeof(T).GetRuntimeProperties()
                          .Where(x => x.IsPrimitive() && _conventions.IsColumnMapped(x))
                          .OrderBy(_conventions.GetColumnOrderOrDefault)
                          .ToDictionary(_conventions.GetColumnName, x => x);

            _navigationProperties = new Dictionary <Type, Dictionary <string, PropertyInfo> >();
        }
        private static Dictionary <string, PropertyInfo> GetProperties(IRepositoryConventions conventions, Type entityType)
        {
            Guard.NotNull(conventions, nameof(conventions));
            Guard.NotNull(entityType, nameof(entityType));

            return(entityType
                   .GetRuntimeProperties()
                   .Where(x => x.IsPrimitive() && conventions.IsColumnMapped(x))
                   .OrderBy(conventions.GetColumnOrderOrDefault)
                   .ToDictionary(conventions.GetColumnName, x => x));
        }
示例#3
0
        public static PropertyInfo[] GetPrimaryKeyPropertyInfos([NotNull] IRepositoryConventions conventions, [NotNull] Type entityType)
        {
            Guard.NotNull(conventions, nameof(conventions));
            Guard.NotNull(entityType, nameof(entityType));

            // Gets by checking the annotations
            var propertyInfos = entityType
                                .GetRuntimeProperties()
                                .Where(x => conventions.IsColumnMapped(x) && x.GetCustomAttribute <KeyAttribute>() != null)
                                .OrderBy(x =>
            {
                var columnAttribute = x.GetCustomAttribute <ColumnAttribute>();
                if (columnAttribute != null && columnAttribute.Order > 0)
                {
                    return(columnAttribute.Order);
                }

                return(int.MaxValue);
            })
                                .ToList();

            // Gets by naming convention
            if (!propertyInfos.Any())
            {
                foreach (var propertyName in GetDefaultPrimaryKeyNameChecks(entityType))
                {
                    var propertyInfo = entityType.GetTypeInfo().GetDeclaredProperty(propertyName);

                    if (propertyInfo != null && conventions.IsColumnMapped(propertyInfo))
                    {
                        propertyInfos.Add(propertyInfo);

                        break;
                    }
                }
            }

            return(propertyInfos.ToArray());
        }
        private Dictionary <string, PropertyInfo> GetProperties([NotNull] IRepositoryConventions conventions)
        {
            Guard.NotNull(conventions, nameof(conventions));

            if (_propertiesMapping == null || _propertiesMapping.Count == 0)
            {
                _propertiesMapping = typeof(T).GetRuntimeProperties()
                                     .Where(x => x.IsPrimitive() && conventions.IsColumnMapped(x))
                                     .ToDictionary(conventions.GetColumnName, x => x);
            }

            return(_propertiesMapping);
        }