创建一个配置类,从基类继承,而不在config.json中重复。

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

Create configuration class with interitance from base class without duplication in config.json

问题

以下是您要翻译的内容:

"I have a few similar classes with similar configurations in it.

So, I want to create a base Class with common configuration, after that inherit it in child config class and in the end initialize it in logic class:

  1. public class OptionsBase
  2. {
  3. public string ConnectionKey { get; set; }
  4. }
  5. public class OptionsChild: OptionsBase
  6. {
  7. public string ChildName{ get; set; }
  8. }

My json file:

  1. "OptionsBase": {
  2. "ConnectionKey": "basekey"
  3. },
  4. "OptionsChild": {
  5. "ChildName": "child1"
  6. }

Expecting to use it like this:

  1. public class Child1
  2. {
  3. private readonly OptionsChild _options;
  4. public Child1(IOptions<OptionsChild> options)
  5. {
  6. _options = options.Value;
  7. }
  8. public void Example()
  9. {
  10. var valueFromParent = _options.ConnectionKey;
  11. }
  12. }

How can I do it without duplication in json file and without using additional common class with common options?

It is web app, config initialization executed like this:

  1. service.Configure<OptionsChild>(config.GetSection("OptionsChild"));

Thank you!

EDIT: I found the solution and added an answer. Please, let me know if the solution is OK."

希望这对您有所帮助。如果您有任何其他问题,请随时提出。

英文:

I have a few similar classes with similar configurations in it.

So, I want to create a base Class with common configuration, after that inherit it in child config class and in the end initialize it in logic class:

  1. public class OptionsBase
  2. {
  3. public string ConnectionKey { get; set; }
  4. }
  5. public class OptionsChild: OptionsBase
  6. {
  7. public string ChildName{ get; set; }
  8. }

My json file:

  1. &quot;OptionsBase&quot;: {
  2. &quot;ConnectionKey&quot;: &quot;basekey&quot;
  3. },
  4. &quot;OptionsChild&quot;: {
  5. &quot;ChildName&quot; &quot;child1&quot;
  6. }

Expecting to use it like this:

  1. public class Child1
  2. {
  3. private readonly OptionsChild _options;
  4. public Child1(IOptions&lt;OptionsChild&gt; options)
  5. {
  6. _options = options.Value;
  7. }
  8. public void Example()
  9. {
  10. var valueFromParent = _options.ConnectionKey;
  11. }
  12. }

How can I do it without duplication in json file and without using additional common class with common options?

It is web app, config initialization executed like this:

  1. service.Configure&lt;OptionsChild&gt;(config.GetSection(&quot;OptionsChild&quot;));

Thank you!

EDIT: I found the solution and added an answer. Please, let me know if the solution is OK.

答案1

得分: 1

我找到了一个答案,在这里 描述了,正是我想要的内容。

这有点绕,需要为每个配置类配置两次,但这样我可以在子选项类中使用基本选项,而无需在JSON文件中重复配置。

请告诉我这个解决方案是否可以。

英文:

I have found an answer, here it is described, exactly what I wanted to receive.

It is a bit workaround, need to configure each configuration class twice, but this way I can use base options without duplicating options in child options class and also I don't need to duplicate configuration in JSON file

Please, let me know if the solution is OK.

huangapple
  • 本文由 发表于 2023年2月14日 22:31:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449305.html
匿名

发表评论

匿名网友

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

确定