Create a Service

Now that we have our queries (maybe utilizing fragments) and our model, we can wire up the code that will utilize our IDatabaseService instance to actually make the calls to the data store. Here is an service that facilitates the CRUD (plus a method to get a list of all of them).

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

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

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="data">
        /// The data service instance to use for this service instance
        /// </param>
        public ExampleService(IDatabaseService 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 Example GetExample(int exampleID)
        {
            using (var reader = Data.SelectOne("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 void InsertExample(Example example)
        {
            Data.Insert("example.insert", example);
        }

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

        /// <summary>
        /// Delete an example
        /// </summary>
        /// <param name="exampleID">
        /// The ID of the example to delete
        /// </param>
        public void DeleteExample(int exampleID)
        {
            Data.Delete("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 IList<Example> GetAllExamples()
        {
            var examples = new List<Example>();

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

            return examples;
        }
    }
}
TIP: Notice the using around the data reader access. This feature will automatically call the Dispose method on the object when it goes out of scope and is no longer needed. This should be done with IDatabaseService instances as well; however, the creation of the data connection is not done by this class.

TIP: The SingleParameter() method defined in the DbUtils class allows you to do a simple query with one parameter, without having to write the code to create a dictionary and add the single parameter to it.

TIP: Creating a base class to hold the IDatabaseService reference may be desirable.

<<

Last edited May 6, 2012 at 4:59 AM by danielsummers, version 7

Comments

No comments yet.