ЖАНРЫ

Шрифт:

IOrder GetOrderById(Guid orderId);

IEnumerable<IOrder> GetCustomerOrders(Guid customerId);

}

Хранилище для заказов позволит выбирать заказ по идентификатору и список заказов определенного заказчика.

public interface IProductRepository {

IProduct GetProductById(Guid productId);

IEnumerable<IProduct> GetAvailableProducts;

IEnumerable<IProduct> GetProductListByName(string name);

}

Хранилище для товаров

позволит найти товар по идентификатору, список товаров по наименованию и список товаров, которые доступны в данный момент.

Реализация данных хранилищ не составляет труда (листинг 3.3).

Листинг 3.3. Реализация хранилищ

public class CustomerRepository : ICustomerRepository {

private readonly MyDatabaseDataContext _dataBase;

public CustomerRepository(MyDatabaseDataContext db)

{

if (db == null)

throw new ArgumentNullException("db");

_dataBase = db;

}

public ICustomer GetCustomerById(Guid customerId)

{

if (customerId == Guid.Empty)

throw new ArgumentException("customerId");

return _dataBase.Customers

.SingleOrDefault(x => x.customerId == customerId);

}

public IEnumerable<ICustomer> GetCustomersByProduct(Guid productId) {

if (productId == Guid.Empty)

throw new ArgumentException("customerId");

return _dataBase.Orders

.Where(x => x.productId == productId)

.Select<Order, ICustomer>(x => x.Customer).Distinct;

}

}

public class OrderRepository : IOrderRepository {

private readonly MyDatabaseDataContext _dataBase;

public OrderRepository(MyDatabaseDataContext db)

{

if (db == null)

throw new ArgumentNullException("db");

_dataBase = db;

}

public IOrder GetOrderByld(Guid orderld)

{

if (orderId == Guid.Empty)

throw new ArgumentException("orderId");

return _dataBase.Orders

.SingleOrDefault(x => x.orderId == orderId);

}

public IEnumerable<IOrder> GetCustomerOrders(Guid customerId)

{

if (customerId == Guid.Empty)

throw new ArgumentException("customerId");

return _dataBase.Orders

.Where(x => x.customerId == customerId)

.Select<Order, IOrder>(x => x);

}

}

public class ProductRepository : IProductRepository {

private readonly MyDatabaseDataContext _dataBase;

public ProductRepository(MyDatabaseDataContext db)

{

if (db == null)

throw new ArgumentNullException("db");

_dataBase = db;

}

public IProduct GetProductById(Guid productId)

{

if (productId == Guid.Empty)

throw new ArgumentException("productId");

return _dataBase.Products

.SingleOrDefault(x => x.productId == productId);

}

public IEnumerable<IProduct> GetAvailableProducts

{

return _dataBase.Products.Where(x => x.isAvailable)

.Select<Product, IProduct>(x => x);

}

public IEnumerable<IProduct> GetProductListByName(string name)

{

if (string.IsNullOrEmpty(name))

throw new ArgumentException("name");

return _dataBase.Products

.Where(x => x.name.Contains(name))

.Select<Product, IProduct>(x => x);

}

}

Далее

создадим сервисы, которые будут помогать нам манипулировать данными. Сначала определим интерфейсы сервисов:

public interface ICustomerService {

ICustomer Create(string name, string phone, string address);

void Delete(ICustomer customer);

void Update(ICustomer customer, string name,

string phone, string address);

}

public interface IOrderService {

IOrder Create(ICustomer customer, IProduct product, int count,

DateTime orderDateTime);

void Delete(IOrder order);

void Update(IOrder order, ICustomer customer,

IProduct product, int count, DateTime orderDateTime);

}

public interface IProductService {

IProduct Create(string name, bool isAvailable, decimal cost);

void Delete(IProduct product);

void Update(IProduct product, string name,

bool isAvailable, decimal cost);

Реализуем интерфейсы сервисов, как показано в листинге 3.4, для класса customerService. Чтобы уменьшить количество кода, приводить реализацию для других классов мы не будем, для остальных классов определяем методы Create, Delete и Update точно таким же образом.

Листинг 3.4. Реализация интерфейсов сервисов

public class CustomerService : ICustomerService {

private readonly MyDatabaseDataContext _database;

public CustomerService(MyDatabaseDataContext db)

{

if (db == null)

throw new ArgumentNullException("db");

_database = db;

}

public ICustomer Create(string name, string phone, string address)

{

if (String.IsNullOrEmpty(name))

throw new ArgumentNullException("name");

Customer customer = new Customer

{

CustomerId = Guid.NewGuid,

Address = address,

Поделиться с друзьями: