- northwind.zip
- Castle.ActiveRecord.dll
- Castle.Core.dll
- Iesi.Collections.dll
- log4net.dll
- NHibernate.dll
- NMock2.dll
- Northwind.DataAccess.Test
- Northwind.DataAccess
- Northwind.Domain.Test
- Northwind.Domain
- Northwind.Presentation.Test
- Northwind.Presentation
- Northwind.Services.Test
- Northwind.Services
- NorthWind.sln
- Northwind.TransferObjects.Test
- Northwind.TransferObjects
- Northwind.UI.Controls
- Northwind.UI
- nunit.framework.dll
|
using NHibernate;
using Northwind.DataAccess;
using Northwind.DataAccess.Repository;
using Northwind.Domain;
using Northwind.Domain.Model;
using Northwind.TransferObjects.Model;
using Northwind.TransferObjects.Presentation;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Northwind.Services
{
/// <summary>
/// Implements a Service Pattern for customer management
/// </summary>
public class CustomerServices : ICustomerServices
{
IRepository<Customer, string> customerRepository;
ISessionFactory sessionFactory;
public CustomerServices()
{
DataAccessHelper.Initialize();
sessionFactory = DataAccessHelper.SessionFactory;
}
public CustomerServices(RepositoryMode repositoryMode) : this(repositoryMode, DataAccessHelper.SessionFactory){}
public CustomerServices(RepositoryMode repositoryMode, ISessionFactory sessionFactory)
{
DataAccessHelper.Initialize();
switch (repositoryMode)
{
case RepositoryMode.Database:
customerRepository = new CustomerRepository();
break;
case RepositoryMode.InMemory:
customerRepository = new InMemoryCustomerRepository();
break;
}
this.sessionFactory = sessionFactory;
}
public CustomerServices(IRepository<Customer,string> customerRepository, ISessionFactory sessionFactory)
{
DataAccessHelper.Initialize();
this.customerRepository = customerRepository;
this.sessionFactory = sessionFactory;
}
public CustomerTO GetDetailsForCustomer(string id)
{
Customer customer = customerRepository.Load(id);
return (CustomerTO)TOHelper<Customer>.GetTO(typeof(CustomerTO), customer);
}
public void UpdateCustomer(CustomerTO customerTO)
{
try
{
Customer customer = customerRepository.Load(customerTO.ID);
Order o = new Order();
o.CustomerID = customer.ID;
o.EmployeeID = 3;
o.ShipVia = 1;
customer.Orders.Add(o);
TOHelper<Customer>.UpdateDomainFromTO(customerTO, customer);
using (ISession session = sessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
customerRepository.AddOrUpdate(session, customer);
transaction.Commit();
}
}
}
catch
{
throw;
}
}
public CustomerTO NewCustomer(string customerId)
{
Customer c = new Customer();
c.ID = customerId;
//c.Address = "";
//c.City = "";
c.CompanyName = "[COMPANY NAME]";
//c.ContactName = "";
//c.ContactTitle = "";
//c.Country = "";
//c.Fax = "";
c.Phone = "[PHONE NUMBER]";
//c.PostalCode = "";
//c.Region = "";
using (ISession session = sessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
customerRepository.AddOrUpdate(session, c);
transaction.Commit();
}
}
return (CustomerTO)TOHelper<Customer>.GetTO(typeof(CustomerTO), c);
}
public void DeleteCustomer(string customerId)
{
Customer c = new Customer();
c.ID = customerId;
using (ISession session = sessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
customerRepository.Remove(session, c);
transaction.Commit();
}
}
}
public ILookupCollection GetCustomerList()
{
Customer c = new Customer();
Customer[] customers = customerRepository.FetchByExample(c);
List<object> toList = (List<object>)TOHelper<Customer>.GetTO(typeof(CustomerTO), customers);
return new LookupCollection(new CustomerToLookupConverter().ConvertAllFrom(toList));
}
public ILookupCollection GetCustomerList(string name)
{
Customer[] customers = ((CustomerRepository)customerRepository).FetchByName(name);
List<object> toList = (List<object>)TOHelper<Customer>.GetTO(typeof(CustomerTO), customers);
return new LookupCollection(new CustomerToLookupConverter().ConvertAllFrom(toList));
}
}
public enum RepositoryMode
{
Database,
InMemory
}
}
|
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.