Exemple #1
0
        public TrackablePocoRedisMapper(RedisTypeConverter typeConverter = null)
        {
            if (typeConverter == null)
            {
                typeConverter = RedisTypeConverter.Instance;
            }

            _trackableType = TrackableResolver.GetPocoTrackerType(typeof(T));
            if (_trackableType == null)
            {
                throw new ArgumentException($"Cannot find type '{typeof(T).Name}'");
            }

            _items = ConstructPropertyItems(typeConverter);
            _fieldNameToItemMap    = _items.ToDictionary(x => x.FieldName, y => y);
            _propertyInfoToItemMap = _items.ToDictionary(x => x.PropertyInfo, y => y);
        }
        public TrackablePocoMongoDbMapper()
        {
            _trackableType = TrackableResolver.GetPocoTrackerType(typeof(T));
            TypeMapper.RegisterTrackablePocoMap(_trackableType);

            _propertyToMemberMap = new Dictionary <PropertyInfo, BsonMemberMap>();

            var classMap = BsonClassMap.LookupClassMap(_trackableType);

            foreach (var property in typeof(T).GetProperties())
            {
                if (property.Name.ToLower() == "id")
                {
                    _idProperty = property;
                }

                var member = classMap.AllMemberMaps.FirstOrDefault(m => m.MemberInfo.Name == property.Name);
                if (member != null)
                {
                    _propertyToMemberMap[property] = member;
                }
            }
        }
        public TrackablePocoSqlMapper(ISqlProvider sqlProvider,
                                      string tableName, ColumnDefinition[] headKeyColumnDefs = null)
        {
            _sqlProvider = sqlProvider;

            _trackableType = TrackableResolver.GetPocoTrackerType(typeof(T));
            if (_trackableType == null)
            {
                throw new ArgumentException($"Cannot find type '{typeof(T).Name}'");
            }

            _tableName        = tableName;
            _tableEscapedName = _sqlProvider.EscapeName(tableName);

            var allColumns        = new List <ColumnProperty>();
            var headKeyColumns    = new List <ColumnProperty>();
            var primaryKeyColumns = new List <ColumnProperty>();
            var valueColumns      = new List <ColumnProperty>();

            // add head key columns

            if (headKeyColumnDefs != null)
            {
                foreach (var headKeyColumnDef in headKeyColumnDefs)
                {
                    var column = new ColumnProperty(
                        name: headKeyColumnDef.Name,
                        escapedName: _sqlProvider.EscapeName(headKeyColumnDef.Name),
                        type: headKeyColumnDef.Type,
                        length: headKeyColumnDef.Length,
                        convertToSqlValue: _sqlProvider.GetConvertToSqlValueFunc(headKeyColumnDef.Type));

                    headKeyColumns.Add(column);
                    primaryKeyColumns.Add(column);
                    allColumns.Add(column);
                }
            }

            // while scan properties of T, construct primaryKey, value column information.

            var valueType = typeof(T);

            foreach (var property in valueType.GetProperties())
            {
                var columnName = property.Name;
                var primaryKey = false;
                var isIdentity = false;

                var attr = property.GetCustomAttribute <TrackablePropertyAttribute>();
                if (attr != null)
                {
                    if (attr["sql.ignore"] != null)
                    {
                        continue;
                    }
                    columnName = attr["sql.column:"] ?? columnName;
                    primaryKey = attr["sql.primary-key"] != null;
                    isIdentity = attr["sql.identity"] != null;
                }

                var column = new ColumnProperty(
                    name: columnName,
                    escapedName: _sqlProvider.EscapeName(columnName),
                    type: property.PropertyType,
                    isIdentity: isIdentity,
                    propertyInfo: property,
                    convertToSqlValue: _sqlProvider.GetConvertToSqlValueFunc(property.PropertyType),
                    convertFromDbValue: _sqlProvider.GetConvertFromDbValueFunc(property.PropertyType),
                    extractToSqlValue: _sqlProvider.GetExtractToSqlValueFunc(property),
                    installFromDbValue: _sqlProvider.GetInstallFromDbValueFunc(property));

                if (primaryKey)
                {
                    primaryKeyColumns.Add(column);
                }

                valueColumns.Add(column);
                allColumns.Add(column);

                if (isIdentity)
                {
                    _identityColumn = column;
                }
            }

            if (primaryKeyColumns.Any() == false)
            {
                var idColumn = allColumns.FirstOrDefault(p => p.Name.ToLower() == "id");
                if (idColumn != null)
                {
                    primaryKeyColumns.Add(idColumn);
                }
            }

            if (primaryKeyColumns.Any() == false)
            {
                throw new ArgumentException("At lease one primary key should be provided.");
            }

            _allColumns        = allColumns.ToArray();
            _headKeyColumns    = headKeyColumns.ToArray();
            _primaryKeyColumns = primaryKeyColumns.ToArray();
            _valueColumns      = valueColumns.ToArray();
            _valueColumnMap    = _valueColumns.ToDictionary(x => x.PropertyInfo, y => y);

            _allColumnStringExceptIdentity = string.Join(
                ",", _allColumns.Where(c => c.IsIdentity == false).Select(c => c.EscapedName));
            _allColumnStringExceptHead = string.Join(
                ",", _valueColumns.Select(c => c.EscapedName));
        }