/// <summary> /// Basic constructor. /// </summary> /// <param name="reader">Instance of an <see cref="T:System.Data.Common.DbDataReader"/> object.</param> public DataReaderService(DbDataReader reader) { if (reader == null) { throw new ArgumentNullException("reader", "Unable to create DataReaderService for an empty reader."); } this._reader = reader; this._hasRows = this._reader.HasRows; this._fieldCount = this._reader.FieldCount; this._isClosed = false; this._columns = null; this._metaInfo = null; this._result = new List <T>(); }
/// <summary> /// Gets meta info for the entity. /// </summary> /// <returns>Returns instance of an <see cref="xDev.Data.DataReaderService{T}"/>.</returns> public DataReaderService <T> GetMetaInfo() { if (this._metaInfo != null) { return(this); } CheckIsClosed(); // Get meta info for the entity type this._metaInfo = MetaInfo <T> .GetMetaInfo(); return(this); }
/// <summary> /// Gets the meta information for the entity. /// </summary> /// <typeparam name="T">Type of the entity.</typeparam> /// <param name="this">Instance of an entity.</param> /// <returns>Returns new instance of a <see cref="xDev.Data.MetaInfo{T}"/> class.</returns> public static MetaInfo <T> GetMetaInfo <T>(this IEntity <T> @this) where T : class, IEntity <T>, new() { var entityType = typeof(T); MetaInfo <T> info = null; lock (IEntityExtensions.SyncRoot) { // Get WeakReference for the enityt type meta info var infoRef = IEntityExtensions.MetaInfos.ContainsKey(entityType) ? IEntityExtensions.MetaInfos[entityType] : null; if ((infoRef != null) && infoRef.IsAlive) { info = infoRef.Target as MetaInfo <T>; } else { // If there is not meta info for the desired type clear all dead references var infoRefKeys = IEntityExtensions.MetaInfos .Where(miRef => (miRef.Value == null) || !miRef.Value.IsAlive) .Select(miRef => miRef.Key); // Remove all dead referencies foreach (var key in infoRefKeys) { IEntityExtensions.MetaInfos.Remove(key); } } // If there is not any meta info create new one if (info == null) { info = new MetaInfo <T>(); IEntityExtensions.MetaInfos[entityType] = new WeakReference(info); } } return(info); }
/// <summary> /// Gets list of <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> objects /// which represent validation state of the entity. Gets only the first invalid rule for each property. /// </summary> /// <typeparam name="T">Type of the entity.</typeparam> /// <param name="entity">Instance of an entity.</param> /// <param name="metaInfo">Entity meta info.</param> /// <returns>Returns list of <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> objects.</returns> private static IEnumerable <ValidationResult> ValidateInternal <T>(IEntity <T> entity, MetaInfo <T> metaInfo) where T : class, IEntity <T>, new() { // Validate all properties foreach (var propertyValidationRules in metaInfo.ValidationRules) { // Create ValidationContext var context = new ValidationContext(entity) { DisplayName = propertyValidationRules.Key, MemberName = propertyValidationRules.Key }; // Get property value which has to be validate var value = GetValueInternal(entity, propertyValidationRules.Key, metaInfo); // Find invalid rule for the current property foreach (var rule in propertyValidationRules.Value) { // Get the ValidationResult var result = rule.GetValidationResult(value, context); // If the validation result is not null than this property is invalid if (result != null) { yield return(result); } } } }
/// <summary> /// Gets value of the property. /// </summary> /// <typeparam name="T">Type of the entity.</typeparam> /// <param name="entity">Instance of an entity.</param> /// <param name="metaInfo">Entity meta info.</param> /// <returns>Returns list of <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> objects.</returns> private static object GetValueInternal <T>(IEntity <T> entity, string propertyName, MetaInfo <T> metaInfo) where T : class, IEntity <T>, new() { // Check if the property name is valid if (!metaInfo.Properties.Contains(propertyName)) { throw new ArgumentException(string.Format(@"Unable to dynamicly get value for the property {0}. Property does not exist on the object instance.", propertyName), "propertyName"); } // Check if there is getter for the property if (!metaInfo.Getters.ContainsKey(propertyName)) { throw new ArgumentException(string.Format(@"Unable to dynamicly get value for the property {0}. There is not any getter for the property.", propertyName), "propertyName"); } try { // Try to get property value return(metaInfo.Getters[propertyName].DynamicInvoke(entity)); } catch (Exception ex) { throw new InvalidOperationException(string.Format(@"Unable to dynamicly get value for the property {0}.", propertyName), ex); } }