2

我正在构建.net 应用程序,使用 nhibernate 将数据映射到实体。我还是面向对象编程的新手,现在我有这个问题:

我有诸如客户、订单、产品等实体。我可以通过休眠从数据库中的数据中获取对象并列出客户等。但是如果我想列出客户的订单总额怎么办?这些不是客户实体的数据,也不是订单实体的数据。如何获取和列出这样的组合数据?他们应该有自己的数据传输对象还是这里有更好的方法来做到这一点?

4

1 回答 1

0

有一个额外的实体 ,CustomerList它将容纳Customer对象。

CustomerList有一个根据订单总数对列表进行排序的方法,这将通过访问每个成员的订单总数并进行比较来完成。

在 PHP 中,我会这样做(这只是一个示例,在实际代码中,您可能需要考虑其他事情)。

<?php

/**
 * Describe a single customer object
 */
class Customer {

    /** @var Order[] List of orders */
    private $orders = array();

    /**
     * Add an order to the order list.
     *
     * @param Order $order
     */
    public function addOrder(Order $order) {
        if (!in_array($order, $this->orders, true)) {
            $this->orders[] = $order;
        }
    }

    /**
     * Get an order by numeric index.
     *
     * @param int $index
     *
     * @return Order
     */
    public function getOrder($index) {
        return $this->orders[$index];
    }

    /**
     * Get total number of orders for customer.
     *
     * @return int
     */
    public function getOrdersTotal() {
        return count($this->orders);
    }
}

/**
 * Describe a single order.
 * I didn't include any information here because it's not relevant.
 */
class Order {

}

/**
 * Describe a list of customers.
 */
class CustomerList {

    /** @var Customer[] List of customers */
    private $customers;

    /**
     * Add a customer to the list
     *
     * @param Customer $customer
     */
    public function addCustomer(Customer $customer) {
        $this->customers[] = $customer;
    }

    /**
     * The sorting function.
     * Compare the orders total and return 1/0/-1 accordingly.
     *
     * @param Customer $a
     * @param Customer $b
     *
     * @return int
     */
    public function sort($a, $b) {
        if ($a->getOrdersTotal() === $b->getOrdersTotal()) {
            return 0;
        }
        return ($a->getOrdersTotal() > $b->getOrdersTotal()) ? 1 : -1;
    }

    /**
     * Call the sorting function on the customer list
     */
    public function sortCustomers() {
        usort($this->customers, array($this, "sort"));
    }

    /**
     * Return the full customer array.
     *
     * @return Customer[]
     */
    public function getCustomers() {
        return $this->customers;
    }
}

//Instantiation
$cList = new CustomerList();

$customer1 = new Customer();
$customer2 = new Customer();
$customer3 = new Customer();

$order1 = new Order();
$order2 = new Order();
$order3 = new Order();
$order4 = new Order();
$order5 = new Order();
$order6 = new Order();

$customer1->addOrder($order1);
$customer1->addOrder($order2);

$customer2->addOrder($order3);

$customer3->addOrder($order4);
$customer3->addOrder($order5);
$customer3->addOrder($order6);

$cList->addCustomer($customer1);
$cList->addCustomer($customer2);
$cList->addCustomer($customer3);

//List customers before sorting
var_dump($cList->getCustomers());

$cList->sortCustomers();

//List customers after sorting
var_dump($cList->getCustomers());
于 2012-08-05T18:08:36.597 回答