Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

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

, 2 Apr 2011 CPOL
Developing an N-Tier application with C#
CodeProjectORM.zip
ORMApplicationServices
bin
Debug
Release
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ORMApplicationServices.dll
ORMApplicationServices.pdb
ResolveAssemblyReference.cache
TempPE
Release
ORMApplicationServices.csproj.user
ORMApplicationServices.suo
Properties
ORMDatabase
ORMDatabase.mdf
ORMDatabase_log.ldf
ORMDataModel
bin
Debug
Release
ORMDataModel.dll
ORMDataModel.pdb
Models
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ORMDataModel.dll
ORMDataModel.pdb
ResolveAssemblyReference.cache
TempPE
Release
DesignTimeResolveAssemblyReferencesInput.cache
ORMDataModel.dll
ORMDataModel.pdb
TempPE
ORMDataModel.csproj.user
ORMDataModel.suo
Properties
ORMDataServices
bin
Debug
Release
DataFactories
EntityFramework
LinqToSQL
NHibernate
DataFactory
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ORMDataServices.dll
ORMDataServices.pdb
ResolveAssemblyReference.cache
TempPE
Release
ORMDataServices.csproj.user
ORMDataServices.suo
Properties
ORMNhiberateMaps
bin
Debug
ORMNhibernateMaps.dll
ORMNhibernateMaps.pdb
Release
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ORMNhibernateMaps.dll
ORMNhibernateMaps.pdb
TempPE
Release
ORMNhiberateMaps.suo
ORMNhibernateMaps.csproj.user
Properties
ORMUtilities
bin
Debug
Release
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ORMUtilities.dll
ORMUtilities.pdb
ResolveAssemblyReference.cache
TempPE
Release
ORMUtilities.csproj.user
ORMUtilities.suo
Properties
ORMWebApplicationMVC
ORMWebApplicationMVC.suo
ORMWebApplicationMVC
App_Data
bin
Antlr3.Runtime.dll
Castle.Core.dll
Iesi.Collections.dll
Iesi.Collections.pdb
NHibernate.ByteCode.Castle.dll
NHibernate.dll
ORMApplicationServices.dll
ORMApplicationServices.pdb
ORMDataModel.dll
ORMDataModel.pdb
ORMDataServices.dll
ORMDataServices.pdb
ORMNhibernateMaps.dll
ORMNhibernateMaps.pdb
ORMUtilities.dll
ORMUtilities.pdb
ORMWebApplicationMVC.dll
ORMWebApplicationMVC.pdb
Remotion.Data.Linq.dll
Content
DownArrow.gif
Images
DownArrow.gif
SampleApplication.jpg
UpArrow.gif
themes
base
images
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
UpArrow.gif
Controllers
Global.asax
Helpers
Models
obj
Debug
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
ORMWebApplicationMVC.dll
ORMWebApplicationMVC.pdb
ResolveAssemblyReference.cache
TempPE
ORMWebApplicationMVC.csproj.user
Properties
Scripts
Views
Account
Customer
Home
Orders
Products
Shared
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)

Share

About the Author

Mark J. Caplin
Software Developer Caplin Systems
United States United States
Mark Caplin has specialized in Information Technology solutions for 25 years. Specializing in full life-cycle development projects for both enterprise-wide systems and Internet/Intranet based solutions.
 
For the past ten years or so, Mark has specialized in the Microsoft .NET framework using both C# and VB.NET as his tools of choice.
 
Mark currently works for e-Builder Inc, www.e-builder.net. e-Builder is a SaaS software company specializing in Construction Program Management Software. If you are a talented Microsoft .NET developer and you are looking for a new opportunity with a rapidly growing company, please send me your resume.
 
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 20 years, feeds him well with some great home cooked meals.
 
...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 2 Apr 2011
Article Copyright 2011 by Mark J. Caplin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid