Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C# 4.0

INCLUDE in classic ADO.NET

Rate me:
Please Sign up or sign in to vote.
4.91/5 (4 votes)
5 Jul 2012CPOL3 min read 21.3K   641   12  
Implementing and using an INCLUDE method in classic ADO.NET.
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using System.Collections.Generic;

using EncodeFirst.Core;
using EncodeFirst.Core.Managers;
using EncodeFirst.Core.DataAccess;
using EncodeFirst.TestService.Core.Managers;
using EncodeFirst.TestService.BusinessObjects.Types;
using EncodeFirst.TestService.BusinessObjects.Contracts;
using EncodeFirst.TestService.BusinessObjects.Types.Filters;
using EncodeFirst.TestService.BusinessObjects.Types.DataModel;

namespace EncodeFirst.TestService.DataAccess
{
    public class CustomerRepository : RepositoryBase, ICustomerRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RepositoryBase"/> class.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <remarks></remarks>
        public CustomerRepository() : base()
        { 
        }

        /// <summary>
        /// Inits the repository.
        /// </summary>
        /// <remarks></remarks>
        protected override void InitRepository()
        {
            _sqlConnection = new SqlConnection(SettingsManager.Instance.TestServiceDBConnectionString);
        }

        /// <summary>
        /// Gets the customer.
        /// </summary>
        /// <param name="customerId">The customer id.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public Customer GetCustomer(long id)
        {
            return GetEntity<Customer>("Customers_Get", new Customer() { Id = id });
        }

        /// <summary>
        /// Gets the name of the customer by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public Customer GetCustomerByName(string name)
        {
            return ExecuteQueryMethod<Customer>(delegate()
            {   //Here: Do whatever is needed before call the Stored Procedure. 
                _parameters = new List<SqlParameter>();
                _parameters.Add(new SqlParameter("@Name", name));

                return SqlHelper.GetEntity<Customer>(_sqlConnection, "Customers_GetByName", _parameters);
            });
        }

        /// <summary>
        /// Gets the customers list.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public Customer[] GetCustomersList()
        {
            return GetEntitiesList<Customer>("Customers_GetList");
        }

        /// <summary>
        /// Gets the customers list by filter.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public Customer[] GetCustomersListByFilter(CustomerFilter filter, out int totalRows)
        {
            return GetEntitiesListByFilter<Customer>("Customers_GetByFilter", filter, out totalRows);
        }

        /// <summary>
        /// Inserts the specified customer.
        /// </summary>
        /// <param name="customer">The customer.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool Insert(Customer customer)
        {
            return InsertEntity("Customers_Insert", customer);
        }

        /// <summary>
        /// Updates the specified customer.
        /// </summary>
        /// <param name="customer">The customer.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool Update(Customer customer)
        {
            return UpdateEntity("Customers_Update", customer);
        }

        /// <summary>
        /// Deletes the specified customer id.
        /// </summary>
        /// <param name="customerId">The customer id.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool Delete(long id)
        {
            return DeleteEntity<Customer>("Customers_Delete", new Customer() { Id = id });
        }

        /// <summary>
        /// Deletes the specified customer ids.
        /// </summary>
        /// <param name="ids">The ids.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool Delete(long[] ids)
        {
            bool result = false;

            if (ids != null && ids.Length > 0)
            {
                List<SqlDataRecord> idsList = new List<SqlDataRecord>();
                SqlMetaData[] idsListDefinition = { new SqlMetaData("id", SqlDbType.BigInt) };

                foreach (long id in ids)
                {
                    SqlDataRecord value = new SqlDataRecord(idsListDefinition);
                    value.SetInt64(0, id);

                    idsList.Add(value);
                }

                result = DeleteEntitiesList("Customers_DeleteList", idsList);
            }

            return result;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions