2.md 55.7 KB
Newer Older
W
wizardforcel 已提交
1
# 二、事件管理
W
wizardforcel 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

事件管理可以定义为解决客户问题所采取的一系列步骤,是客户服务的核心。在管理客户事件时,清晰的沟通是成功的关键。

管理客户和团队的期望是良好沟通的基本要素。简而言之,您的客户应该知道他们报告问题后会发生什么(一个清晰的路线图和持续的反馈循环至关重要),您的经理应该知道您将在特定事件上花费多少时间和精力。

管理客户事件的期望首先要问用户和老板对你的期望是否可以实现。在回答之前,你还必须问自己,就时间限制而言,你能合理地期望自己做什么。

如果被问的是不合理的,你必须后退一步,向各方(主要是你的老板)说明这一点,并向客户做出协调一致的解释,说明为什么回应当前的请求是不可行的。另一方面,如果请求合理,您应该尽快做出响应,给出明确的时间估计,并指出解决客户问题所涉及的步骤。

管理对客户事件的预期意味着理解以下内容:

*   当用户希望快速解决问题时,你的老板可能会希望你在这项任务上花更少的时间,而是要求快速解决问题。
*   你必须明白自己的极限,让所有相关方知道什么是合理的。
*   如果你是主要的联系人,你出去的时候一定要让人知道。
*   如果你手头没有一个完整的回应或解决方案,一句简单的“别担心,我有这个”会有很大帮助。

事件管理还包括确定需要多少沟通、沟通的语气,以及最重要的沟通频率。

W
wizardforcel 已提交
20
在本章中,我们将通过创建 C# 解决方案来自动化事件管理,该解决方案包括期望管理和成功关闭任何报告的事件所需的沟通循环。大多数帮助台和客户关系管理工具并不关注期望管理或沟通的语气和频率,但是当这些工具直接包含在您的项目或产品中时,它们应该是有用的,并且它们将允许您在您公司的产品线中开箱即用地集成这些功能。
W
wizardforcel 已提交
21 22 23 24 25

客户关系管理是为了管理公司与当前和未来客户的互动而采取的方法。

大多数当前的客户关系管理软件,如 Salesforce 和 Microsoft Dynamics,更侧重于结束销售周期,而不是事件管理本身。尽管大多数客户关系管理软件都包含服务台子集,但我检查过的客户关系管理工具(甚至是那些专门为客户支持而构建的工具,如 Zendesk)都不太重视期望管理和沟通的频率或语气。

W
wizardforcel 已提交
26
我的建议是,你提供一个 C# 类,负责管理围绕沟通的期望,以及与任何报告的客户事件相关的沟通频率和语气。该类可以作为项目或产品中的一个可嵌入的、面向小型事件管理的客户关系管理系统,允许客户在产品本身中向您的 CORD 服务台报告问题。
W
wizardforcel 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

表 4 代表了我们的简单可怕的客户关系管理工具将强调的规格。

表 4:简单易用的客户关系管理工具的规格

| 允许产品内的用户向 CORD 服务台报告问题。 |
| 明确定义和重新定义客户对问题的期望。 |
| 管理和设置通信频率。 |
| 向用户报告事件的状态和阶段,直到问题得到解决。 |
| 向服务台工作人员表明需要跟进或提供更新。 |
| 向用户指示为任务分配的资源及其可用性。 |

写完这些想法,让我们考虑如何编写代码。理想情况下,简单可怕的客户关系管理工具应该能够将其数据存储在云中,以便可以从任何位置轻松存储和检索。

当涉及到在云中存储数据时,使用传统的关系数据库(例如,MySQL、SQL Server、Postgres SQL、Oracle 等)有无数种选择。)和 NoSQL(如 MongoDB、DynamoDB、RavenDB 等。)可以私有托管,或者托管在亚马逊网络服务(AWS)或微软 Azure 等服务上。

尽管近年来在 AWS 或 Azure 上设置任何关系或非关系(NoSQL)数据库已经大大简化,但您仍然必须调配实例和吞吐量,并且您必须考虑请求和定价等因素,以便正确部署在线数据存储。这是一个相当复杂的过程,坦率地说,要求很高。

我们的目标是构建一个简单的客户关系管理工具,它可以很容易地嵌入到现有的项目和产品中,而不会导致我们在部署在线数据存储时绞尽脑汁。

表 5 展示了我们的数据存储应该允许我们的简单的令人敬畏的客户关系管理工具实现什么。

表 5:简单棒极了的客户关系管理工具数据存储规格

| 零维护,开箱即用。 |
| 可扩展,对吞吐量和请求没有限制。 |
| 廉价、无模式、出色的安全特性。 |
| NoSQL(非关系)数据库,但具有 SQL 语言功能。 |

现在,等一下。乍一看,我们的要求似乎有点过分。一个几乎无限可扩展的、无模式的 NoSQL 数据库,它允许与 SQL 兼容的查询功能?这真的存在吗?

嗯,是的,这样的奇迹确实存在。微软的员工已经开发了一个非常灵活和令人印象深刻的、完全托管的 NoSQL 文档(JSON)数据库,该数据库具有 SQL 查询语言功能。叫做 DocumentDB,可以在[微软 Azure](https://azure.microsoft.com/en-us/) 找到。

为了编写简单易用的客户关系管理工具,我们将使用。DocumentDB 的. NET SDK,可以在 [GitHub](https://github.com/Azure/azure-documentdb-dotnet) 上找到。

虽然显示的代码示例对于后续操作来说非常直观,但我建议您查看一下优秀的在线文档库[文档](https://azure.microsoft.com/en-us/documentation/services/documentdb/)

W
wizardforcel 已提交
64
根据[表 4](#Table4) 中的规格,我们需要创建一个 C# 基类,它应该能够:
W
wizardforcel 已提交
65 66 67 68

*   允许客户报告事件及其期望(严重性和重要性)。
*   允许客户进行一次或多次交流互动。
*   允许客户设置所需的通信交互频率级别(双向)。这应该提醒服务台跟进或采取行动。
W
wizardforcel 已提交
69
*   允许服务台报告事件状态(如[表 2](1.html#Table2) 中所述),直到解决为止,明确指出修复的可能交付日期。
W
wizardforcel 已提交
70 71 72 73
*   明确告知客户在每个步骤中分配的资源(负责人)和状态。

我们需要在 Microsoft Azure 上创建一个 DocumentDB 实例,您需要使用 Microsoft 帐户登录或注册 Azure。你可以通过访问 azure.microsoft.com 来实现。

W
wizardforcel 已提交
74
![](img/00003.jpeg)
W
wizardforcel 已提交
75 76 77 78 79

图 1:微软 Azure 登录屏幕

注册或登录 Azure 门户后,您可以浏览 Azure 服务列表并选择`DocumentDB Accounts`选项。

W
wizardforcel 已提交
80
![](img/00004.jpeg)
W
wizardforcel 已提交
81 82 83 84 85

图 Azure 服务列表中的文档数据库

选择文档数据库后,必须通过点击`Add`创建一个新的文档数据库账户。

W
wizardforcel 已提交
86
![](img/00005.jpeg)
W
wizardforcel 已提交
87 88 89 90 91 92 93

图 3:添加文档数据库帐户的屏幕

这将导致一个屏幕,允许您输入文档数据库帐户的详细信息:标识、NoSQL 应用编程接口、订阅、资源组和位置。您可以选择将托管您的帐户的 Azure 区域。

该标识是微软 Azure 中文档数据库帐户的唯一全局标识符。要完成帐户的创建,请单击`Create`。DocumentDB 帐户创建过程可能需要几分钟时间。

W
wizardforcel 已提交
94
![](img/00006.jpeg)
W
wizardforcel 已提交
95 96 97 98 99

图 4:最终文档数据库帐户创建屏幕

图 5 描述了 DocumentDB 帐户在创建后将如何显示。

W
wizardforcel 已提交
100
![](img/00007.jpeg)
W
wizardforcel 已提交
101 102 103 104 105 106 107

图 5:文档数据库帐户仪表板

一个 DocumentDB 帐户可以托管多个 DocumentDB 数据库,但是简单地拥有一个 DocumentDB 帐户并不意味着您可以随时使用一个 DocumentDB 数据库。但是,这样的数据库可以通过点击`Add Database`来创建。

唯一的要求是为新的文档数据库提供一个标识。

W
wizardforcel 已提交
108
![](img/00008.jpeg)
W
wizardforcel 已提交
109 110 111 112 113

图 6:新的文档数据库创建屏幕

创建文档数据库后,可以通过直观的仪表板进行配置。

W
wizardforcel 已提交
114
![](img/00009.jpeg)
W
wizardforcel 已提交
115 116 117 118 119 120 121

图 7:文档数据库仪表板

在 DocumentDB 中,JSON 文档存储在集合下。集合是 JSON 文档和相关的 JavaScript 应用程序逻辑(用户函数、存储过程和触发器)的容器。

图 8 描述了文档数据库的结构。

W
wizardforcel 已提交
122
![](img/00010.jpeg)
W
wizardforcel 已提交
123 124 125 126 127

图 8:文档数据库内部结构

收款是一个可计费的实体,其成本由与收款相关的绩效水平决定。性能级别(S1、S2 和 S3)提供 10GB 的存储和固定的吞吐量。

W
wizardforcel 已提交
128
![](img/00011.jpeg)
W
wizardforcel 已提交
129 130 131 132 133 134 135 136 137

图 9:文档数据库集合的性能级别和定价层

请求单位可以用秒来衡量。例如,在 S1 级别,每秒可以执行 250 个 RUs(应用编程接口调用)。关于 DocumentDB 中性能级别的更多信息,可以在[这里](https://azure.microsoft.com/en-us/documentation/articles/documentdb-performance-levels/)找到。

创建集合时,默认定价层是 S2。但是,对于演示和概念验证(POCs),S1 层级就足够了。

点击仪表盘上的`Add Collection`可以创建一个集合。您需要指定定价级别(通过选择 S1)和索引策略。索引策略的默认设置实际上是默认设置。如果您想利用全字符串查询,只需在创建集合后在`Scale & Settings`选项中将索引策略从默认更改为哈希即可。打开`Indexing Policy`选项,将`indexingMode`属性从`consistent`更改为`hash`。对于简单可怕的客户关系管理,我们将使用哈希索引策略。

W
wizardforcel 已提交
138
| ![](img/00012.jpeg) | ![](img/00013.jpeg) |
W
wizardforcel 已提交
139 140 141 142 143

图 10:创建文档数据库集合并更改索引策略

现在,文档数据库帐户、数据库和集合已经在微软 Azure 上准备好了,接下来您必须安装。为了开始编码,NuGet 的. NET DocumentDB 客户端库调用了`Microsoft.Azure.DocumentDB`

W
wizardforcel 已提交
144
首先,启动 Visual Studio 2015 并创建一个 C# 控制台应用程序。模板代码加载后,转到`Tools` > `NuGet Package Manager` > `Manage NuGet Packages`,在`Package Source`下拉控件中选择`nuget.org`。在搜索框中,输入`DocumentDB`,将显示软件包,可供安装。
W
wizardforcel 已提交
145

W
wizardforcel 已提交
146
![](img/00014.jpeg)
W
wizardforcel 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

一旦。NET DocumentDB 客户端已经安装,您将在您的 Visual Studio 解决方案中引用`Microsoft.Azure.Documents.Client``Newtonsoft.Json`程序集。

随着 DocumentDB 连接就绪,我们可以开始编码了。

根据规定的指导原则,我们的简单卓越的客户关系管理软件应该能够允许用户打开事件、按属性查找事件、更改事件属性、添加注释,并允许服务台工作人员指示为解决事件分配的任何资源。

DocumentDB 支持数值字段的基于范围的索引,允许您进行范围查询(例如,其中字段> 10,字段< 20)。为了避免在对日期进行范围查询时进行昂贵的扫描(例如,早于昨天的记录或上周的记录),我们需要将日期的字符串表示转换为数字。这将允许我们在这些字段上使用范围索引。

我们将把`DateTime`值视为纪元值(自特定日期以来的秒数)。这节课我们以 1970 年 1 月 1 日 00:00 为起点;但是,您可以自由使用不同的值。

现在让我们研究如何使用 Visual Studio 2015 和实现这一点。NET 4.5.2。

首先,我们将创建一个名为`CrmCore.DateEpoch`的名称空间,负责实现纪元值。这将包括`DateEpoch``Extension`课程。

稍后,我们将需要实现一个`CrmCore.Enum`命名空间,其中将定义枚举定义,并且我们将需要创建一个名为`CrmCore.EnumUtils`的实用程序命名空间。

稍后,我们还需要实现一个`CrmCore.Enum`命名空间,其中将定义枚举定义,我们还需要一个名为`CrmCore.EnumUtils`的实用程序命名空间。

代码清单 1:实现纪元

W
wizardforcel 已提交
168
```cs
W
wizardforcel 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  using System;

  namespace CrmCore.DateEpoch
  {

  public static class Extensions

  {

  public static int ToEpoch(this DateTime date)

  {

  if (date == null) return int.MinValue;

  DateTime epoch = new DateTime(1970, 1, 1);

  TimeSpan epochTimeSpan = date - epoch;

  return (int)epochTimeSpan.TotalSeconds;

  }

  }

  public class DateEpoch

  {

  public DateTime Date { get; set;
  }

  public int
  Epoch

  {

  get

  {

  return (Date.Equals(null) || 

  Date.Equals(DateTime.MinValue))

  ? DateTime.UtcNow.ToEpoch()

  : Date.ToEpoch();

  }

  }

  public DateEpoch(DateTime dt)

  {

  Date = dt;

  }

  }
  }

```

既然我们已经看到了 DocumentDB 将如何处理日期,我们还需要定义几个`Enum`类型,用于指示事件的状态、严重性、反馈频率和通信类型。

代码清单 2:表示事件状态的枚举

W
wizardforcel 已提交
239
```cs
W
wizardforcel 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  using System;

  namespace CrmCore.Enums
  { 

  public enum IncidentSeverity {

  [Description("Urgent")]

  Urgent,

  [Description("High")]

  High,

  [Description("Normal")]

  Normal,

  [Description("Low")]

  Low

  };

  public enum
  IncidentFeedbackFrequency {

  [Description("Hourly")]

  Hourly,

  [Description("Every4Hours")]

  Every4Hours,

  [Description("Every8Hours")]

       Every8Hours,

  [Description("Daily")]

  Daily,

  [Description("Every2Days")]

  Every2Days,

  [Description("Weekly")]

  Weekly,

  [Description("Every2Weeks")]

  Every2Weeks,

  [Description("Monthly")]

  Monthly

  };

  public enum
  IncidentCommunicationType {

  [Description("ReceiveUpdatesOnly")]

  ReceiveUpdatesOnly,

  [Description("Bidirectional")]

  Bidirectional

  };

  public enum
  IncidentStatus {

  [Description("NotReported")]

  NotReported,

  [Description("Reported")]

  Reported,

  [Description("FeedbackRequested")]

  FeedbackRequested,

  [Description("UnderAnalysis")]

  UnderAnalysis,

  [Description("IssueFound")]

  IssueFound,

  [Description("WorkingOnFix")]

  WorkingOnFix,

  [Description("FixDelivered")]

  FixDelivered,

  [Description("FixAccepted")]

  FixAccepted,

  [Description("Solved")]

  Solved,

  [Description("Closed")]

  Closed,

  [Description("Reopen")]

  Reopen

  };
  }

```

随着`IncidentSeverity``IncidentFeedbackFrequency``IncidentCommunicationType``IncidentStatus`的到位,我们有必要的类别来分配使用系统提交的任何事件。

但是,考虑到 DocumentDB 与 JSON 一起工作,需要理解的是,当转换为 JSON 时,一个 C# `enum`将存储为一个整数,这可能会使浏览 DocumentDB 上的文档列表时难以阅读。

因此,为了使任何`Enum`值作为`string`可读,我们必须在将该值存储在 DocumentDB 上之前使用`System.ComponentModel``System.Reflection`。让我们实现一个`EnumUtils`类来处理这个过程。

代码清单 3:枚举类

W
wizardforcel 已提交
375
```cs
W
wizardforcel 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  using System;
  using System.ComponentModel;
  using System.Reflection;

  namespace CrmCore.EnumUtils
  { 

  public class EnumUtils

  {

  protected const string cStrExcep = 

  "The string is
  not a description or value of the enum.";

  public static string stringValueOf(Enum value)

  {

  FieldInfo fi =
  value.GetType().GetField(value.ToString());

  DescriptionAttribute[] attributes = (DescriptionAttribute[])

  fi.GetCustomAttributes(typeof(DescriptionAttribute), 

  false);

  return (attributes.Length > 0) ?
  attributes[0].Description : 

  value.ToString();

  }

  public static object enumValueOf(string value, Type
  enumType)

  {

  string[] names = Enum.GetNames(enumType);

  foreach (string name in names)

  {

  if (stringValueOf((Enum)Enum.Parse(enumType, 

  name)).Equals(value))

  {

               return Enum.Parse(enumType, name);

  }

  }

  throw new
  ArgumentException(cStrExcep);

  }

  }
  }

```

`stringValueOf`方法将一个整数`Enum`值转换为其`string`表示,`enumValueOf`取一个`string`值并将其转换为其对应的整数`Enum`值(给定其类型)。

`EnumUtils`是 Simple Awesome CRM 系统中非常方便和重要的一部分,因为它将被用于接下来的许多方法中。

现在,我们已经知道如何在 DocumentDB 中处理日期,并通过使用`Enum`类型对事件进行分类,让我们创建存储事件详细信息的类。

代码清单 4:事件详细信息类

W
wizardforcel 已提交
453
```cs
W
wizardforcel 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
  using CrmCore.EnumUtils;
  using CrmCore.Enums;
  using CrmCore.DateEpoch;

  using System;
  using System.ComponentModel;
  using System.Reflection;

  namespace CrmCore.IncidentInfo
  { 

  public sealed class AllocatedResource

  {

  private IncidentStatus stage;

  public string Engineer { get; set;
  }

  public string Stage {

  get

  {

  return EnumUtils.stringValueOf(stage);

  }

  set

  {

  stage = (IncidentStatus)EnumUtils.

  enumValueOf(value, typeof(IncidentStatus));

           }

  }

  public DateEpoch Start { get; set;
  }

  public DateEpoch End { get; set;
  }

  }

  public sealed class Comment

  {

  public string Description { get; set; }

  public string UserId { get; set;
  }

  public string AttachmentUrl { get; set; }

  public DateEpoch When { get; set;
  }

  }

  public sealed class IncidentInfo

  {

  private IncidentSeverity severity;

  private IncidentStatus status;

  private IncidentFeedbackFrequency feedbackFrequency;

  private IncidentCommunicationType communicationType;

  public string Description { get; set; }

  public string Severity

  {

  get

  {

  return EnumUtils.stringValueOf(severity);

  }

  set

  {

  severity = (IncidentSeverity)EnumUtils.

  enumValueOf(value, typeof(IncidentSeverity));

  }

  }

  public string Status

  {

  get

         {

  return EnumUtils.stringValueOf(status);

  }

  set

  {

  status = (IncidentStatus)EnumUtils.

  enumValueOf(value, typeof(IncidentStatus));

  }

  }

    public string FeedbackFrequency

  {

  get

  {

  return EnumUtils.stringValueOf(feedbackFrequency);

  }

  set

  {

  feedbackFrequency = (IncidentFeedbackFrequency)EnumUtils.

  enumValueOf(value,typeof(IncidentFeedbackFrequency));

  }

  }

  public string CommunicationType

  {

  get

  {

  return EnumUtils.stringValueOf(communicationType);

       }

  set

  {

  communicationType = (IncidentCommunicationType)EnumUtils.

  enumValueOf(value,typeof(IncidentCommunicationType));

  }

  }

  public AllocatedResource[] Resources { get; set; }

  public Comment[] Comments { get; set; }

  public DateEpoch Opened { get; set;
  }

  public DateEpoch Closed { get; set;
  }

  }
  }

```

W
wizardforcel 已提交
645
每个特定事件的详细信息都作为一个单独的 JSON 文档存储在 DocumentDB 中,在 C# 中,它由`IncidentInfo`类表示。`IncidentInfo`类是密封的,这表明它只是一个数据容器。它可能不包含注释或资源(作为对象数组),也可能包含多个项目。
W
wizardforcel 已提交
646 647 648 649 650 651 652 653 654 655 656

`Comment`对象表示与特定事件相关的注释的实例。`AllocatedResource`对象表示分配给特定事件的资源实例。

这都是好的,但是我们还不能用`IncidentInfo`或它的相关类做任何有意义的事情,因为到目前为止它们只是数据定义。

为了与文档数据库交互并使用`IncidentInfo` `,`,我们必须创建一个`Incident`类,该类将作为负责向文档数据库发布和检索任何事件数据(`IncidentInfo`)的类。

代码清单 5 包含了`Incident`类的完整代码。接下来,我们将分别检查每种方法。

代码清单 5:事件类实现

W
wizardforcel 已提交
657
```cs
W
wizardforcel 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
  using Microsoft.Azure.Documents;
  using Microsoft.Azure.Documents.Client;
  using Microsoft.Azure.Documents.Linq;
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Reflection;
  using System.Threading.Tasks;

  using CrmCore.EnumUtils;
  using CrmCore.Enums;
  using CrmCore.DateEpoch;
  using CrmCore.IncidentInfo;

  namespace CrmCore.Incident
  {

  public class Incident : IDisposable

  {

  private bool
  disposed = false;

  private const string docDbUrl = 

  "dbs/SimpleAwesomeCrm/colls/CrmObjects";

  private const string docDbEndpointUrl = 

  "https://fastapps.documents.azure.com:443/";

  private const string docDbAuthorizationKey = "<<DocDb Key>>";

  private const string cStrSeverityProp = "Severity";

  private const string cStrStatusProp = "Status";

  private const string cStrFrequencyProp = "FeedbackFrequency";

  private const string cStrCTProp = "CommunicationType";

  private const string cStrClosedProp = "Closed";

  private const string cStrComments = "Comments";

  private const string cStrResources = "Resources";

  private const string cStrOpened = "Opened";

  public IncidentInfo info = null;

  // Class defined in
  Microsoft.Azure.Documents.Client

       protected DocumentClient client = null;

  ~Incident()

  {

  Dispose(false);

  }

  public Incident()

  {

  info = new IncidentInfo();

  info.Status = 

  EnumUtils.stringValueOf(IncidentStatus.Reported);

  info.Severity = 

  EnumUtils.stringValueOf(IncidentSeverity.Normal);

  info.FeedbackFrequency = 

  EnumUtils.stringValueOf(IncidentFeedbackFrequency.Daily);

  info.CommunicationType = 

  EnumUtils.stringValueOf(IncidentCommunicationType.

  ReceiveUpdatesOnly);

  info.Opened = new DateEpoch(DateTime.UtcNow);

  info.Resources = null;

  info.Comments = null;

  }

   public Incident(IncidentStatus status, IncidentSeverity severity,    

  IncidentFeedbackFrequency freq, 

  IncidentCommunicationType
  comType)

  {

  info = new IncidentInfo();

  info.Status = EnumUtils.stringValueOf(status);

  info.Severity = EnumUtils.stringValueOf(severity);

  info.FeedbackFrequency = EnumUtils.stringValueOf(freq);

  info.CommunicationType = EnumUtils.stringValueOf(comType);

  info.Opened = new DateEpoch(DateTime.UtcNow);

  info.Resources = null;

  info.Comments = null;

  }

  private void
  Connect2DocDb()

  {

  client = new DocumentClient(new Uri(docDbEndpointUrl), 

  docDbAuthorizationKey);

  }

          public IEnumerable<Document> FindById(string id)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery(docDbUrl)

  where c.Id == id

  select c;

  return cases;

  }

  else

  return null;

  }

  public IEnumerable<IncidentInfo> FindByDescription(string 

  description)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery

  <IncidentInfo>(docDbUrl)

  where c.Description.ToUpper().

  Contains(description.ToUpper())

  select c;

  return cases;

  }

  else

      return null;

  }

  public IEnumerable<IncidentInfo> FindByDateOpenedAfter(DateTime 

  opened)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery

  <IncidentInfo>(docDbUrl)

  where c.Opened.Epoch >=
  opened.ToEpoch()

  select c;

  return cases;

         }

  else

  return null;

  }

  public IEnumerable<IncidentInfo> FindByDateOpenedBefore(DateTime 

  opened)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery

  <IncidentInfo>(docDbUrl)

  where c.Opened.Epoch <
  opened.ToEpoch()

  select c;

  return cases;

  }

  else

  return null;

  }

  public IEnumerable<IncidentInfo> FindByDateOpenedBetween(DateTime 

  start, DateTime end)

  {

  if (client == null)

            Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery

  <IncidentInfo>(docDbUrl)

  where c.Opened.Epoch >=
  start.ToEpoch()

  where c.Opened.Epoch < end.ToEpoch()

  select c;

  return cases;

  }

  else

  return null;

  }

  public IEnumerable<IncidentInfo> FindByStatus(IncidentStatus 

  status)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery

  <IncidentInfo>(docDbUrl)

  where c.Status == status.ToString()

  select c;

  return cases;

  }

  else

  return null;

  }

  public IEnumerable<IncidentInfo> FindBySeverity(IncidentSeverity 

  severity)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

           from c in
  client.CreateDocumentQuery

  <IncidentInfo>(docDbUrl)

  where c.Severity == severity.ToString()

  select c;

  return cases;

  }

  else

     return null;

  }

  public IEnumerable<IncidentInfo> 

  FindByFeedbackFrequency(IncidentFeedbackFrequency ff)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery

  <IncidentInfo>(docDbUrl)

  where c.FeedbackFrequency ==
  ff.ToString()

  select c;

  return cases;

  }

  else

  return null;

  }

  public IEnumerable<IncidentInfo> 

  FindByCommunicationType(IncidentCommunicationType ct)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery

  <IncidentInfo>(docDbUrl)

  where c.CommunicationType == ct.ToString()

  select c;

  return cases;

  }

  else

  return null;

  }

  public async Task<Document> Open(string description, bool check = 

  false)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  info.Description = description;

  Document id = await client.CreateDocumentAsync

   (docDbUrl, info);

  if (check)

  return 

  (id != null) ?
  client.CreateDocumentQuery(docDbUrl).

  Where(d => d.Id == 

  id.Id).AsEnumerable().FirstOrDefault() :

  null;

  else

  return id;

  }

  else

  return null;

  }

  public async Task<IncidentInfo> AddResource(string id, 

  IncidentStatus stage, string engineer, DateTime
  st, DateTime end)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery(docDbUrl)

  where
  c.Id.ToUpper().Contains(id.ToUpper())

  select c;

  IncidentInfo issue = null;

  Document oDoc = null;

  foreach (var
  cs in cases)

  {

                  var it = await client.ReadDocumentAsync(cs.AltLink);

  oDoc = it;

  issue = (IncidentInfo)(dynamic)it.Resource;

  break;

  }

  if (oDoc != null)

        {

  AllocatedResource rc = new
  AllocatedResource();

  rc.End = new DateEpoch((end != null) ? 

  end.ToUniversalTime() : DateTime.UtcNow);

  rc.Engineer = engineer;

           rc.Stage = EnumUtils.stringValueOf(stage);

  rc.Start = new DateEpoch((st != null) ? 

  st.ToUniversalTime() : DateTime.UtcNow);

  List<AllocatedResource> rRsc = new 

  List<AllocatedResource>();

  if (issue?.Resources?.Length > 0)

  {

  rRsc.AddRange(issue.Resources);

  rRsc.Add(rc);

  oDoc.SetPropertyValue(cStrResources, 

  rRsc.ToArray());

  }

  else

  {

  rRsc.Add(rc);

  oDoc.SetPropertyValue(cStrResources, 

  rRsc.ToArray());

  }

  var updated = await 

  client.ReplaceDocumentAsync(oDoc);

  issue = (IncidentInfo)(dynamic)updated.Resource;

  }

  return issue;

  }

        else

  return null;

  }

  public async Task<IncidentInfo> AddComment(string id, string 

  userId, string comment, string attachUrl)

  {

  if (client == null)

  Connect2DocDb();

     if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery(docDbUrl)

  where
  c.Id.ToUpper().Contains(id.ToUpper())

  select c;

     IncidentInfo issue = null;

  Document oDoc = null;

  foreach (var
  cs in cases)

  {

  var it = await client.ReadDocumentAsync(cs.AltLink);

  oDoc = it;

  issue = (IncidentInfo)(dynamic)it.Resource;

  break;

  }

  if (oDoc != null)

  {

  Comment c = new
  Comment();

  c.AttachmentUrl = attachUrl;

    c.Description = comment;

  c.UserId = userId;

  c.When = new DateEpoch(DateTime.UtcNow);

  List<Comment> cMts = new List<Comment>();

  if (issue?.Comments?.Length > 0) {

                cMts.AddRange(issue.Comments);

  cMts.Add(c);

  oDoc.SetPropertyValue(cStrComments, 

  cMts.ToArray());

  }

  else {

      cMts.Add(c);

  oDoc.SetPropertyValue(cStrComments, 

  cMts.ToArray());

  }

  var updated = await 

  client.ReplaceDocumentAsync(oDoc);

    issue = (IncidentInfo)(dynamic)updated.Resource;

  }

  return issue;

  }

  else

  return null;

  }

  public async Task<IncidentInfo> ChangePropertyValue(string id, 

  string propValue, string propName)

  {

  if (client == null)

  Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

  from c in
  client.CreateDocumentQuery(docDbUrl)

  where
  c.Id.ToUpper().Contains(id.ToUpper())

  select c;

  IncidentInfo issue = null;

  Document oDoc = null;

  foreach (var
  cs in cases)

  {

          var it = await client.ReadDocumentAsync(cs.AltLink);

  oDoc = it;

  issue = (IncidentInfo)(dynamic)it.Resource;

  break;

  }

  if (oDoc != null)

  {

  switch (propName)

  {

  case cStrSeverityProp:

  oDoc.SetPropertyValue(cStrSeverityProp, 

  propValue);

            break;

  case cStrStatusProp:

  oDoc.SetPropertyValue(cStrStatusProp, 

  propValue);

  break;

  case cStrFrequencyProp:

  oDoc.SetPropertyValue(cStrFrequencyProp, 

  propValue);

  break;

  case cStrCTProp:

  oDoc.SetPropertyValue(cStrCTProp, propValue);

  break;

  case cStrClosedProp:

  oDoc.SetPropertyValue(cStrClosedProp, 

  issue.Closed);

  break;

  }

  var updated = await 

  client.ReplaceDocumentAsync(oDoc);

  issue = (IncidentInfo)(dynamic)updated.Resource;

  }

  return issue;

  }

  else

         return null;

  }

  protected virtual void
  Dispose(bool disposing)

  {

  if (!disposed)

  {

  if (disposing)

  client.Dispose();

  }

  disposed = true;

   }

  public void
  Dispose()

  {

  Dispose(true);

  GC.SuppressFinalize(this);

  }

  }
  }

```

为了理解我们的 Simple Awesome CRM 是如何与 DocumentDB 一起工作的,让我们详细了解一下`Incident`类中的每一种方法,并确保我们理解以下每一项的作用。

代码 的重要初始化细节

W
wizardforcel 已提交
1517
```cs
W
wizardforcel 已提交
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
  private const string docDbUrl = "dbs/SimpleAwesomeCrm/colls/CrmObjects";

  private const string docDbEndpointUrl = "https://fastapps.documents.azure.com:443/";

  private const string docDbAuthorizationKey = "<<DocDb Key>>";
  protected DocumentClient client = null;

```

`docDbUrl`字符串表示使用 Azure 门户在文档数据库中创建的`CrmObjects`集合的相对网址。该网址的格式如下:

`dbs/<DocumentDB database name>`/cols/<documentdb 集合名称>

在我们的例子中,`SimpleAwesomeCrm`是 DocumentDB 数据库名称,`CrmObjects`是 DocumentDB 集合名称(这是我们的 CRM JSON 文档将被存储的地方)。

`docDbEndpointUrl`字符串表示运行在 Azure 上的 DocumentDB 实例的实际 URL。我们选择了子域“fastapps”,但是您可以使用其他任何东西(只要它对应于您在 Azure 门户上创建 DocumentDB 帐户时定义的内容)。

`docDbAuthorizationKey`字符串是指定的文档数据库实例的 Azure 访问键  y `docDbEndpointUrl`

`client`对象是的 DocumentDB Azure SDK。NET 实例类,负责与 DocumentDB 的所有通信。

现在我们可以建立一个到文档数据库的连接。

代码清单 7:与文档数据库的连接

W
wizardforcel 已提交
1543
```cs
W
wizardforcel 已提交
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
  private void
  Connect2DocDb()
  {

  client = new DocumentClient(new Uri(docDbEndpointUrl), 

  docDbAuthorizationKey);
  }

```

 `DocumentClient`的实例来实现的,该实例将在以后得到适当的处理。

代码 

W
wizardforcel 已提交
1559
```cs
W
wizardforcel 已提交
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
  protected virtual void Dispose(bool disposing)
  {

  if (!disposed)

  {

      if (disposing)

      client.Dispose();

  }

  disposed = true;
  }

```

建立连接后,我们现在可以打开一个事件并将其提交给 DocumentDB。我们可以这样做,如代码清单 9 所示。

W
wizardforcel 已提交
1580
```cs
W
wizardforcel 已提交
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
  public async Task<Document> Open(string description, bool check = false)
  {

  if (client == null)

      Connect2DocDb();

  if (info != null && client != null)

  {

      info.Description = description;

  Document id = await client.CreateDocumentAsync(docDbUrl, info);

  if (check)

  return (id != null) ? client.CreateDocumentQuery(docDbUrl).

                 Where(d => d.Id == 

  id.Id).AsEnumerable().FirstOrDefault() :

  null;

  else

  return id;

  }

  else

      return null;
  }

```

`client`(T2】实例)为空时,即当连接尚未建立时,`Open`方法会连接到文档数据库。

一旦建立了连接,就用`docDbUrl`和一个`IncidentInfo`实例调用`CreateDocumentAsync`。要检查文档是否成功提交到文档数据库,`check`参数可以设置为`true`

然而,为了使其工作,需要创建一个`IncidentInfo`的实例。代码清单 10 展示了我们如何从包装器做到这一点。

代码清单 10:事件包装器。打开

W
wizardforcel 已提交
1627
```cs
W
wizardforcel 已提交
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
  using System;
  using Newtonsoft.Json;
  using System.Threading.Tasks;
  using Microsoft.Azure.Documents;
  using System.Linq;
  using System.Collections.Generic;

  using CrmCore.EnumUtils;
  using CrmCore.Enums;
  using CrmCore.DateEpoch;
  using CrmCore.IncidentInfo;
  using
  CrmCore.Incident;

  namespace CrmCore
  {

  public class CrmExample

  {

      private const string cStrCaseCreated = 

  "Case created
  (as JSON) ->";

  private const string cStrTotalResults = "Total results: ";

  private const string cStrDescription = "Description: ";

  private const string cStrStatus = "Status: ";

  private const string cStrSeverity = "Severity: ";

  private const string cStrCommunication = "Communication: ";

  private const string cStrFrequency = "Frequency: ";

  private const string cStrOpened = "Opened: ";

     private const string cStrClosed = "Closed: ";

  private const string cStrDateTimeFormat = 

  "dd-MMM-yyyy
  hh:mm:ss UTC";

  public static async Task<string> OpenCase(string description)

  {

  string id = string.Empty;

  using (Incident inc = new Incident(IncidentStatus.Reported,

  IncidentSeverity.High,

  IncidentFeedbackFrequency.Every4Hours,

  IncidentCommunicationType.Bidirectional))

  {

         var issue = await inc.Open(description);

  if (issue != null)

  {

  var i = JsonConvert.DeserializeObject

  <IncidentInfo>(issue.ToString());

  Console.WriteLine(cStrCaseCreated);

  Console.WriteLine(issue.ToString());

  id = issue.Id;

  }

  }

  return id;

  }

  public static async Task<string> OpenCase(string description, 

    IncidentStatus st, IncidentSeverity sv, 

  IncidentFeedbackFrequency ff, IncidentCommunicationType ct)

  {

  string id = string.Empty;

  using (Incident inc = new Incident(st, sv, ff, ct))

  {

    var issue = await inc.Open(description);

  if (issue != null)

  {

  var i = JsonConvert.DeserializeObject

  <IncidentInfo>(issue.ToString());

  Console.WriteLine(cStrCaseCreated);

  Console.WriteLine(issue.ToString());

  id = issue.Id;

  }

  }

  return id;

  }

  public static void CheckCase(string id)

  {

  using (Incident inc = new Incident())

  {

  IEnumerable<Document> issues = inc.FindById(id);

  foreach (var
  issue in issues)

  OutputCaseDetails(issue);

  }
          }

  private static void OutputCaseDetails(object issue)

  {

  IncidentInfo i = (issue is IncidentInfo) ? 

  (IncidentInfo)issue :

  JsonConvert.DeserializeObject

  <IncidentInfo>(issue.ToString());

  Console.WriteLine(cStrDescription +
  i?.Description);

  Console.WriteLine(cStrStatus + i?.Status);

  Console.WriteLine(cStrSeverity +
  i?.Severity);

  Console.WriteLine(cStrCommunication +
  i?.CommunicationType);

  Console.WriteLine(cStrFrequency +
  i?.FeedbackFrequency);

  Console.WriteLine(cStrOpened + 

  i?.Opened?.Date.ToString(cStrDateTimeFormat));

  Console.WriteLine(cStrClosed + 

  i?.Closed?.Date.ToString(cStrDateTimeFormat));

  }
      }
  }

  using CrmCore;
  using System;
  using System.Threading.Tasks;

  namespace CustomerSuccess
  {

  class Program

  {

  static void
  Main(string[] args)

  {

  Console.Clear();

  OpenFindCase();

  Console.ReadLine();

  }

  public static async void
  OpenFindCase()

  {

  await Task.Run(

  async () =>

  {

  string id = await CrmExample.OpenCase(

  "Export failing", 

  IncidentStatus.Reported,

  IncidentSeverity.High, 

  IncidentFeedbackFrequency.Every8Hours, 

  IncidentCommunicationType.Bidirectional);

  Console.WriteLine(Environment.NewLine);

  CrmExample.CheckCase(id);

  });

  }

  }
  }

```

`OpenFindCase`将调用包装在`CrmExample.OpenCase`周围, `which`创建一个`Incident`实例并调用该实例的`Open`方法。这会返回一个`Document`实例,然后反序列化为一个`IncidentInfo`对象。最后,这个`IncidentInfo`对象通过`OutputCaseDetails`打印在屏幕上。运行这个例子将产生如图 12 所示的结果。

W
wizardforcel 已提交
1871
![](img/00015.jpeg)
W
wizardforcel 已提交
1872 1873 1874

该文档可以在 Azure 上看到,如图 13 所示。

W
wizardforcel 已提交
1875
![](img/00016.jpeg)
W
wizardforcel 已提交
1876 1877 1878 1879 1880 1881 1882 1883 1884

图 Azure 文档浏览器中的事件

当创建的文档没有特定的规范时,DocumentDB 会自动为其分配一个随机的 GUID 在本例中为:ce 333 e7b-9d6a-49e 2-a6b 9-d0b 89 d3c 5086。

现在我们已经能够创建事件,我们将把重点转移到在 Azure 中检索和查找文档。假设我们应该能够使用`IncidentInfo`对象的任何字段来搜索任何特定的标准,我们接下来需要向`Incident`类添加几个`Find`方法。

代码清单 11:查找事件类的方法

W
wizardforcel 已提交
1885
```cs
W
wizardforcel 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
  public IEnumerable<Document> FindById(string id)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

       from c in client.CreateDocumentQuery(docDbUrl)

  where c.Id == id

      select c;

  return cases;

  }

  else

      return null;
  } 

  public IEnumerable<IncidentInfo> FindByDescription(string description)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

      from c in
  client.CreateDocumentQuery<IncidentInfo>(docDbUrl)

      where c.Description.ToUpper().

          Contains(description.ToUpper())

      select c;

  return cases;

  }

  else

     return null;
  } 

  public IEnumerable<IncidentInfo> FindByDateOpenedAfter(DateTime opened)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

  from c in
  client.CreateDocumentQuery<IncidentInfo>(docDbUrl)

  where c.Opened.Epoch >=
  opened.ToEpoch()

  select c;

  return cases;

  }

  else

      return null;
  } 

  public IEnumerable<IncidentInfo> FindByDateOpenedBefore(DateTime opened)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

      from c in
  client.CreateDocumentQuery<IncidentInfo>(docDbUrl)

   where c.Opened.Epoch <
  opened.ToEpoch()

   select c;

  return cases;

  }

  else

      return null;
  } 

  public IEnumerable<IncidentInfo> FindByDateOpenedBetween(DateTime start,    

  DateTime end)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

      from c in
  client.CreateDocumentQuery<IncidentInfo>(docDbUrl)

  where c.Opened.Epoch >=
  start.ToEpoch()

  where c.Opened.Epoch < end.ToEpoch()

  select c;

     return cases;

  }

  else

      return null;
  }

  public IEnumerable<IncidentInfo> FindByStatus(IncidentStatus status)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

      from c in
  client.CreateDocumentQuery<IncidentInfo>(docDbUrl)

  where c.Status == status.ToString()

  select c;

  return cases;

  }

  else

      return null;
  } 

  public IEnumerable<IncidentInfo> FindBySeverity(IncidentSeverity 

  severity)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

      from c in
  client.CreateDocumentQuery<IncidentInfo>(docDbUrl)

  where c.Severity == severity.ToString()

  select c;

  return cases;

  }

  else

      return null;
  } 

  public IEnumerable<IncidentInfo> FindByFeedbackFrequency

  (IncidentFeedbackFrequency ff)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

      from c in
  client.CreateDocumentQuery<IncidentInfo>(docDbUrl)

  where c.FeedbackFrequency ==
  ff.ToString()

  select c;

  return cases;

  }

  else

      return null;
  } 

  public IEnumerable<IncidentInfo> FindByCommunicationType

  (IncidentCommunicationType ct)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

      from c in
  client.CreateDocumentQuery<IncidentInfo>(docDbUrl)

         where c.CommunicationType ==
  ct.ToString()

  select c;

  return cases;

  }

  else

      return null;
  }

```

`Incident`类的所有`Find`方法都是相似的——它们都是从尝试使用`Connect2DocDb`连接到 DocumentDB 开始的,如果存在连接(当`client`不为空时),则使用特定条件执行 LINQ 查询。在大多数情况下,会返回一个带有 LINQ 查询结果的`IEnumerable<IncidentInfo>`实例。

`IEnumerable`更可取,因为它描述了行为,而`List`是该行为的实现。当你使用`IEnumerable`的时候,你给了编译器一个把工作推迟到以后的机会,允许沿途可能的优化。如果使用`List`,则强制编译器立即检查结果。

无论何时使用 LINQ 表达式,也应该使用`IEnumerable`,因为只有当你指定行为时,你才会给 LINQ 一个延迟评估和优化程序的机会。

假设可能有一个或多个结果,返回的`IEnumerable<IncidentInfo>`对象需要被解析,每个结果都应该被打印出来。为了做到这一点,我们将使用我们的包装器`CrmExample`类。

让我们为`CrmExample`类中的方法`FindByDescription`创建第一个包装器。

代码清单 12:查找描述包装器

W
wizardforcel 已提交
2159
```cs
W
wizardforcel 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
  public static void FindByDescription(string description)
  {

  using (Incident inc = new Incident())

  {

      IEnumerable<IncidentInfo> issues = 

  inc.FindByDescription(description);

  Console.WriteLine("FindByDescription: " + description);

  if (issues.Count() > 0)

  {

      Console.Write(Environment.NewLine);

  foreach (var
  issue in issues)

  {

      OutputCaseDetails(issue);

    Console.Write(Environment.NewLine);

  }

  }

  Console.WriteLine(cStrTotalResults +
  issues.Count().ToString());

  }
  }

```

代码清单 13 描述了如何从主程序调用它。

代码清单 13:查找描述调用

W
wizardforcel 已提交
2204
```cs
W
wizardforcel 已提交
2205 2206 2207 2208 2209 2210
  CrmExample.FindByDescription("Exception");

```

图 14 和 15 显示了屏幕上和 Azure 中的`FindByDescription`结果。

W
wizardforcel 已提交
2211
![](img/00017.jpeg)
W
wizardforcel 已提交
2212 2213 2214

图 14:在屏幕上查找描述结果

W
wizardforcel 已提交
2215
![](img/00018.jpeg)
W
wizardforcel 已提交
2216 2217 2218 2219 2220 2221 2222

图 15:Azure 中的 FindByDescription 结果

现在我们来考察一下方法`FindByDateOpenedAfter`。这将从文档数据库中检索任何打开时`DateTime`等于或大于用于搜索的参数的文档。

代码清单 14: FindByDateOpenedAfter 包装器

W
wizardforcel 已提交
2223
```cs
W
wizardforcel 已提交
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
  public static void FindByDateOpenedAfter(DateTime opened)
  {

  using (Incident inc = new Incident())

  {

      IEnumerable<IncidentInfo> issues = 

  inc.FindByDateOpenedAfter(opened);

  Console.WriteLine("FindByDateOpenedAfter:
  " + 

  opened.ToString(cStrDateTimeFormat));

  if (issues.Count() > 0)

  {

      Console.Write(Environment.NewLine);

  foreach (var
  issue in issues)

  {

      OutputCaseDetails(issue);

  Console.Write(Environment.NewLine);

  }

  }

  Console.WriteLine(cStrTotalResults +
  issues.Count().ToString());

  }
  }

```

代码清单 15 描述了从主程序调用`FindByDateOpenedAfter`

代码清单 15:调用后查找

W
wizardforcel 已提交
2271
```cs
W
wizardforcel 已提交
2272 2273 2274 2275 2276 2277
  CrmExample.FindByDateOpenedAfter(new DateTime(2016, 3, 4, 16, 03, 58, DateTimeKind.Utc));

```

该操作产生图 16 和 17 中的结果。

W
wizardforcel 已提交
2278
![](img/00019.jpeg)
W
wizardforcel 已提交
2279 2280 2281

图 16:在屏幕上显示结果后查找

W
wizardforcel 已提交
2282
![](img/00020.jpeg)
W
wizardforcel 已提交
2283 2284 2285 2286 2287 2288 2289

图 17:在 Azure 中找到结果后的查找时间

接下来我们来考察一下方法`FindBySeverity`。这将从文档数据库中检索分配了特定`IncidentSeverity`的任何文档。

代码清单 16:查找比永久包装器

W
wizardforcel 已提交
2290
```cs
W
wizardforcel 已提交
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
  public static void FindBySeverity(IncidentSeverity severity)
  {

  using (Incident inc = new Incident())

  {

      IEnumerable<IncidentInfo> issues = inc.FindBySeverity(severity);

  Console.WriteLine("FindBySeverity: " + 

  EnumUtils.stringValueOf(severity));

  if (issues.Count() > 0)

  {

      Console.Write(Environment.NewLine);

  foreach (var
  issue in issues)

  {

      OutputCaseDetails(issue);

  Console.Write(Environment.NewLine);

  }

  }

  Console.WriteLine(cStrTotalResults +
  issues.Count().ToString());

  }
  }

```

代码清单 17 描述了从主程序调用`FindBySeverity`

代码清单 17:findbyeverity 调用

W
wizardforcel 已提交
2335
```cs
W
wizardforcel 已提交
2336 2337 2338 2339 2340 2341
  CrmExample.FindBySeverity(IncidentSeverity.Urgent);

```

图 18 显示了结果。

W
wizardforcel 已提交
2342
![](img/00021.jpeg)
W
wizardforcel 已提交
2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357

图 18:屏幕上的查找结果

我们已经看到了使用围绕着`CrmExample``Incident`类的一些`Find`方法从文档数据库中检索文档是多么容易。请注意,`Find`方法的关键区别在于它们在每个 LINQ 查询中的内部差异。

在这个阶段,我们知道如何打开事件,并在整个`Find`方法中使用不同的 LINQ 搜索查询找到它们。`IncidentInfo`旨在使任何事件的分配评论和资源从无到多不等。

注释和资源都是`Comments``AllocatedResource`类的数组。当一个新的事件被打开时,两者都是空的,所以要添加注释和资源,我们必须定义一个方法来添加它们。

一个`Comment`实例将包括一个`Description``UserId`(提交评论的人)、`AttachmentUrl`(与评论相关的附件的网址位置)和`When` ( `DateTime`已提交)。

让我们探索如何在`Incident`类中实现一个方法,以便为事件添加注释。

代码清单 18:添加注释实现

W
wizardforcel 已提交
2358
```cs
W
wizardforcel 已提交
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
  public async Task<IncidentInfo> AddComment(string id, string userId, 
  string comment, string attachUrl)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

      from c in
  client.CreateDocumentQuery(docDbUrl)

  where
  c.Id.ToUpper().Contains(id.ToUpper())

  select c;

  IncidentInfo issue = null;

  Document oDoc = null;

  foreach (var
  cs in cases)

  {

      var it = await client.ReadDocumentAsync(cs.AltLink);

  oDoc = it;

  issue = (IncidentInfo)(dynamic)it.Resource;

  break;

  }

  if (oDoc != null)

  {

  Comment c = new
  Comment();

  c.AttachmentUrl = attachUrl;

  c.Description = comment;

  c.UserId = userId;

  c.When = new DateEpoch(DateTime.UtcNow);

  List<Comment> cMts = new List<Comment>();

  if (issue?.Comments?.Length > 0) {

      cMts.AddRange(issue.Comments);

  cMts.Add(c);

  oDoc.SetPropertyValue(cStrComments, cMts.ToArray());

  }

  else {

      cMts.Add(c);

  oDoc.SetPropertyValue(cStrComments, cMts.ToArray());

  }

  var updated = await client.ReplaceDocumentAsync(oDoc);

  issue = (IncidentInfo)(dynamic)updated.Resource;

     }

  return issue;

  }

  else

      return null;
  }

```

`AddComment`首先检查是否有到 DocumentDB 的连接,如果没有,则通过调用`Connect2DocDb`建立连接。

连接到文档数据库后,接下来使用`CreateDocumentQuery`执行 LINQ 查询。这是通过使用我们想要添加注释的事件的标识来完成的。

方法`CreateDocumentQuery`返回一个`IOrderedQueryable<Document>`实例,我们需要循环遍历该实例(即使`IOrderedQueryable<Document>`上的计数可能是 1),以便执行`ReadDocumentAsync`并检索与被查询的标识对应的特定`Document`实例。

`ReadDocumentAsync`方法需要从 DocumentDB 中读取的文档的`AltLink`(替代 URL 链接)。`AltLink``Document`实例的一个属性,它表示我们想要获取的 DocumentDB 集合中的文档的 URL。

有趣的是:要从调用`ReadDocumentAsync`返回的结果中获取`IncidentInfo`实例,我们必须将结果转换为`issue = (IncidentInfo`)(动态)它。资源;这将被转换成一个`IncidentInfo`实例。

随后,创建一个`Comment`的数组实例,并将其添加到现有的`Document`实例中,该实例表示正在查询的文档数据库文档(事件)。使用属性`SetPropertyValue`通过`Document`对象的实例添加`Comment`的数组实例。

一旦将`Comment`对象添加到`Document`实例中,就调用`ReplaceDocumentAsync`,并将添加了`Comment`的文档写回 DocumentDB 集合(在 Azure 中更新)。代表该更新文档的更新的`IncidentInfo`对象被返回给`AddComment`的调用者。

为了实现`AddComment` `,`,我们需要在`CrmExample`中创建一个包装器。让我们看看这是如何做到的。

代码清单 19:添加注释包装器

W
wizardforcel 已提交
2468
```cs
W
wizardforcel 已提交
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
  public static async void
  AddComment(string id, string userId, string 

  comment, string attachUrl)
  {

  using (Incident inc = new Incident())

  {

      await Task.Run(

  async () =>

  {

      IncidentInfo ic = await inc.AddComment(id, userId, comment, 

  attachUrl);

  OutputCaseDetails(ic);

  });

  }
  }

```

代码清单 20 描述了如何从主程序调用它。

代码清单 20:添加注释调用

W
wizardforcel 已提交
2502
```cs
W
wizardforcel 已提交
2503 2504 2505 2506 2507 2508 2509
  CrmExample.AddComment("4b992d62-4750-47d2-ac4a-dbce2ce85c12", "efreitas", "Request
  for feedback", "https://eu3.salesforce.com/...");

```

结果如图 19 所示。

W
wizardforcel 已提交
2510
![](img/00022.jpeg)
W
wizardforcel 已提交
2511 2512 2513 2514 2515 2516 2517

图 19:在 Azure 中添加注释结果

为了给事件增加资源,我们需要采取与`AddComment`方法相同的方法。这意味着我们将在`Incident`类中实现一个`AddResource`方法,并在`CrmExample`中创建一个包装器。

代码清单 21:添加资源实现

W
wizardforcel 已提交
2518
```cs
W
wizardforcel 已提交
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
  public async Task<IncidentInfo> AddResource(string id, IncidentStatus stage, string engineer, DateTime
  st, DateTime end)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

  var cases =

      from c in
  client.CreateDocumentQuery(docDbUrl)

  where
  c.Id.ToUpper().Contains(id.ToUpper())

  select c;

  IncidentInfo issue = null;

  Document oDoc = null;

  foreach (var
  cs in cases)

  {

      var it = await client.ReadDocumentAsync(cs.AltLink);

  oDoc = it;

  issue = (IncidentInfo)(dynamic)it.Resource;

  break;

  }

  if (oDoc != null)

  {

      AllocatedResource rc = new
  AllocatedResource();

  rc.End = new DateEpoch((end != null) ? end.ToUniversalTime() 

  : DateTime.UtcNow);

   rc.Engineer = engineer;

  rc.Stage = EnumUtils.stringValueOf(stage);

  rc.Start = new DateEpoch((st != null) ? st.ToUniversalTime() 

  : DateTime.UtcNow);

  List<AllocatedResource> rRsc = new List<AllocatedResource>();

  if (issue?.Resources?.Length > 0)

  {

       rRsc.AddRange(issue.Resources);

  rRsc.Add(rc);

  oDoc.SetPropertyValue(cStrResources, rRsc.ToArray());

  }

  else

       {

      rRsc.Add(rc);

  oDoc.SetPropertyValue(cStrResources, rRsc.ToArray());

  }

  var updated = await client.ReplaceDocumentAsync(oDoc);

  issue = (IncidentInfo)(dynamic)updated.Resource;

     }

  return issue;

  }

  else

  return null;
  }

```

可以看到`AddResource`的实现和`AddComment`的实现几乎一样。

`AddResource`首先检查是否有到 DocumentDB 的连接,如果没有,则通过调用`Connect2DocDb`建立连接。

连接到文档数据库后,接下来使用`CreateDocumentQuery`执行 LINQ 查询。这是通过使用我们要添加资源的事件的标识来完成的。

方法`CreateDocumentQuery`返回一个`IOrderedQueryable<Document>`集合,我们需要循环通过该集合来执行一个`ReadDocumentAsync`并检索与被查询文档的标识相对应的特定`Document`实例。

`ReadDocumentAsync`方法期望提供`AltLink`

要从调用`ReadDocumentAsync`的返回结果中获取`IncidentInfo`实例,我们必须将结果转换为`issue = (IncidentInfo`)(动态)它。资源;这将被转换成一个`IncidentInfo`实例。

创建`AllocatedResource`的数组实例,并将其添加到现有的`Document`实例中,该实例表示正在查询的文档数据库文档(事件)。使用`SetPropertyValue`方法通过`Document`的实例添加`AllocatedResource`的数组实例。

一旦`AllocatedResource`属性被添加到`Document`实例中,就调用`ReplaceDocumentAsync`,并且添加了`AllocatedResource`的文档被写回文档数据库集合。代表该更新文档的更新的`IncidentInfo`对象被返回给`AddResource`的调用者。

要做到这一点,`AddResource`需要从`CrmExample`类进行包裹和暴露。

代码清单 22:添加资源包装器

W
wizardforcel 已提交
2638
```cs
W
wizardforcel 已提交
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670
  public static async void AddResource(string id, IncidentStatus stage, 

  string engineer, DateTime st, DateTime end)
  {

  using (Incident inc = new Incident())

  {

      await Task.Run(

  async () =>

  {

  IncidentInfo ic = await inc.AddResource(id, stage, 

  engineer, st, end);

  OutputCaseDetails(ic);

  });

  }
  }

```

最后,如代码清单 23 所示,可以从主程序中调用`AddResource`

代码 

W
wizardforcel 已提交
2671
```cs
W
wizardforcel 已提交
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
  CrmExample.AddResource("4b992d62-4750-47d2-ac4a-dbce2ce85c12", IncidentStatus.IssueFound, "bob smith", DateTime.Now, DateTime.Now);

```

其输出也可以在图 19 中看到。

虽然知道如何删除注释和分配资源在某些时候可能会有用,但我们不会在这本电子书中实现该功能。

到目前为止,我们已经实现了简单可怕的客户关系管理系统,这样就可以提交和查询事件,以及添加评论和分配资源——所有这些都是为了完成一项工作。

更改现有文档(`IncidentInfo`实例)属性的能力至关重要,因为可能需要更改文档中的`IncidentStatus``IncidentSeverity`或任何其他属性。

让我们在`IncidentInfo`类内实现`ChangePropertyValue`方法。

代码清单 24:变更属性值实现

W
wizardforcel 已提交
2688
```cs
W
wizardforcel 已提交
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
  public async Task<IncidentInfo> ChangePropertyValue(string id, string 

  propValue, string propName)
  {

  if (client == null) Connect2DocDb();

  if (info != null && client != null)

  {

      var cases =

      from c in
  client.CreateDocumentQuery(docDbUrl)

  where
  c.Id.ToUpper().Contains(id.ToUpper())

  select c;

  IncidentInfo issue = null;

  Document oDoc = null;

  foreach (var
  cs in cases)

  {

      var it = await client.ReadDocumentAsync(cs.AltLink);

  oDoc = it;

  issue = (IncidentInfo)(dynamic)it.Resource;

  break;

  }

  if (oDoc != null)

  {

      switch (propName)

  {

      case cStrSeverityProp:

      oDoc.SetPropertyValue(cStrSeverityProp, propValue);

  break;

  case cStrStatusProp:

      oDoc.SetPropertyValue(cStrStatusProp, propValue);

  break;

  case cStrFrequencyProp:

  oDoc.SetPropertyValue(cStrFrequencyProp, propValue);

  break;

  case cStrCTProp:

      oDoc.SetPropertyValue(cStrCTProp, propValue);

  break;

  case cStrClosedProp:

      oDoc.SetPropertyValue(cStrClosedProp, issue.Closed);

  break;

  }

    var updated = await client.ReplaceDocumentAsync(oDoc);

  issue = (IncidentInfo)(dynamic)updated.Resource;

  }

  return issue;

  }

  else

      return null;
  }

```

就像`AddComment``AddResource`一样,`ChangePropertyValue`也遵循同样的逻辑。它试图通过`Connect2DocDb`和调用`CreateDocumentQuery`并查询特定的标识来连接到文档数据库。被查询单据的标识返回`IOrderedQueryable<Document>`

然后通过调用`ReadDocumentAsync`返回特定的`Document`实例,并使用`SetPropertyValue`更改各自的属性。

接下来,通过调用`ReplaceDocumentAsync`,在 DocumentDB 集合上更新当前的`Document`

为了做到这一点——为了从主程序中调用它——我们需要将`ChangePropertyValue`包装在`CrmExample`中,如代码清单 25 所示。

代码清单 25:变更属性值包装器

W
wizardforcel 已提交
2796
```cs
W
wizardforcel 已提交
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
  public static async void
  ChangePropertyValue(string id, string propValue, 

  string propName)
  {

  using (Incident inc = new Incident())

  {

      await Task.Run(

  async () =>

  {

      IncidentInfo ic = await inc.ChangePropertyValue(id, 

  propValue, propName);

  OutputCaseDetails(ic);

  });

   }
  }

```

如代码清单 26 所示,这可以从主程序中调用。

代码清单 26:变更属性值调用

W
wizardforcel 已提交
2830
```cs
W
wizardforcel 已提交
2831 2832 2833 2834
  CrmExample.ChangePropertyValue("4b992d62-4750-47d2-ac4a-dbce2ce85c12", EnumUtils.stringValueOf(IncidentStatus.FeedbackRequested), "Status");

```

W
wizardforcel 已提交
2835
在这一章中,我们已经看到了如何使用 DocumentDB 和 C# 来实现一个简单而有效的客户关系管理应用编程接口,您可以根据自己的需求进行建模。
W
wizardforcel 已提交
2836 2837 2838 2839 2840 2841 2842 2843

鉴于其可靠性、稳定性、健壮性和易用性,DocumentDB 是我们的首选。它的应用编程接口很容易理解,只需要很少的文档,并且它非常适合构建一个简单的客户关系管理应用编程接口的概念,这使得我们能够迎合[第 1 章](1.html#_Chapter_1_)中解释的概念。

这里展示的客户关系管理示例代码足以让您入门,并且应该很容易通过添加功能进行扩展。

我们邀请您将自己的想法添加到这个代码库中,并扩展第 1 章中解释的概念——请随意继续改进它。

在接下来的几章中,我们将探索客户服务可以帮助您的业务增长和增加其价值的一些方法。我们还将了解简单的逆向工程如何成为更快解决问题的宝贵资产。