Cov khoom siv tau zoo nrog Eloquent?

Lub lim tiam dhau los kuv tau sau tsab xov xwm hais txog qhov tsis muaj txiaj ntsig ntawm Repository template rau Eloquent entities, txawm li cas los xij, nws tau cog lus tias yuav qhia kuv yuav ua li cas siv ib nrab rau nws kom zoo dua. Txhawm rau ua qhov no, Kuv yuav sim txheeb xyuas seb tus qauv no feem ntau siv li cas hauv cov haujlwm. Yam tsawg kawg nkaus uas yuav tsum tau muaj txheej txheem rau ib tug repository:

<?php
interface PostRepository
{
    public function getById($id): Post;
    public function save(Post $post);
    public function delete($id);
}

Txawm li cas los xij, hauv cov haujlwm tiag tiag, yog tias nws tau txiav txim siab siv cov chaw khaws cia, cov txheej txheem rau kev khaws cov ntaub ntawv feem ntau ntxiv rau lawv:

<?php
interface PostRepository
{
    public function getById($id): Post;
    public function save(Post $post);
    public function delete($id);

    public function getLastPosts();
    public function getTopPosts();
    public function getUserPosts($userId);
}

Cov txheej txheem no tuaj yeem siv los ntawm Eloquent scopes, tab sis overloading chav kawm nrog lub luag haujlwm ntawm kev nqa lawv tus kheej tsis yog lub tswv yim zoo tshaj plaws, thiab txav lub luag haujlwm no mus rau cov chav kawm repository zoo li muaj laj thawj. Puas yog li ntawd? Kuv tshwj xeeb visually faib qhov interface no ua ob ntu. Thawj feem ntawm cov txheej txheem yuav siv rau hauv kev sau ntawv.

Cov txheej txheem sau ntawv yog:

  • kev tsim kho cov khoom tshiab thiab kev sib tw PostRepository:: txuag
  • PostRepository::getById, qhov chaw manipulation thiab summoning PostRepository:: txuag
  • muaj kev sib tw PostRepository::delete

Kev sau ntawv tsis siv txoj hauv kev nqa. Hauv kev nyeem ntawv, tsuas yog siv tau * txoj hauv kev. Yog koj nyeem txog Interface Segregation Principple (ntawv I Π² KHOOM NO), tom qab ntawd nws yuav pom tseeb tias peb lub interface loj dhau thiab ua haujlwm tsawg kawg yog ob lub luag haujlwm sib txawv. Nws yog lub sij hawm los faib nws los ntawm ob. Txoj kev getById yog tsim nyog nyob rau hauv ob qho tib si, tab sis raws li daim ntawv thov yuav complex, nws cov kev siv yuav txawv. Peb yuav pom qhov no me ntsis tom qab. Kuv tau sau txog qhov tsis muaj txiaj ntsig ntawm kev sau ib feem hauv tsab xov xwm dhau los, yog li hauv qhov no kuv tsuas yog tsis nco qab txog nws.

Qhov Kev Nyeem Ntawv zoo li kuv tsis muaj txiaj ntsig, txij li txawm tias Eloquent tuaj yeem muaj ntau qhov kev siv ntawm no. Yuav npe li cas rau chav kawm? Ua tau ReadPostRepository, tab sis rau tus qauv Repository nws twb muaj me ntsis cuam tshuam. Koj ua tau xwb PostQueries:

<?php
interface PostQueries
{
    public function getById($id): Post;
    public function getLastPosts();
    public function getTopPosts();
    public function getUserPosts($userId);
}

Siv nws nrog Eloquent yog qhov yooj yim heev:

<?php
final class EloquentPostQueries implements PostQueries
{
    public function getById($id): Post
    {
        return Post::findOrFail($id);
    }

    /**
    * @return Post[] | Collection
    */
    public function getLastPosts()
    {
        return Post::orderBy('created_at', 'desc')
            ->limit(/*some limit*/)
            ->get();
    }
    /**
    * @return Post[] | Collection
    */
    public function getTopPosts()
    {
        return Post::orderBy('rating', 'desc')
            ->limit(/*some limit*/)
            ->get();
    }

    /**
    * @param int $userId
    * @return Post[] | Collection
    */
    public function getUserPosts($userId)
    {
        return Post::whereUserId($userId)
            ->orderBy('created_at', 'desc')
            ->get();
    }
}

Lub interface yuav tsum tau txuam nrog kev siv, piv txwv li hauv AppServiceProvider:

<?php
final class AppServiceProvider extends ServiceProvider 
{
    public function register()
    {
        $this->app->bind(PostQueries::class, 
            EloquentPostQueries::class);
    }
}

Cov chav kawm no twb muaj txiaj ntsig. Nws paub txog nws lub luag haujlwm los ntawm kev tshem tawm cov tswj lossis cov chav kawm. Hauv kev tswj hwm nws tuaj yeem siv tau zoo li no:

<?php
final class PostsController extends Controller
{
    public function lastPosts(PostQueries $postQueries)
    {
        return view('posts.last', [
            'posts' => $postQueries->getLastPosts(),
        ]);
    }
} 

Txujci PostsController::lastPosts tsuas yog thov rau qee qhov kev siv Cov lus nug thiab ua haujlwm nrog nws. Hauv tus neeg muab kev pabcuam peb txuas PostQueries nrog chav kawm EloquentPostQueries thiab chav kawm no yuav raug hloov mus rau hauv tus tswj.

Cia peb xav txog tias peb daim ntawv thov tau nrov heev. Ntau txhiab tus neeg siv ib feeb qhib nplooj ntawv nrog cov ntawv tshaj tawm tshiab. Cov ntawv tshaj tawm uas nrov tshaj plaws kuj tau nyeem ntau zaus. Databases tsis lis cov loads zoo heev, yog li lawv siv cov txheej txheem kev daws teeb meem - cache. Ntxiv nrog rau cov ntaub ntawv, qee cov ntaub ntawv snapshot yog khaws cia hauv kev cia kom zoo rau qee yam haujlwm - memcached los yog liab.

Caching logic feem ntau tsis nyuaj, tab sis kev siv nws hauv EloquentPostQueries tsis yog qhov tseeb (yog tias tsuas yog vim Lub Luag Haujlwm Tib Leeg). Nws yog ntau yam ntuj los siv tus qauv Kho kom zoo nkauj thiab siv caching raws li kho kom zoo nkauj rau lub ntsiab ua:

<?php
use IlluminateContractsCacheRepository;

final class CachedPostQueries implements PostQueries
{
    const LASTS_DURATION = 10;

    /** @var PostQueries */
    private $base;

    /** @var Repository */
    private $cache;

    public function __construct(
        PostQueries $base, Repository $cache) 
    {
        $this->base = $base;
        $this->cache = $cache;
    }

    /**
    * @return Post[] | Collection
    */
    public function getLastPosts()
    {
        return $this->cache->remember('last_posts', 
            self::LASTS_DURATION, 
            function(){
                return $this->base->getLastPosts();
            });
    }

    // Π΄Ρ€ΡƒΠ³ΠΈΠ΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ практичСски Ρ‚Π°ΠΊΠΈΠ΅ ΠΆΠ΅
}

Tsis quav ntsej lub interface Repository nyob rau hauv lub constructor. Rau qee qhov laj thawj tsis paub, lawv tau txiav txim siab sau npe lub interface rau caching hauv Laravel li no.

Класс CachedPostQueries siv caching nkaus xwb. $this->cache-> nco ntsoov xyuas seb qhov kev nkag no puas nyob hauv cache thiab yog tias tsis yog, ces hu xov tooj rov qab thiab sau tus nqi xa rov qab mus rau cache. Txhua yam uas tseem tshuav yog siv cov chav kawm no rau hauv daim ntawv thov. Peb xav tau tag nrho cov chav kawm uas nyob rau hauv daim ntawv thov thov kev siv ntawm lub interface PostQueries pib txais ib qho piv txwv ntawm chav kawm CachedPostQueries. Txawm li cas los xij, nws tus kheej CachedPostQueries tus constructor yuav tsum tau txais ib chav kawm raws li ib tug parameter EloquentPostQueriesvim nws tsis tuaj yeem ua haujlwm yam tsis muaj kev siv "tiag tiag". Peb hloov AppServiceProvider:

<?php
final class AppServiceProvider extends ServiceProvider 
{
    public function register()
    {
        $this->app->bind(PostQueries::class, 
            CachedPostQueries::class);

        $this->app->when(CachedPostQueries::class)
            ->needs(PostQueries::class)
            ->give(EloquentPostQueries::class);
    }
}

Tag nrho kuv cov kev xav tau zoo heev tau piav qhia hauv tus neeg muab kev pabcuam. Yog li, peb tau siv caching rau peb qhov kev thov tsuas yog sau ib chav kawm thiab hloov lub thawv configuration. Tus so ntawm daim ntawv thov code tsis tau hloov.

Tau kawg, kom ua tiav kev siv caching, nws tseem yuav tsum tau siv qhov tsis raug cai kom cov ntawv tshem tawm tsis dai rau ntawm qhov chaw rau qee lub sijhawm, tab sis raug tshem tawm tam sim ntawd. Tab sis cov no yog yam me me.

Cov kab hauv qab: peb tsis siv ib qho, tab sis ob lub qauv. Qauv Command Query Responsibility Segregation (CQRS) proposes kom cais tag nrho nyeem thiab sau ua haujlwm ntawm theem interface. Kuv los rau ntawm nws Interface Segregation Principple, uas qhia tias kuv txawj siv cov qauv thiab cov hauv paus ntsiab lus thiab muab ib qho los ntawm lwm tus raws li kev xav :) Tau kawg, tsis yog txhua qhov project xav tau kev paub daws teeb meem rau kev xaiv cov koom haum, tab sis kuv yuav qhia qhov ua kom yuam kev rau koj thaum pib ntawm daim ntawv thov. kev loj hlob, koj tuaj yeem tsim ib chav kawm PostQueries nrog rau kev ua raws li niaj zaus ntawm Eloquent:

<?php
final class PostQueries
{
    public function getById($id): Post
    {
        return Post::findOrFail($id);
    }

    // Π΄Ρ€ΡƒΠ³ΠΈΠ΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹
}

Thaum qhov kev xav tau ntawm caching tshwm sim, nrog rau qhov yooj yim txav koj tuaj yeem tsim qhov interface (lossis chav kawm paub daws teeb meem) nyob rau hauv qhov chaw ntawm chav kawm no PostQueries, luam nws qhov kev siv rau hauv chav kawm EloquentPostQueries thiab mus rau lub tswvyim uas kuv tau piav ua ntej lawm. Tus so ntawm daim ntawv thov code tsis tas yuav hloov.

Tag nrho cov tricks no nrog cov chav kawm, interfaces, Cawv Raug Txhaj ΠΈ CQRS piav qhia meej hauv kuv phau ntawv "Architecture of Complex Web Applications". Kuj tseem muaj kev daws teeb meem rau cov lus tsis txaus ntseeg vim li cas tag nrho kuv cov chav kawm hauv cov piv txwv rau tsab xov xwm no raug cim tias kawg.

Tau qhov twg los: www.hab.com

Ntxiv ib saib