【问题标题】:Can we set the Convertible attribute using Castle Windsors Fluent API我们可以使用 Castle Windsors Fluent API 设置 Convertible 属性吗
【发布时间】:2013-08-08 18:20:56
【问题描述】:

我正在尝试从我的 api 的 web.config 填充对象集合。我已经正确地提取了这些对象的所有属性,除了一个,这是我们对象中的另一个自定义对象。

现在,如果我将可转换属性添加到类,一切正常,但是通过在我们的域中添加对 castle.windsor 的外部引用,这破坏了我们的洋葱架构。可转换属性的复杂参数详解here

web.config 的城堡部分中的 Xml(去掉了绒毛)如下所示:

<component id="uniqueId" type="Project.Domain.Objects.Object, Project.Domain">
    <parameters>
    <Id>1</Id>
    <Name>name1</Name>
    <geofenceLocation>
      <parameters>
      <latitude>12</latitude>
      <longitude>44</longitude>
      <radius>100</radius>
      </parameters>
    </geofenceLocation>
    </parameters>
  </component>

我试图在不添加可转换属性的情况下进行序列化的对象是 geofenceLocation 对象。有没有办法将属性注入到对象中,以便我们可以在 Api 中注入 Convertible 属性,或者有其他方法可以解决这个问题吗?

感谢您的宝贵时间。

【问题讨论】:

    标签: c# dependency-injection web-config castle-windsor asp.net-web-api


    【解决方案1】:

    您可以像这样将地理围栏定位作为一个新组件来处理:

    <component id="geofenceLocation" type="your.type.here, your.assembly">
        <parameters>
          <latitude>12</latitude>
          <longitude>44</longitude>
          <radius>100</radius>
        </parameters>
    </component>
    

    以及您的其他组件对它的引用,如下所示:

    <component id="uniqueId" type="Project.Domain.Objects.Object, Project.Domain">
        <parameters>
          <Id>1</Id>
          <Name>name1</Name>
          <geofenceLocation>${geofenceLocation}</geofenceLocation>
        </parameters>
    </component>
    

    希望对您有所帮助。再见

    【讨论】:

    • 感谢您的回答。我们决定暂时将这些存储在数据表中。我以前见过这种解决方案,但我认为它会导致配置文件非常混乱,所以我想避免它。
    • 不客气。如果您认为我的答案是大多数情况下的通用解决方案,请将其标记为正确答案,以便对其他人有所帮助。谢谢
    • 我会接受它,因为它确实解决了我正在研究的问题。如果其他人提供了更适合上述问题的更清洁的解决方案,请随时将其添加到某处并告诉我:)
    猜你喜欢
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 2012-06-30
    • 2012-03-08
    相关资源
    最近更新 更多