/// <summary> /// Initializes static members of the Together class. /// </summary> /// <param name="parentIDName">The name of the field containing the ID in the parent class. Defaults to autodetect.</param> /// <param name="into">The name of the field list of the child objects in the parent class. Defaults to autodetect.</param> public Together(string parentIDName = null, string into = null) { _parentIDName = parentIDName; _into = into; _parentID = ChildMapperHelper.GetIDAccessor(typeof(T), parentIDName).CreateGetMethod <T, Object>(); _listSetter = ChildMapperHelper.GetListAccessor(typeof(T), typeof(TChild), into, setter: true).CreateSetMethod <T, List <TChild> >(); _listGetter = ChildMapperHelper.GetListAccessor(typeof(T), typeof(TChild), into, setter: false).CreateGetMethod <T, ICollection <TChild> >(); }
/// <summary> /// Initializes a new instance of the ChildMapper class. /// </summary> /// <param name="idSelector">The function that selects IDs from the parent object.</param> /// <param name="listSetter">The action that sets the children into the proper parent.</param> public ChildMapper( Func <TParent, TId> idSelector, Action <TParent, List <TChild> > listSetter) { if (idSelector == null) { _idType = ChildMapperHelper.GetIDAccessor(typeof(TParent)).MemberType; } _idSelector = idSelector ?? _defaultIDSelector.Value; _listSetter = listSetter ?? _defaultListSetter.Value; }
/// <summary> /// Creates a child record reader that autogroups the given type, based on the parent's ids. /// </summary> /// <typeparam name="TId">The type of the ID.</typeparam> /// <param name="parentType">The type of the parent.</param> /// <returns>A child record reader.</returns> private IChildRecordReader <T, TId> CreateAutoGroupBy <TId>(Type parentType) { // if we can detect the parent ids from the child class, then use that to do the mapping var idAccessor = ChildMapperHelper.FindParentIDAccessor(typeof(T), null, parentType); if (idAccessor != null) { var getid = idAccessor.CreateGetMethod <T, TId>(); return(new ChildRecordReader <T, TId, T>(this, records => records.GroupBy(getid, r => r))); } else { List <Type> guardianTypes = new List <Type>(); // if a selector was specified, use it, else use the parent's ID accessor to define external columns to use as the key if (typeof(TId) != typeof(object)) { if (typeof(TId).Name.StartsWith("Tuple`", StringComparison.OrdinalIgnoreCase)) { guardianTypes.AddRange(typeof(TId).GetGenericArguments()); } else { guardianTypes.Add(typeof(TId)); } } else { guardianTypes.AddRange(ChildMapperHelper.GetIDAccessor(parentType).MemberTypes); } guardianTypes.Insert(0, typeof(T)); var guardianType = GetGuardianType(guardianTypes.Count).MakeGenericType(guardianTypes.ToArray()); #if NET35 var getReader = this.GetType().GetMethod("GetAdaptedReader", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(guardianType, typeof(Guardian <T>)); #else var getReader = this.GetType().GetMethod("GetGuardianReader").MakeGenericMethod(guardianType); #endif var reader = (IRecordReader <Guardian <T> >)getReader.Invoke(this, Parameters.EmptyArray); return(new ChildRecordReader <Guardian <T>, TId, T>(reader, records => records.GroupBy(g => (TId)g.GetID(), g => g.Object))); } }
/// <summary> /// Gets the ID selector from the class, looking for ID, classID, and then anything with xxxID. /// </summary> /// <returns>An accessor for the ID field.</returns> private static Func <TParent, TId> GetIDSelector() { return(ChildMapperHelper.GetIDAccessor(typeof(TParent)).CreateGetMethod <TParent, TId>()); }
/// <summary> /// Gets the ID selector from the class, looking for ID, classID, and then anything with xxxID. /// </summary> /// <returns>An accessor for the ID field.</returns> private static Func <T, Object> GetIDSelector() { return(ChildMapperHelper.GetIDAccessor(typeof(T)).CreateGetMethod <T, Object>()); }
/// <summary> /// Used for internal testing. /// </summary> /// <param name="type">The parameter is not used.</param> /// <param name="name">The parameter is not used.</param> /// <returns>The parameter is not used.</returns> public static IList <string> GetIDAccessor(Type type, string name = null) { return(ChildMapperHelper.GetIDAccessor(type, name).MemberNames.ToList()); }