承接 dealnews/repository 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dealnews/repository

最新稳定版本:v4.1.0

Composer 安装命令:

composer require dealnews/repository

包简介

In Process/Request Data Repository

README 文档

README

This library provides a generic data repository which is capable of loading data on demand through registered callbacks and ensures the data is only loaded once while the repository object exists. This is useful if different parts of an application will need to use the same data for different purposes.

It also supports writing data through the repository.

Usage

Basic Example

<?php

$repo = new \DealNews\Repository\Repository();
$dao = new My_Data_Object();

// assuming $dao has a load() method that takes
// an array of ids
$repo->register("my_data", [$dao, "load"]);

// $data is returned as an array with keys matching the ids passed
// in to the getMulti() method
$data = $repo->getMulti("my_data", [1,2,3]);

// Or, the get method can be used to return just on object
$obj = $repo->get("my_data", 1);

Writing

<?php

$repo = new \DealNews\Repository\Repository();
$dao = new My_Data_Object();
// assuming $dao has a load() method that takes
// an array of ids
$repo->register("my_data", [$dao, "load"], [$dao, "save"]);

$foo = new Foo();
$foo->name = "example";

// save returns the data (possibly updated by the storage later)
// that it is sent
$foo = $repo->save("my_data", $foo);

Our Use Case

At DealNews, we use a singleton of the Repository with the needed handlers registered for the application.

Below is an example of how a Repository object could be built.

<?php

namespace MyAPP;

class DataRepository {
    public static function init() {
        static $repo;
        if (empty($repo)) {
            $repo = new \DealNews\Repository\Repository();

            // All of these handlers are only setting read callbacks
            // There is no writing for this repository.

            $book = new Book();
            $repo->register("book", [$book, "fetch"]);

            $author = new Author();
            $repo->register("author", [$author, "fetch"]);

        }
        return $repo;
    }
}

统计信息

  • 总下载量: 653
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 5
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-07-05