Example #1
0
        public ObjectPool(CreateObjectMethod createObjectMethod, RecycleObjectMethod recycleObjectMethod, int capacity)
        {
            this.createObjectMethod  = createObjectMethod;
            this.recycleObjectMethod = recycleObjectMethod;

            this.capacity     = capacity;
            this.freedObjects = new Stack <T>(capacity);
        }
Example #2
0
        /// <summary>
        /// Attempts to retrieve a database record and return a Business Object populated with
        /// values from that record.
        /// </summary>
        /// <typeparam name="ObjectType">The Type of the Business Object that we're working with,
        /// e.g. BroadcastEmail, Format, Audience, etc.</typeparam>
        /// <param name="command">A SqlCommand to execute to retrieve a database record.</param>
        /// <param name="createObjectMethod">A delegate method used to create a Business Object
        /// and to copy values from the retrieved database record into it.</param>
        /// <returns>If a database record is not found by executing <paramref name="command"/>,
        /// null is returned.  If a database record does exist, an instantied object with
        /// values set from the retrieved database record is returned.</returns>
        public static ObjectType GetSingleObjectFromDatabase <ObjectType>(SqlCommand command, CreateObjectMethod <ObjectType> createObjectMethod)
        {
            // BenC (2011-01-07): Set "obj" to null by calling default() with its type.
            // For reference types this'll be null, for value types it'll be zero.
            // So either null or an instantiated object with values from the database
            // will be what's returned by this our method.
            ObjectType obj = default(ObjectType);

            command.Connection.Open();

            using (SqlDataReader reader = command.ExecuteReader())
            {
                if (reader.Read())
                {
                    obj = createObjectMethod(reader);
                }
            }


            command.Connection.Close();

            return(obj);
        }
Example #3
0
        /// <summary>
        /// Attempts to retrieve a database record and return a Business Object populated with
        /// values from that record.
        /// </summary>
        /// <typeparam name="T">The Type of the Business Object that we're working with, e.g. BroadcastEmail, Format, Audience, etc.</typeparam>
        /// <param name="command">A SqlCommand to execute to retrieve a database record.</param>
        /// <param name="createObjectMethod">A delegate method used to create a Business Object
        /// and to copy values from the retrieved database record into it.</param>
        /// <returns>If a database record is not found by executing <paramref name="command"/>,
        /// null is returned.  If a database record does exist, an instantied object with
        /// values set from the retrieved database record is returned.</returns>
        public static async Task <T> GetSingleObjectFromDatabaseAsync <T>(SqlCommand command, CreateObjectMethod <T> createObjectMethod)
        {
            var obj = default(T);

            await command.Connection.OpenAsync();

            using (var reader = await command.ExecuteReaderAsync())
            {
                if (await reader.ReadAsync())
                {
                    obj = createObjectMethod(reader);
                }
            }


            command.Connection.Close();

            return(obj);
        }
Example #4
0
        public static List <ObjectType> GetCollectionFromDatabase <ObjectType>(SqlCommand command, CreateObjectMethod <ObjectType> createObjectMethod)
        {
            List <ObjectType> collection = new List <ObjectType>();

            command.Connection.Open();

            using (SqlDataReader reader = command.ExecuteReader())
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        collection.Add(createObjectMethod(reader));
                    }
                }
            }

            command.Connection.Close();

            return(collection);
        }