Ejemplo n.º 1
0
        /// <summary>
        /// Instantiates the object and sets properties based on the field name. Only returns the first row.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="cmd"></param>
        /// <param name="load">A method that will create an object and fill it. If null, the object will be instantiated based on its type using the ClassFactory (must have a default ctor). If this returns null, it will not be added to the results.</param>
        /// <returns></returns>
        public static IEnumerable <T> GetObjects <T>(this BaDatabase db, DbCommand cmd, Func <DbDataReader, T> load = null) where T : class
        {
            var results = new List <T>();

            // If load doesn't have a value, use the default loader.
            if (load == null)
            {
                var props = TypeDescriptor.GetProperties(typeof(T));
                load = (row) =>
                {
                    var obj = ClassFactory.CreateObject <T>();
                    FillObject(row, obj, props);
                    return(obj);
                };
            }

            db.ExecuteReader(cmd, (row) =>
            {
                var result = load(row);
                if (result != null)
                {
                    results.Add(result);
                }

                return(true);
            });

            return(results);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Instantiates the object and sets properties based on the field name. Only returns the first row.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="cmd"></param>
        /// <param name="load">A method that will create an object and fill it. If null, the object will be instantiated based on its type using the ClassFactory (must have a default ctor). If this returns null, it will not be added to the results.</param>
        /// <returns></returns>
        public async static Task <IEnumerable <T> > GetObjectsAsync <T>(this BaDatabase db, DbCommand cmd, Func <DbDataReader, Task <T> > load = null) where T : class
        {
            var results = new List <T>();

            // If load doesn't have a value, use the default loader.
            if (load == null)
            {
                var props = TypeDescriptor.GetProperties(typeof(T));
                load = async(row) =>
                {
                    var obj = ClassFactory.CreateObject <T>();
                    FillObject(row, obj, props);
                    return(await Task.FromResult(obj).ConfigureAwait(false));
                };
            }

            await db.ExecuteReaderAsync(cmd, async (row) =>
            {
                var result = await load(row).ConfigureAwait(false);
                if (result != null)
                {
                    results.Add(result);
                }

                return(true);
            }).ConfigureAwait(false);

            return(results);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Instantiates the object and sets properties based on the field name. Only returns the first row.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="cmd"></param>
        /// <param name="load">A method that will create an object and fill it. If null, the object will be instantiated based on its type using the ClassFactory (must have a default ctor).</param>
        /// <returns></returns>
        public async static Task <T> GetObjectAsync <T>(this BaDatabase db, DbCommand cmd, Func <IDataReader, Task <T> > load = null) where T : class
        {
            T obj = null;

            await db.ExecuteReaderAsync(cmd, async (row) =>
            {
                if (load != null)
                {
                    obj = await load(row).ConfigureAwait(false);
                    return(false);
                }

                // Load doesn't have a value, so use the default loader.
                obj       = ClassFactory.CreateObject <T>();
                var props = TypeDescriptor.GetProperties(typeof(T));
                FillObject(row, obj, props);

                return(false);
            }).ConfigureAwait(false);

            return(obj);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Instantiates the object and sets properties based on the field name. Only returns the first row.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="cmd"></param>
        /// <param name="load">A method that will create an object and fill it. If null, the object will be instantiated based on its type using the ClassFactory (must have a default ctor).</param>
        /// <returns></returns>
        public static T GetObject <T>(this BaDatabase db, DbCommand cmd, Func <IDataReader, T> load = null) where T : class
        {
            T obj = null;

            db.ExecuteReader(cmd, (row) =>
            {
                if (load != null)
                {
                    obj = load(row);
                    return(false);
                }

                // Load doesn't have a value, so use the default loader.
                obj       = ClassFactory.CreateObject <T>();
                var props = TypeDescriptor.GetProperties(typeof(T));
                FillObject(row, obj, props);

                return(false);
            });

            return(obj);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Creates a new instance of the type.
 /// </summary>
 /// <param name="type"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public static object Instantiate(this Type type, params object[] args)
 {
     return(ClassFactory.CreateObject(type, args));
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Displays the help for the command-line object.
        /// </summary>
        /// <param name="results"></param>
        public static void WriteHelp(CmdLineParseResults results)
        {
            var generator = ClassFactory.CreateObject <HelpGenerator>(results);

            generator.WriteHelp(results);
        }