php - 存储库 - 我应该如何实现“自定义”标准?

时间:2017-07-31 20:38:09

标签: php repository-pattern

我开始学习DDD等等。现在我来到了Repository,但我不太确定如何解决以下问题:
这是我的示例界面:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    int searchEditTextId = R.id.search_src_text;
    final AutoCompleteTextView searchEditText = (AutoCompleteTextView) searchView.findViewById(searchEditTextId);
    final View dropDownAnchor = searchView.findViewById(searchEditText.getDropDownAnchor());

    if (dropDownAnchor != null) {
        dropDownAnchor.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom,
                                   int oldLeft, int oldTop, int oldRight, int oldBottom) {

                // screen width
                int screenWidthPixel = ActivitySearchUni.this.getResources().getDisplayMetrics().widthPixels;
                searchEditText.setDropDownWidth(screenWidthPixel);
            }
        });
    }

    return true;
}

现在我需要检索模型的单个标准的数据,例如。 interface RepositoryInterface { public function all($columns = array('*')); public function paginate($perPage = 15, $columns = array('*')); public function create(array $data); public function update(array $data, $id); public function delete($id); public function find($id, $columns = array('*')); public function findBy($field, $value, $columns = array('*')); } - 我该如何处理这个案子?将这样的方法添加到接口或可以参考上述任何方法并分别“扩展”?

1 个答案:

答案 0 :(得分:2)

对我来说,findActive()方法更具特定的表(模型),可以在额外的接口(例如CustomerRepositoryInterface)中定义。然后,您可以在特殊类(例如CustomerRepository)中“实现”这些不同的接口。

以下是一个例子:

<?php

interface RepositoryInterface
{
    public function all($columns = array('*'));
    public function paginate($perPage = 15, $columns = array('*'));
    public function create(array $data);
    public function update(array $data, $id);
    public function delete($id);
    public function find($id, $columns = array('*'));
    public function findBy($field, $value, $columns = array('*'));
}

interface CustomerRepositoryInterface extends RepositoryInterface
{
    public function findActive();
}

abstract class BaseRepository implements RepositoryInterface
{
    public function all($columns = array('*')) {}
    public function paginate($perPage = 15, $columns = array('*'))  {}
    public function create(array $data)  {}
    public function update(array $data, $id) {}
    public function delete($id)  {}
    public function find($id, $columns = array('*'))  {}
    public function findBy($field, $value, $columns = array('*'))  {}
}

class CustomerRepository extends BaseRepository implements CustomerRepositoryInterface
{
    public function findActive()  {
        echo "findActive";
    }
}

$customerRepository = new CustomerRepository();
$customerRepository->findActive();