Пример #1
0
        public static T CreateInstance <T>(this Table table, InstanceCreationOptions creationOptions)
        {
            var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T));

            return(TEHelpers.ThisTypeHasADefaultConstructor <T>()
                       ? TEHelpers.CreateTheInstanceWithTheDefaultConstructor <T>(instanceTable, creationOptions)
                       : TEHelpers.CreateTheInstanceWithTheValuesFromTheTable <T>(instanceTable, creationOptions));
        }
Пример #2
0
 private static void VerifyAllColumn(Table table, InstanceCreationOptions creationOptions, IEnumerable <string> memberNames)
 {
     if (creationOptions?.VerifyAllColumnsBound == true)
     {
         var memberNameKeys = new HashSet <string>(memberNames);
         var allIds         = table.Rows.Select(r => r.Id()).ToList();
         var missing        = allIds.Where(m => !memberNameKeys.Contains(m)).ToList();
         if (missing.Any())
         {
             throw new ColumnCouldNotBeBoundException(missing);
         }
     }
 }
Пример #3
0
        public static IEnumerable <T> CreateSet <T>(this Table table, InstanceCreationOptions creationOptions)
        {
            var list = new List <T>();

            var pivotTable = new PivotTable(table);

            for (var index = 0; index < table.Rows.Count(); index++)
            {
                var instance = pivotTable.GetInstanceTable(index).CreateInstance <T>(creationOptions);
                list.Add(instance);
            }

            return(list);
        }
Пример #4
0
        public static T CreateInstance <T>(this Table table, Func <T> methodToCreateTheInstance, InstanceCreationOptions creationOptions)
        {
            var instance = methodToCreateTheInstance();

            table.FillInstance(instance, creationOptions);
            return(instance);
        }
Пример #5
0
        internal static T CreateTheInstanceWithTheValuesFromTheTable <T>(Table table, InstanceCreationOptions creationOptions)
        {
            var constructor = GetConstructorMatchingToColumnNames <T>(table);

            if (constructor == null)
            {
                throw new MissingMethodException($"Unable to find a suitable constructor to create instance of {typeof(T).Name}");
            }

            var membersThatNeedToBeSet = GetMembersThatNeedToBeSet(table, typeof(T));

            var constructorParameters = constructor.GetParameters();
            var parameterValues       = new object[constructorParameters.Length];

            var members = new List <string>(constructorParameters.Length);

            for (var parameterIndex = 0; parameterIndex < constructorParameters.Length; parameterIndex++)
            {
                var parameter     = constructorParameters[parameterIndex];
                var parameterName = parameter.Name;
                var member        = (from m in membersThatNeedToBeSet
                                     where string.Equals(m.MemberName, parameterName, StringComparison.OrdinalIgnoreCase)
                                     select m).FirstOrDefault();
                if (member != null)
                {
                    members.Add(member.MemberName);
                    parameterValues[parameterIndex] = member.GetValue();
                }
                else if (parameter.HasDefaultValue)
                {
                    parameterValues[parameterIndex] = parameter.DefaultValue;
                }
            }

            VerifyAllColumn(table, creationOptions, members);
            return((T)constructor.Invoke(parameterValues));
        }
Пример #6
0
        internal static T CreateTheInstanceWithTheDefaultConstructor <T>(Table table, InstanceCreationOptions creationOptions)
        {
            var instance = (T)Activator.CreateInstance(typeof(T));

            LoadInstanceWithKeyValuePairs(table, instance, creationOptions);
            return(instance);
        }
Пример #7
0
        internal static void LoadInstanceWithKeyValuePairs(Table table, object instance, InstanceCreationOptions creationOptions)
        {
            var membersThatNeedToBeSet = GetMembersThatNeedToBeSet(table, instance.GetType());
            var memberHandlers         = membersThatNeedToBeSet.ToList();
            var memberNames            = memberHandlers.Select(h => h.MemberName);

            VerifyAllColumn(table, creationOptions, memberNames);

            memberHandlers.ForEach(x => x.Setter(instance, x.GetValue()));
        }