public JoinTableQueryPart(QueryIdentifier targetTable, QueryIdentifier sourceTable, QueryIdentifier joinAlias, Type targetTargetTableType, ColumnInfo targetColumn, ColumnInfo sourceColumn, IEnumerable <ColumnInfo> columns, DbPropertyInfoCache targetProperty, JoinMode joinAs) { _joinAs = joinAs; TargetTable = targetTable; SourceTable = sourceTable; Alias = joinAlias; TargetTableType = targetTargetTableType; Columns = columns; TargetColumn = targetColumn; SourceColumn = sourceColumn; DependingJoins = new List <JoinTableQueryPart>(); var joinInfos = new JoinParseInfo(); joinInfos.TargetProperty = targetProperty; joinInfos.Columns = Columns; joinInfos.Alias = Alias; joinInfos.SourceColumnName = SourceColumn; joinInfos.TargetColumnName = TargetColumn; joinInfos.SourceTable = SourceTable; joinInfos.TargetTableType = targetTargetTableType; JoinParseInfo = joinInfos; }
internal static IEnumerable <Attribute> GetCustomAttributes(this DbPropertyInfoCache type) { if (IsAnonymousType(type.DeclaringClass)) { return(new Attribute[0]); //Anonymous types does not have any Attributes } return(type.Attributes.Select(s => s.Attribute)); }
/// <summary> /// Checks the info declaring type to be an List /// </summary> /// <param name="info">The information.</param> /// <returns></returns> public static bool CheckForListInterface(this DbPropertyInfoCache info) { if (info.PropertyType == typeof(string)) { return(false); } if (info.PropertyType.GetInterface(typeof(IEnumerable).Name) != null) { return(true); } return(info.PropertyType.GetInterface(typeof(IEnumerable <>).Name) != null); }
/// <summary> /// Gets the converted value. /// </summary> /// <param name="source">The source.</param> /// <param name="instance">The instance.</param> /// <returns></returns> internal static object GetConvertedValue(this DbPropertyInfoCache source, object instance) { var converterAttributeModel = source.Attributes.FirstOrDefault(s => s.Attribute is ValueConverterAttribute); if (converterAttributeModel != null) { var converterAtt = converterAttributeModel.Attribute as ValueConverterAttribute; var valueConverter = converterAtt.CreateConverter(); return(valueConverter.ConvertBack(source.Getter.Invoke(instance), null, converterAtt.Parameter, CultureInfo.CurrentCulture)); } return(source.Getter.Invoke(instance)); }
public List <DbPropertyInfoCache> ColumninfosToInfoCache(IEnumerable <IColumInfoModel> columnInfos) { var dic = new List <DbPropertyInfoCache>(); foreach (var item in columnInfos) { var dbInfoCache = new DbPropertyInfoCache(); dic.Add(dbInfoCache); dbInfoCache.Init(null, true); dbInfoCache.PropertyName = item.GetPropertyName(); dbInfoCache.PropertyType = item.ColumnInfo.TargetType; if (item.ColumnInfo.Nullable && !item.ColumnInfo.TargetType.IsClass) { dbInfoCache.PropertyType = typeof(Nullable <>).MakeGenericType(item.ColumnInfo.TargetType); } if (item.NewColumnName != item.ColumnInfo.ColumnName) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new ForModelAttribute(item.ColumnInfo.ColumnName))); } if (item.IsRowVersion) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new RowVersionAttribute())); } if (item.InsertIgnore) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new InsertIgnoreAttribute())); } if (item.PrimaryKey) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new PrimaryKeyAttribute())); } if (item.ForgeinKeyDeclarations != null) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new ForeignKeyDeclarationAttribute(item.ForgeinKeyDeclarations.TargetColumn, item.ForgeinKeyDeclarations.TableName))); } if (item.EnumDeclaration != null) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new ValueConverterAttribute(typeof(EnumMemberConverter)))); } dbInfoCache.Refresh(); } return(dic); }
/// <summary> /// Adds a Fake property to the class getter and setter will be invoked like normal ones /// </summary> /// <typeparam name="TE"></typeparam> public void CreateProperty <TE>(string name, Action <T, TE> setter = null, Func <T, TE> getter = null, params AttributeInfoCache[] attributes) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (ClassInfoCache.Propertys.Any(s => s.Key == name)) { throw new ArgumentOutOfRangeException(nameof(name), "Property name does exist. Cannot define a property twice"); } if (setter == null && getter == null) { throw new ArgumentNullException(nameof(setter), "Propertys must define at least one accessor. You cannot define a property without getter and setter"); } var propInfo = new DbPropertyInfoCache <T, TE>(name, setter, getter, attributes); ClassInfoCache.Propertys.Add(name, propInfo); }
/// <summary> /// Initializes a new instance of the <see cref="LocalDbUniqueConstraint{TEntity,TKey}" /> class. /// </summary> /// <param name="name">The name.</param> /// <param name="column">The index Column.</param> /// <param name="elementComparer">The element comparer.</param> /// <exception cref="ArgumentNullException"> /// name /// or /// getKey /// </exception> public DbIndex( string name, DbPropertyInfoCache column, IEqualityComparer <TKey> elementComparer = null) { _lockRoot = new object(); if (name == null) { throw new ArgumentNullException("name"); } Name = name; _indexer = column; if (elementComparer != null) { _index = new ConcurrentDictionary <TKey, TEntity>(elementComparer); } else { _index = new ConcurrentDictionary <TKey, TEntity>(); } }
/// <summary> /// Initializes a new instance of the <see cref="LocalDbDefaultConstraintEx{TEntity, TValue}" /> class. /// </summary> /// <param name="config">The configuration.</param> /// <param name="name">The name.</param> /// <param name="generateValue">The generate value.</param> /// <param name="column">The column.</param> /// <exception cref="ArgumentException"> /// </exception> /// <exception cref="InvalidCastException"> /// The given property name is invalid. When using Nullable types do not use the /// Value property. Use the Nullable propertie /// </exception> public LocalDbDefaultConstraintEx(DbConfig config, string name, Func <TValue> generateValue, Expression <Func <TEntity, TValue> > column) { _config = config; _generateValue = generateValue; _exp = column; Name = name; var member = _exp.Body as MemberExpression; if (member == null) { throw new ArgumentException(string.Format( "Expression '{0}' refers to a method, not a property.", _exp)); } var propInfo = member.Member as PropertyInfo; if (propInfo == null) { throw new ArgumentException(string.Format( "Expression '{0}' refers to a field, not a property.", _exp)); } var type = _config.GetOrCreateClassInfoCache(typeof(TEntity)); var fod = type.Propertys.FirstOrDefault(f => f.Key == propInfo.Name); if (fod.Value == null) { throw new InvalidCastException( "The given property name is invalid. When using Nullable types do not use the Value property. Use the Nullable propertie"); } _dbPropertyInfoCache = fod.Value; }
public static ClassType FromProperty(DbPropertyInfoCache dbPropertyInfoCach) { return(FromCsType(dbPropertyInfoCach.PropertyType)); }
public static object ReflectionPropertySet( DbConfig config, object instance, DbClassInfoCache info, IDataRecord reader, Dictionary <int, DbPropertyInfoCache> cache, DbAccessType?dbAccessType) { if (instance == null) { throw new ArgumentNullException("instance"); } if (info == null) { throw new ArgumentNullException("info"); } if (reader == null) { return(instance); } //Left c# property name and right the object to read from the reader //var listofpropertys = new Dictionary<string, object>(); var propertys = info.Propertys.ToArray(); var instanceOfFallbackList = new Dictionary <string, object>(); if (cache == null) { cache = new Dictionary <int, DbPropertyInfoCache>(); for (var i = 0; i < reader.FieldCount; i++) { DbPropertyInfoCache val = null; info.Propertys.TryGetValue(info.SchemaMappingDatabaseToLocal(reader.GetName(i)), out val); cache.Add(i, val); } } for (var i = 0; i < reader.FieldCount; i++) { var property = cache[i]; var value = reader.GetValue(i); if (property != null) { var attributes = property.Attributes; var valueConverterAttributeModel = attributes.FirstOrDefault(s => s.Attribute is ValueConverterAttribute); //Should the SQL value be converted if (valueConverterAttributeModel != null) { var converter = valueConverterAttributeModel.Attribute as ValueConverterAttribute; //Create the converter and then convert the value before everything else var valueConverter = converter.CreateConverter(); value = valueConverter.Convert(value, property.PropertyInfo.PropertyType, converter.Parameter, CultureInfo.CurrentCulture); } var xmlAttributeModel = attributes.FirstOrDefault(s => s.Attribute is FromXmlAttribute); //should the Content be considerd as XML text? if (xmlAttributeModel != null) { //Get the XML text and check if its null or empty var xmlStream = value.ToString(); if (string.IsNullOrEmpty(xmlStream)) { continue; } //Check for List //if this is a list we are expecting other entrys inside if (property.CheckForListInterface()) { //target Property is of type list //so expect a xml valid list Take the first element and expect the propertys inside this first element var record = XmlDataRecord.TryParse(xmlStream, property.PropertyInfo.PropertyType.GetGenericArguments().FirstOrDefault(), false, config); var xmlDataRecords = record.CreateListOfItems(); var genericArguments = config.GetOrCreateClassInfoCache( property.PropertyInfo.PropertyType.GetGenericArguments().FirstOrDefault()); var enumerableOfItems = xmlDataRecords.Select( s => genericArguments.SetPropertysViaReflection(s, dbAccessType, config)).ToList(); object castedList; if (genericArguments.Type.IsClass && genericArguments.Type.GetInterface("INotifyPropertyChanged") != null) { var caster = typeof(DbCollection <>).MakeGenericType(genericArguments.Type) .GetConstructor(new[] { typeof(IEnumerable) }); castedList = caster.Invoke(new object[] { enumerableOfItems }); } else { var caster = typeof(NonObservableDbCollection <>).MakeGenericType(genericArguments.Type) .GetConstructor(new[] { typeof(IEnumerable) }); castedList = caster.Invoke(new object[] { enumerableOfItems }); } property.Setter.Invoke(instance, castedList); } else { var classInfo = config.GetOrCreateClassInfoCache(property .PropertyInfo .PropertyType); var xmlDataRecord = XmlDataRecord.TryParse(xmlStream, property.PropertyInfo.PropertyType, true, config); //the t var xmlSerilizedProperty = classInfo.SetPropertysViaReflection(xmlDataRecord, dbAccessType, config); property.Setter.Invoke(instance, xmlSerilizedProperty); } } else if (value is DBNull || value == null) { property.Setter.Invoke(instance, new object[] { null }); } else { object changedType; if (value.GetType() != property.PropertyInfo.PropertyType) { changedType = DataConverterExtensions.ChangeType(value, property.PropertyInfo.PropertyType); } else { changedType = value; } property.Setter.Invoke(instance, changedType); } } //This variable is null if we tried to find a property with the LoadNotImplimentedDynamicAttribute but did not found it else if (instanceOfFallbackList != null) { //no property found Look for LoadNotImplimentedDynamicAttribute property to include it if (instanceOfFallbackList.Any()) { instanceOfFallbackList.Add(reader.GetName(i), value); } else { var maybeFallbackProperty = propertys.FirstOrDefault( s => s.Value.Attributes.Any(e => e.Attribute is LoadNotImplimentedDynamicAttribute)); if (maybeFallbackProperty.Value != null) { instanceOfFallbackList = (Dictionary <string, object>)maybeFallbackProperty.Value.Getter.Invoke(instance); if (instanceOfFallbackList == null) { instanceOfFallbackList = new Dictionary <string, object>(); maybeFallbackProperty.Value.Setter.Invoke(instance, instanceOfFallbackList); } instanceOfFallbackList.Add(reader.GetName(i), value); } else { instanceOfFallbackList = null; } } } } //foreach (var item in listofpropertys) //{ // var property = propertys.FirstOrDefault(s => s.PropertyName == item.Key); //} return(instance); }
public List<DbPropertyInfoCache> ColumninfosToInfoCache(IEnumerable<IColumInfoModel> columnInfos) { var dic = new List<DbPropertyInfoCache>(); foreach (var item in columnInfos) { var dbInfoCache = new DbPropertyInfoCache(); dic.Add(dbInfoCache); dbInfoCache.PropertyName = item.GetPropertyName(); dbInfoCache.PropertyType = item.ColumnInfo.TargetType; if (item.NewColumnName != item.ColumnInfo.ColumnName) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new ForModelAttribute(item.ColumnInfo.ColumnName))); } if (item.IsRowVersion) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new RowVersionAttribute())); } if (item.InsertIgnore) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new InsertIgnoreAttribute())); } if (item.PrimaryKey) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new PrimaryKeyAttribute())); } if (item.ForgeinKeyDeclarations != null) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new ForeignKeyDeclarationAttribute(item.ForgeinKeyDeclarations.TargetColumn, item.ForgeinKeyDeclarations.TableName))); } if (item.EnumDeclaration != null) { dbInfoCache.Attributes.Add(new DbAttributeInfoCache(new ValueConverterAttribute(typeof(EnumMemberConverter)))); } dbInfoCache.Refresh(); } return dic; }