Create an Asynchronous Service

We created a service, but it's synchronous. Aren't all the cool kids these days using async? Well, we want to be cool kids, too, so here is the same service as before, only this time utilizing IDatabaseServiceAsync instead of IDatabaseService.

namespace DocExample.Services
{
    using DatabaseAbstraction.Async.Interfaces;
    using DocExample.Models;
    using System.Data;
    using System.Threading.Tasks;

    /// <summary>
    /// This service manipulates examples
    /// </summary>
    public class ExampleService
    {
        /// <summary>
        /// The data service instance to use for this service instance
        /// </summary>
        private IDatabaseServiceAsync Data { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="data">
        /// The data service instance to use for this service instance
        /// </param>
        public ExampleService(IDatabaseServiceAsync data)
        {
            Data = data;
        }

        /// <summary>
        /// Get an example
        /// </summary>
        /// <param name="exampleID">
        /// The ID of the example to retrieve
        /// </param>
        /// <returns>
        /// The example (or null if not found)
        /// </returns>
        public async Task<Example> GetExample(int exampleID)
        {
            using (var reader = await Data.SelectOneAsync("example.select",
                    DbUtils.SingleParameter("example_id", exampleID)))
                return (reader.Read()) ? new Example(reader) : null;
        }

        /// <summary>
        /// Insert an example
        /// </summary>
        /// <param name="example">
        /// The example to insert
        /// </param>
        public async Task InsertExample(Example example)
        {
            await Data.InsertAsync("example.insert", example);
        }

        /// <summary>
        /// Update an example
        /// </summary>
        /// <param name="example">
        /// The example to update
        /// </param>
        public async Task UpdateExample(Example example)
        {
            await Data.UpdateAsync("example.update", example);
        }

        /// <summary>
        /// Delete an example
        /// </summary>
        /// <param name="exampleID">
        /// The ID of the example to delete
        /// </param>
        public async Task DeleteExample(int exampleID)
        {
            await Data.DeleteAsync("example.delete", DbUtils.SingleParameter(
                "example_id", exampleID));
        }

        /// <summary>
        /// Get a list of all Example objects
        /// </summary>
        /// <returns>
        /// A list of all Example objects
        /// </returns>
        public async Task<IList<Example>> GetAllExamples()
        {
            var examples = new List<Example>();

            using (var reader = await Data.SelectAsync("example.all"))
                while (reader.Read())
                    examples.Add(new Example(reader));

            return examples;
        }
    }
}
TIP: Asynchronous access does not relieve us of the need to use a using on the data reader, so it is disposed properly.

<<

Last edited Apr 2, 2013 at 2:35 AM by danielsummers, version 2

Comments

No comments yet.