Click here to Skip to main content
15,885,811 members
Articles / Programming Languages / C#

In memory data access, a methodology to simplify agile development

Rate me:
Please Sign up or sign in to vote.
4.75/5 (11 votes)
25 Nov 2008CPOL5 min read 44.4K   304   40  
An article on architecting application layers to separate concerns and, particularly, data access.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using InMemoryDal.Entities;
using InMemoryDal.Interfaces;
using InMemoryDal.Helpers;

namespace InMemoryDal.Logic
{
    /// <summary>
    /// Customer BL
    /// </summary>
    /// <remarks>
    /// GB 28/10/2008
    /// </remarks>
    public class CustomerLogic
    {
        string _connectionString;
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public CustomerLogic(string connectionString)
        {
            _connectionString = connectionString;
        }

        public CustomerLogic()
        {
            //  gets connectin string from configuration
        }

        public Customer CreateCustomer(Guid id, String firstname, String lastname, String address)
        {
            Customer customer = new Customer();
            customer.Id = id;
            if (firstname == string.Empty)
            {
                throw new ArgumentException("Firstname empty.");
            }
            if (lastname == string.Empty)
            {
                throw new ArgumentException("Lastname empty.");
            }
            customer.Firstname = firstname;
            customer.Lastname = lastname;
            customer.Address = address;
            return customer;
        }

        public Customer NewCustomer(String firstname, String lastname, String address)
        {
            Customer customer = CreateCustomer(Guid.NewGuid(), firstname, lastname, address);
            //customer.Versione = new byte[] { };
            return customer;
        }

        public Customer AddCustomer(String firstname, String lastname, String address)
        {
            Customer entity = NewCustomer(firstname, lastname, address);

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

        public Customer UpdateCustomer(Guid id, String firstname, String lastname, String address, byte[] versione)
        {
            Customer entitySulDb = GetById(id);

                Customer entity = CreateCustomer(id, firstname, lastname, address);

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

        public void DeleteCustomer(Guid id)
        {
            OrderLogic orderLogic = new OrderLogic();
            List<Order> orders = orderLogic.GetByCustomerId(id);
            if (orders.Count != 0)
            {
                throw new ArgumentException("Can't delete customer, it has orders.");
            }
            Customer ent = GetById(id);
            Delete(ent);
            logger.Trace(ent.ToString());
        }

        public Customer GetById(Guid id)
        {
            List<Customer> lis;
            using (IOrdersSession sess = GBDipendencyInjection.GetSession())
            {
                lis = sess.DbCustomers.Search(id, string.Empty, string.Empty, string.Empty, CreateCustomer);
            }
            if (lis.Count == 1)
                return lis[0];
            else if (lis.Count == 0)
                return null;
            else
                throw new ArgumentException("DB read error");
        }

        public List<Customer> GetAll()
        {
            List<Customer> lis;
            using (IOrdersSession sess = GBDipendencyInjection.GetSession())
            {
                lis = sess.DbCustomers.Search(Guid.Empty, string.Empty, string.Empty, string.Empty, CreateCustomer);
            }
            return lis;
        }

        private void Save(Customer customer)
        {
            using (IOrdersSession sess = GBDipendencyInjection.GetSession())
            {
                IDbTransaction trans = sess.BeginTransaction();
                sess.Save(customer);
                trans.Commit();
            }
        }

        private void Delete(Customer customer)
        {
            using (IOrdersSession sess = GBDipendencyInjection.GetSession())
            {
                IDbTransaction trans = sess.BeginTransaction();
                sess.Delete(customer);
                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