using System; using System.Collections.Generic; using System.Linq; namespace DatabaseExport { public class CustomerDetails { private readonly ISO.CustomerDetails _customerDetails; public string Name { get => _customerDetails.Name; set => _customerDetails.Name = value; } public CustomerDetails() { _customerDetails = new ISO.CustomerDetails(); _customerDetails.Name = "(none)"; } public CustomerDetails(ISO.CustomerDetails customerDetails) { _customerDetails = new ISO.CustomerDetails(customerDetails); } public ISO.CustomerDetails GetISOCustomer() { return _customerDetails; } public override string ToString() { return Name; } } public class CustomerDetailsList { private static readonly CustomerDetailsList _customerList = new CustomerDetailsList(); public static CustomerDetailsList CustomerList => _customerList; private void PopulateCustomers() { if (null != _customers) return; _customers = new Dictionary(); foreach (var c in _customerList.GetAllCustomers()) { if (!_customers.ContainsKey(c.Name)) { _customers.Add(c.Name, c); } } } private static readonly object _customerLock = new object(); private Dictionary _customers = null; public CustomerDetails[] Customers { get { lock (_customerLock) { PopulateCustomers(); } var customers = new List(_customers.Values); customers.Sort(CompareCustomers); return customers.ToArray(); } } private static int CompareCustomers(CustomerDetails a, CustomerDetails b) { if (a == b) { return 0; } if (null == a) { return -1; } return null == b ? 1 : String.Compare(a.Name, b.Name, StringComparison.Ordinal); } public CustomerDetails[] GetAllCustomers() { var list = new List(); list.Add(new CustomerDetails()); //This is the "(none)" entry list.AddRange(ISO.CustomerDetails.GetAllCustomerDetails().Select(cs => new CustomerDetails(cs))); return list.ToArray(); } public CustomerDetails GetCustomerDetail(string name) { var customers = from c in Customers.AsParallel() where c.Name == name select c; return customers.Any() ? customers.First() : null; } } }