using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Collections.Specialized;
using System.Collections;
namespace Pariveda.DataAccess35.Business
{
///
/// Service Layer for Managing Customers
///
public class CustomerService
{
///
/// Gets the lookup list for customers
///
///
public List> GetLookupList()
{
using (AdventureWorksDataContext db = new AdventureWorksDataContext())
{
var customers = from c in db.Customers
orderby c.CompanyName
select new KeyValuePair(c.CustomerID, c.CompanyName);
return customers.ToList();
}
}
///
/// Gets the specified customer
///
/// The id of the customer to get
/// if set to true also include the child addresses.
///
public Customer Get(int customerId, bool getAddresses)
{
using (AdventureWorksDataContext db = new AdventureWorksDataContext())
{
GetFullCustomerGraph(db);
return db.Customers.SingleOrDefault(c => c.CustomerID == customerId);
}
}
///
/// Searches for the customer by company name
///
/// Name of the company.
///
public List SearchByCompanyName(string companyName)
{
using (AdventureWorksDataContext db = new AdventureWorksDataContext())
{
GetFullCustomerGraph(db);
var customers = from c in db.Customers
where c.CompanyName.StartsWith(companyName)
select c;
return customers.ToList();
}
}
///
/// Updates the specified customer.
///
/// The customer to update.
public Customer Update(Customer customer)
{
using (AdventureWorksDataContext db = new AdventureWorksDataContext())
{
if (customer.CustomerID > 0)
{
db.Customers.Attach(customer, true);
var deletedAddresses = from ca in db.CustomerAddresses
where !(from a in customer.CustomerAddresses select a.AddressID).Contains(ca.AddressID)
&& ca.CustomerID == customer.CustomerID
select ca;
db.CustomerAddresses.DeleteAllOnSubmit(deletedAddresses);
db.CustomerAddresses.InsertAllOnSubmit(
from a in customer.CustomerAddresses
where a.AddressID == 0
select a);
foreach (CustomerAddress address in
from a in customer.CustomerAddresses
where a.AddressID > 0
select a)
{
db.CustomerAddresses.Attach(address, true);
}
}
else
{
db.Customers.InsertOnSubmit(customer);
//db.CustomerAddresses.InsertAllOnSubmit(customer.CustomerAddresses);
}
db.SubmitChanges();
return customer;
}
}
///
/// Deletes the customer specified by customer id.
///
/// The customer id.
public void Delete(int customerId)
{
using (AdventureWorksDataContext db = new AdventureWorksDataContext())
{
Customer customer = db.Customers.SingleOrDefault(c => c.CustomerID == customerId);
if (customer != null)
{
db.CustomerAddresses.DeleteAllOnSubmit(customer.CustomerAddresses);
db.Customers.DeleteOnSubmit(customer);
db.SubmitChanges();
}
}
}
///
/// Deletes the customers specified by the customer ids.
///
/// The customer ids.
public void Delete(IEnumerable customerIds)
{
foreach (int customerId in customerIds)
{
Delete(customerId);
}
}
///
/// Sets the load options to enable full object graph gets.
///
/// The db to apply the options t
void GetFullCustomerGraph(AdventureWorksDataContext db)
{
DataLoadOptions options = new DataLoadOptions();
options.LoadWith(c => c.CustomerAddresses);
db.LoadOptions = options;
}
}
}