Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C#

Test Driving NHibernate 3.0, LINQ and the Entity Framework CTP 5 with the Abstract Factory Design Pattern

Rate me:
Please Sign up or sign in to vote.
4.89/5 (18 votes)
2 Apr 2011CPOL12 min read 229.1K   1.7K   53  
Developing an N-Tier application with C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ORMDataModel;
using ORMDataServices.DataServices;
using ORMDataServices.DataFactories.EntityFramework;
using ORMUtilities;
using System.Linq.Dynamic;

namespace ORMDataServices.DataFactories.EntityFramework
{


    class EFCustomerService : EFDataService, ICustomerDataService
    {

        PerformanceLogging _performanceLogging;

        /// <summary>
        /// Constructor
        /// </summary>
        public EFCustomerService()
        {
            _performanceLogging = new PerformanceLogging();
        }

        /// <summary>
        /// Get Customers - Dynamic Linq
        /// </summary>
        /// <param name="customerTransaction"></param>
        /// <param name="totalCustomers"></param>
        /// <returns></returns>
        public List<Customer> GetCustomers(CustomerTransaction customerTransaction, out long totalCustomers)
        {
            _performanceLogging.StartLogging("EFCustomerService.GetCustomers");

            string customerCode = customerTransaction.Customer.CustomerCode;
            string companyName = customerTransaction.Customer.CompanyName;
            string contactName = customerTransaction.Customer.ContactName;
      
            int pageSize = customerTransaction.PageSize;
            int pageNumber = customerTransaction.CurrentPageNumber;

            var query = ORMDatabaseFactory.Customers.AsQueryable();

            if (customerCode != null && customerCode.Trim().Length > 0)
            {
                query = query.Where(p => p.CustomerCode.StartsWith(customerCode));
            }

            if (companyName != null && companyName.Trim().Length > 0)
            {
                query = query.Where(p => p.CompanyName.StartsWith(companyName));            
            }

            if (contactName != null && contactName.Trim().Length > 0)
            {
                query = query.Where(p => p.ContactName.StartsWith(contactName));
    
            }
     
            List<Customer> customerList = query.ToList();
            List<Customer> customers;

            if (customerTransaction.SortExpression != null && customerTransaction.SortExpression.Trim().Length > 0)
            {
                if (customerTransaction.SortDirection == "DESC")
                    customers = customerList.AsQueryable()
                        .OrderBy(customerTransaction.SortExpression + " DESC")
                        .Skip((pageNumber - 1) * pageSize)
                        .Take(pageSize)                       
                        .ToList();
                else
                    customers = customerList.AsQueryable()
                        .OrderBy(customerTransaction.SortExpression)
                        .Skip((pageNumber - 1) * pageSize)
                        .Take(pageSize)                       
                        .ToList();
            }
            else
            {
                customers = customerList.AsQueryable()
                        .OrderBy(p => p.CustomerID)
                        .Skip((pageNumber - 1) * pageSize)
                        .Take(pageSize)                      
                        .ToList();                  
            }

            totalCustomers = customerList.Count;
                         
            _performanceLogging.EndLogging("EFCustomerService.GetCustomers");

            return customers;
        }

        /// <summary>
        /// Get Customer Information
        /// </summary>
        /// <param name="customerID"></param>
        /// <returns></returns>
        public Customer GetCustomerInformation(Int32? customerID)
        {
            _performanceLogging.StartLogging("EFCustomerService.GetCustomerInformation");
      
            Customer customer = ORMDatabaseFactory.Customers.SingleOrDefault(c => c.CustomerID == customerID);

            _performanceLogging.EndLogging("EFCustomerService.GetCustomerInformation");

            return customer;
        }

        /// <summary>
        /// Get Customer Information
        /// </summary>
        /// <param name="customerID"></param>
        /// <returns></returns>
        public Customer GetCustomerInformationWithLock(Int32? customerID)
        {

            _performanceLogging.StartLogging("EFCustomerService.GetCustomerInformationWithLock");

            Customer customer = ORMDatabaseFactory.Customers.SingleOrDefault(c => c.CustomerID == customerID);

            _performanceLogging.EndLogging("EFCustomerService.GetCustomerInformationWithLock");

            return customer;

        }

        /// <summary>
        /// Validate Customer Code - existing customer
        /// </summary>
        /// <param name="customerCode"></param>
        /// <param name="customerID"></param>
        /// <returns></returns>
        public List<Customer> ValidateCustomerCode(string customerCode, Int32? customerID)
        {

            _performanceLogging.StartLogging("EFCustomerService.ValidateCustomerCode1");
           
            Customer customer = ORMDatabaseFactory.Customers.SingleOrDefault(c => c.CustomerID != customerID && c.CustomerCode == customerCode);

            List<Customer> customerCount = new List<Customer>();
            if (customer != null) customerCount.Add(customer);
           
            _performanceLogging.EndLogging("EFCustomerService.ValidateCustomerCode1");

            return customerCount;

        }

        /// <summary>
        /// Validate Customer Code - new customer
        /// </summary>
        /// <param name="customerCode"></param>
        /// <returns></returns>
        public List<Customer> ValidateCustomerCode(string customerCode)
        {

            _performanceLogging.StartLogging("EFCustomerService.ValidateCustomerCode2");

            Customer customer = ORMDatabaseFactory.Customers.SingleOrDefault(c => c.CustomerCode == customerCode);

            List<Customer> customerCount = new List<Customer>();
            if (customer!=null) customerCount.Add(customer);

            _performanceLogging.EndLogging("EFCustomerService.ValidateCustomerCode2");

            return customerCount.ToList();

        }

        /// <summary>
        /// Insert Customer 
        /// </summary>
        /// <param name="customer"></param>
        public void InsertCustomer(Customer customer)
        {
            _performanceLogging.StartLogging("EFCustomerService.InsertCustomer");

            ORMDatabaseFactory.Customers.Add(customer);

            _performanceLogging.EndLogging("EFCustomerService.InsertCustomer");

        }

        /// <summary>
        /// Insert Transaction
        /// </summary>
        /// <param name="transactionLog"></param>
        public void InsertTransactionLog(TransactionLog transactionLog)
        {
            _performanceLogging.StartLogging("EFCustomerService.InsertTransactionLog");

            ORMDatabaseFactory.Transactions.Add(transactionLog);

            _performanceLogging.EndLogging("EFCustomerService.InsertTransactionLog");

        }

        /// <summary>
        /// Update Customer
        /// </summary>
        /// <param name="customer"></param>
        public void UpdateCustomer(Customer customer)
        {
            // not needed for EntityFramework
        }

    }

}

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 Joey Software Solutions
United States United States
Mark Caplin has specialized in Information Technology solutions for the past 30 years. Specializing in full life-cycle development projects for both enterprise-wide systems and Internet/Intranet based solutions.

For the past fifteen years, Mark has specialized in the Microsoft .NET framework using C# as his tool of choice. For the past four years Mark has been implementing Single Page Applications using the Angular platform.

When not coding, Mark enjoys playing tennis, listening to U2 music, watching Miami Dolphins football and watching movies in Blu-Ray technology.

In between all this, his wife of over 25 years, feeds him well with some great home cooked meals.

You can contact Mark at mark.caplin@gmail.com

...

Comments and Discussions