查找MS Graph常量以替换魔术字符串

huangapple go评论82阅读模式
英文:

Looking MS Graph constants to replace magic strings

问题

我正在使用MS Graph SDK为我的Blazor Server应用程序获取一些数据。因为我正在使用更高级的请求,所以我正在使用一些由魔术字符串表示的设置,比如:

var queryOptions = new List<QueryOption>()
{
    new QueryOption("$count", "true"),
    new QueryOption("ConsistencyLevel", "eventual")
};

或者

var countElement = (JsonElement) users.AdditionalData["@odata.count"];

我想尽可能删除这些魔术字符串。我原计划只是创建一个用于保存常量的类,但我想问一下是否已经存在类似的东西,以避免重复发明轮子。我尝试查看了MS Graph SDK文档,但没有找到相关信息。

英文:

I am using MS Graph SDK to fetch some data for my Blazor Server app. Because I am using more advanced requests, I am using some settings represented by magic strings, like:

var queryOptions = new List&lt;QueryOption&gt;()
{
     new QueryOption(&quot;$count&quot;, &quot;true&quot;),
     new QueryOption(&quot;ConsistencyLevel&quot;, &quot;eventual&quot;)
};

or

var countElement = (JsonElement) users.AdditionalData[&quot;@odata.count&quot;];

I would like to remove as many of these magic strings as possible. I was planning on just making a class for holding constants but I wanted to ask if something like that already exists to avoid re-inventing the wheel. I tried looking through MS Graph SDK docs but found nothing.

答案1

得分: 2

最近,v5 SDK发布了一些重大变更,因为底层生成器的更改。

通过查看升级指南的这一部分,支持count已经添加到代码库中,但像consistencyLevel这样的其他内容仍然需要使用“魔术字符串”来完成:

var count = await graphServiceClient.Users.Count.GetAsync(requestConfiguration =>
      requestConfiguration.Headers.Add("ConsistencyLevel","eventual"));

因此,您可以创建自己的静态类,以有意义的方式包含所需的常量字符串。以下是一个即兴的示例:

public static class GraphParameters
{
    public const string ConsistencyLevel = "ConsistencyLevel";

    public static class ConsistencyLevelValue
    {
        public const string Eventual = "eventual";
    }
}
英文:

Recently the v5 SDK came out with some big breaking changes due to the change of the underlying generator.

By looking at this section of the upgrade guide the support for count already made it into the code base, but other stuff like the consistencyLevel must still be done with magic strings:

var count = await graphServiceClient.Users.Count.GetAsync(requestConfiguration =&gt;
      requestConfiguration.Headers.Add(&quot;ConsistencyLevel&quot;,&quot;eventual&quot;));

So just make up your own static class containing the needed const strings in a meaningful way. Here is a spontaneous example:

public static class GraphParameters
{
    public const string ConsistencyLevel = &quot;ConsistencyLevel&quot;;

    public static class ConsistencyLevelValue
    {
        public const string Eventual = &quot;eventual&quot;;
    }
}

huangapple
  • 本文由 发表于 2023年3月3日 19:04:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定