物化视图和命令查询职责分离_关于命令查询分离和面向对象的设计

论坛 期权论坛     
选择匿名的用户   2021-5-21 16:06   348   0
<article style="font-size: 16px;">
<p>物化视图和命令查询职责分离</p>
<div>
  <section></section>
  <section>
   <div>
    <div>
     <h2>什么是CQS / CQRS(命令和查询分离)?<span style="font-weight: bold;">(</span>What is CQS/CQRS (Command &amp; Query Separation)?<span style="font-weight: bold;">)</span></h2>
     <p>First, let’s agree that each method of a class can do <strong>one </strong>or <strong>both </strong>things:</p>
     <p> 首先,让我们同意一个类的每个方法可以做<strong>一</strong>件事或<strong>两</strong>件事:</p>
     <ol><li>It can mutate a private value of the class (hence you tell the object to mutate it, therefore it is also called a “Command”).<p class="nodelete"></p> 它可以更改类的私有值(因此,您告诉对象对其进行更改,因此也称为“命令”)。 </li><li>It can return an object/value as a result or additional data (hence you want to query the object to tell you something).<p class="nodelete"></p> 它可以返回结果的对象/值或其他数据(因此您要查询该对象以告诉您一些信息)。 </li></ol>
     <p>Let’s see some examples (TypeScript):</p>
     <p> 让我们看一些示例(TypeScript):</p>
     <p><em>Don’t worry about the long class, you can instantly skip to the methods explanation right beneath it.</em></p>
     <p> <em>不用担心冗长的类,您可以立即跳到其下面的方法说明。</em></p>
     <pre class="blockcode"><code>class MyNumber
{
    private innerInteger: number &#61; 0;
    ...
    ...
    ...


    // This is a command, you ask the object of class &#39;Number&#39; to Increment
    // it&#39;s value, notice that it does not return anything, simply mutating
    // inner properties and nothing else.
    public increment(): void
    {
        this.innerInteger&#43;&#43;;
    }


    // This is a query, you ask the object of class &#39;Number&#39; for some information.
    // Notice that it does not change/mutate any inner properties, this is a simple get method.
    // although it can do any computation it see fits to do so.
    public value(): number
    {
        return this.innerInteger;
    }


    // Another example for query,
    public isLargerThan(other: MyNumber): boolean
    {
        return (this.value() &gt; other.value())
    }


    // This is an example for a method that does both.
    // You command the object to change it&#39;s inner value to some other value
    // AND query it to tell you if the value was changed at all.
    public changeTo(other: MyNumber): boolean
    {
        if (this.value() &#61;&#61; other.value())
            return false;
        
        this.innerInteger &#61; other.value();
        return true;
    }
}</code></pre>
     <p>Let’s look at methods by category:</p>
     <p>让我们按类别查看方法:</p>
     <p><strong>Command:</strong></p>
     <p> <strong>命令:</strong></p>
     <pre class="blockcode"><code>public increment(): void
{
    this.innerInteger&#43;&#43;;
}</code></pre>
     <p>You can see that we ask the object to do something, we <em>command </em>it to mutate and increment its inner value by 1.</p>
     <p>可以看到,我们要求对象执行某项操作,<em>命令</em>它进行变异并将其内部值增加1。</p>
     <p><strong>Query:</strong></p>
     <p> <strong>查询:</strong></p>
     <p>The following method is a simple ‘gettter’ of the property <strong>innerInteger, </strong>we ask the object to tell us things (what is your value?), and not do things.</p>
     <p> 下面的方法是属性<strong>innerInteger</strong>的简单“ getter” <strong>,</strong>我们要求对象告诉我们事情(您的价值是什么?),而不是做事情。</p>
     <pre class="blockcode"><code>public value(): number
{
   return this.innerInteger;
}</code></pre>
     <p>As the method is a simple ‘getter’ it is a bit hard to understand the concept of query, so I’ve written another method that simply asks the object “are you larger than the other object?”.</p>
     <p>由于该方法是一个简单的“获取器”,因此很难理解查询的概念,因此我编写了另一种方法,只是询问对象“您比另一个对象大吗?”。</p>
     <pre class="blockcode"><code>public isLargerThan(other: MyNumber): boolean
{
   return (this.value() &gt; other.value())
}</code></pre>
     <p><strong>Both Command &amp; Query:</strong></p>
     <p><strong>命令和查询:</strong></p>
     <p>The function ‘changeTo’ as its name implies, tells the object to change its value to some other value, but only in case it does not already have that value, and then it returns (it tells us) if the value was indeed changed or not.</p>
     <p> 顾名思义,函数“ changeTo”告诉对象将其值更改为其他值,但前提是该对象尚未具有该值,然后返回(告诉我们)该值是否确实已更改或不。</p>
     <pre class="blockcode"><code>public changeTo(other: MyNumber): boolean
{
   if (this.value() &#61;&#61; other.value())
      return false;
      
   this.innerInteger &#61; other.value();
   return true;
}</code></pre>
     <p>So, what about separation? the CQS principle says that we should separate commands from queries
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP