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

Building an Agile SQL Data Access Layer

Rate me:
Please Sign up or sign in to vote.
4.80/5 (5 votes)
6 Jan 2009CPOL4 min read 51K   646   58  
An article on building a SQL Server data access layer in C# in a completely separate layer.
using System;
using System.Collections.Generic;
using System.Text;
using InMemoryDal.Entities;
using InMemoryDal.Interfaces;
using InMemoryDal.Helpers;

namespace InMemoryDal.Logic
{
    /// <summary>
    /// OrderItem BL
    /// </summary>
    /// <remarks>
    /// GB 02/11/2008
    /// </remarks>
    [InMemoryDALAttribute]
    public class OrderItemLogic
    {
        private ProductLogic productLogicCache;
        private OrderLogic orderLogicCache;
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();


        private OrderLogic orderLogic
        {
            get
            {
                if (orderLogicCache == null)
                    orderLogicCache = new OrderLogic();
                return orderLogicCache;
            }
        }
        private ProductLogic productLogic
        {
            get
            {
                if (productLogicCache == null)
                    productLogicCache = new ProductLogic();
                return productLogicCache;
            }
        }

        public OrderItemLogic()
        {
        }

        public OrderItem CreateOrderItem(Guid id, Guid idProduct, Guid idContainingOrder, Int32 quantity)
        {
            if (quantity <= 0)
            {
                throw new ArgumentException("Quantity should be a number greater then zero.");
            }
            Product product = productLogic.GetById(idProduct);
            if (product == null)
            {
                throw new ArgumentException("Product not found.");
            }
            Order containingOrder = orderLogic.GetById(idContainingOrder);
            if (product == null)
            {
                throw new ArgumentException("Order not found.");
            }
            OrderItem orderItem = new OrderItem();
            orderItem.Id = id;
            orderItem.Quantity = quantity;
            orderItem.ContainingOrder = containingOrder;
            orderItem.Product = product;
            return orderItem;
        }

        public OrderItem NewOrderItem(Guid idProduct, Guid idContainingOrder, Int32 quantity)
        {
            OrderItem orderItem = CreateOrderItem(Guid.NewGuid(), idProduct, idContainingOrder, quantity);
            return orderItem;
        }

        public OrderItem AddOrderItem(Guid idProduct, Guid idContainingOrder, Int32 quantity)
        {
            OrderItem entity = NewOrderItem(idProduct, idContainingOrder, quantity);

            Save(entity);
            logger.Trace(entity.ToString());
            entity = GetById(entity.Id);
            return entity;
        }

        public OrderItem UpdateOrderItem(Guid id, Guid idProduct, Guid idContainingOrder, Int32 quantity, byte[] versione)
        {

            OrderItem entity = CreateOrderItem(id, idProduct, idContainingOrder, quantity);

            Save(entity);
            logger.Trace(entity.ToString());
            return GetById(id);
        }

        public void DeleteOrderItem(Guid id, byte[] versione)
        {
            OrderItem orderItem = GetById(id);
            Delete(orderItem);
            logger.Trace(id.ToString());
        }

        public OrderItem GetById(Guid id)
        {
            List<OrderItem> lis;
            using (IOrdersSession sess = GBDipendencyInjection.GetSession())
            {
                lis = sess.DbOrderItems.Search(id,Guid.Empty,Guid.Empty,0, CreateOrderItem);
            }
            if (lis.Count == 1)
                return lis[0];
            else if (lis.Count == 0)
                return null;
            else
                throw new ApplicationException("Db read error");
        }

        public List<OrderItem> GetAll()
        {
            List<OrderItem> lis;
            using (IOrdersSession sess = GBDipendencyInjection.GetSession())
            {
                lis = sess.DbOrderItems.Search(Guid.Empty, Guid.Empty, Guid.Empty, 0, CreateOrderItem);
            }
            return lis;
        }
        public List<OrderItem> GetByOrderId(Guid orderId)
        {
            List<OrderItem> lis;
            using (IOrdersSession sess = GBDipendencyInjection.GetSession())
            {
                lis = sess.DbOrderItems.Search(Guid.Empty, Guid.Empty, orderId, 0, CreateOrderItem);
            }
            return lis;
        }

        private void Save(OrderItem orderItem)
        {
            using (IOrdersSession sess = GBDipendencyInjection.GetSession())
            {
                System.Data.IDbTransaction trans = sess.BeginTransaction();
                sess.Save(orderItem);
                trans.Commit();
            }
        }

        private void Delete(OrderItem orderItem)
        {
            using (IOrdersSession sess = GBDipendencyInjection.GetSession())
            {
                System.Data.IDbTransaction trans = sess.BeginTransaction();
                sess.Delete(orderItem);
                trans.Commit();
            }
        }
    }
}

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
Italy Italy
Software architect. At present working on C# development, with mainly Asp.net Ajax and MVC user inteface. Particularly interested in OOP, test driven, agile development.

Comments and Discussions