private SpmAccessors() { Type itemType = typeof(TItem); PropertyInfo[] properties = itemType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); List <ISpmAccessor <TItem> > list = new List <ISpmAccessor <TItem> >(properties.Length); for (int i = 0; i < properties.Length; i++) { PropertyInfo property = properties[i]; SpmAttribute mappedAttr = property.GetCustomAttribute <SpmAttribute>(); if (mappedAttr != null) { bool isNullable = false; Type valueType = property.PropertyType; if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Nullable <>)) { // nullable type valueType = Nullable.GetUnderlyingType(valueType); isNullable = true; } if (valueType.IsEnum) { valueType = Enum.GetUnderlyingType(valueType); } Delegate readDelegate = SpmReaderDelegates.Get(valueType); ISpmAccessor <TItem> accessor; if (readDelegate == null) { // user type // test for default constructor if (valueType.GetConstructor(Type.EmptyTypes) == null) { throw new ArgumentException(String.Concat("Type \"", valueType.Name, " of property \"", property.Name, "\" marked with SqlMapped must have default constructor")); } Type propertyAccessorType = typeof(SpmComplexAccessor <,>).MakeGenericType(itemType, valueType); accessor = (ISpmAccessor <TItem>) Activator.CreateInstance(propertyAccessorType, property, mappedAttr); } else { // standard type Type propertyAccessorType = isNullable ? typeof(SpmNullableAccessor <,>).MakeGenericType(itemType, valueType) : typeof(SpmAccessor <,>).MakeGenericType(itemType, valueType); accessor = (ISpmAccessor <TItem>) Activator.CreateInstance(propertyAccessorType, property, mappedAttr, readDelegate); } list.Add(accessor); } } list.TrimExcess(); this.collection = new ReadOnlyCollection <ISpmAccessor <TItem> >(list); }
public SpmComplexAccessor(PropertyInfo property, SpmAttribute mappedAttribute) : base(property, mappedAttribute) { valueType = typeof(TValue); this.propertyToken = property.MetadataToken; if (valueType.IsClass && mappedAttribute.ShareId == String.Empty) { mappedAttribute.ShareId = property.Name; } }
public SpmAccessor(PropertyInfo property, SpmAttribute mappedAttribute, Func <SqlDataReader, int, TValue> readValue) : base(property, mappedAttribute) { this.read = readValue; if (mappedAttribute.Columns.Length == 0) { mappedAttribute.ColumnNames = property.Name; } }
public SpmAccessorBase(PropertyInfo property, SpmAttribute mappedAttribute) { if (property == null) { throw new ArgumentNullException("property"); } if (mappedAttribute == null) { throw new ArgumentNullException("mappedAttribute"); } MethodInfo getMethod = property.GetGetMethod(true); MethodInfo setMethod = property.GetSetMethod(true); if (getMethod == null || setMethod == null) { throw new ArgumentException(String.Concat("Property \"", property.Name, "\" of type \"", property.ReflectedType.Name, "\" marked with SqlMapped must have both get and set accessor")); } this.getter = (Func <TItem, TValue>)getMethod.CreateDelegate(typeof(Func <TItem, TValue>)); this.setter = (Action <TItem, TValue>)setMethod.CreateDelegate(typeof(Action <TItem, TValue>)); this.mapped = mappedAttribute; }
public SpmItem(ISpmContext readContext, SpmAttribute mappedHolder, string group) { if (readContext == null) { throw new ArgumentNullException("itemReader"); } this.context = readContext; this.MappedHolder = mappedHolder; ReadOnlyCollection <ISpmAccessor <TItem> > coll = SpmAccessors <TItem> .Collection; this.accessors = new List <SprIndexedAccessor>(coll.Count); this.keys = new List <SprIndexedAccessor>(4); for (int i = 0; i < coll.Count; i++) { ISpmAccessor <TItem> accessor = coll[i]; if (accessor.Mapped.InGroup(group)) { string[] colNames = accessor.Mapped.Columns; int index = -1; for (int j = 0; j < colNames.Length; j++) { string colName = colNames[j]; if (colName.StartsWith("/")) { if (this.MappedHolder == null || !this.MappedHolder.HasSynonyms) { throw new ArgumentException(String.Concat("Не заданы родительские синонимы для", colNames[j])); } if (String.IsNullOrEmpty((colName = this.MappedHolder.GetSynonim(colName.Substring(1))))) { throw new ArgumentException(String.Concat("Не найдена ссылка на родительский синоним ", colNames[j])); } } if ((index = this.context.TryGetIndex(colName)) >= 0) { break; } } if (index < 0 && colNames.Length > 0) { throw new ArgumentException(String.Concat("В результате запроса не найдено хотя бы одно из полей: ", String.Join(", ", colNames))); } SprIndexedAccessor indexedAccessor = new SprIndexedAccessor(index, accessor); this.accessors.Add(indexedAccessor); if (accessor.Mapped.Key) { keys.Add(indexedAccessor); } } } this.accessors.TrimExcess(); if (mappedHolder != null && mappedHolder.Shared) { this.sharedItems = readContext.SharedPool.Get <TItem>(mappedHolder.ShareId, out this.extendable); if (this.sharedItems == null) { this.sharedItems = new Dictionary <TItem, TItem>(10); readContext.SharedPool.Set(mappedHolder.ShareId, this.sharedItems, true); this.extendable = true; } else if (this.keys.Count == 0 && !this.extendable) { throw new ArgumentException("Не заданы ключи для использования фиксированного пула элементов."); } this.sharedItem = new TItem(); } }
public SpmNullableAccessor(PropertyInfo property, SpmAttribute mappedAttribute, Func <SqlDataReader, int, TValue> readValue) : base(property, mappedAttribute) { this.read = readValue; }