现有的WCF服务的xml转换,需要与MEF集成吗?

发布于 2024-10-09 09:17:12 字数 347 浏览 0 评论 0原文

我的应用程序是在WCF中的xml转换。现在需要更改以与 MEF 集成。哪种是实现 MEF 的最佳方式,或者我应该使用哪种类型的架构来以更少的工作量和更少的现有代码更改来实现?

编辑

说明:

我有四个酒店的xml改造 在wcf服务中。一端是固定的 格式xml和另一端不同 每个新酒店的 xml 格式。 另有20家酒店工作即将到来。为了 我需要一些重复的工作 可重用和可扩展的架构。 我想转换现有的 使用 MEF 进行架构升级 未来的视角。所以我可以做得更好 用于接下来的 20 个酒店 xml 转换。

My application is in WCF of xml transformation. now need to change to integrate with MEF. which is the best way to implement MEF or which type of architecture should i use to implement with less effort and less change in existing code?

EDIT

Explanation:

I have four hotel xml transformation
in wcf service. At one end it is fixed
format xml and another end different
xml format for each new hotel.and
another 20 hotel work will come. for
this repetative work i need some
re-usable and extendable architecture.
i want to convert existing
architecture upgrade with MEF for
future perspective. so i can do better
for next 20 hotel xml transformation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌吟 2024-10-16 09:17:12

您的 Xml 转换进展如何?是通过代码还是 XSLT?

如果通过代码,我会定义一个 IXmlTranslator 来将您的 xml 转换为通用模型:

public interface IXmlTranslator
{
  XmlModel Translate(XElement element);
}

其中 XmlModel 是您的通用模型:

public class XmlModel
{
  // Properties
}

您需要具体知道要使用哪个翻译器,因此您需要传递某种元数据,因此我们将定义一个名称:

public interface INamedMetadata
{
  string Name { get; }
}

因此示例翻译器可能如下所示:

[Export(typeof(IXmlTranslator),
 ExportMetadata("Name", "Null")]
public class NullXmlTranslator : IXmlTranslator
{
  public XmlModel Translate(XElement element)
  {
    return null;
  }
}

MEF 将负责将元数据投影到 INamedMetadata 的实例中。接下来,创建一个使用 IXmlTranslator 的服务:

[Export]
public class XmlTranslatorService
{
  private readonly IEnumerable<Lazy<IXmlTranslator, INamedMetadata>> _translators;

  [ImportingConstructor]
  public XmlTranslatorService(IEnumerable<Lazy<IXmlTranslator, INamedMetadata>> translators)
  {
    _translators = translators;
  }

  public XmlModel Translate(string name, XElement element)
  {
    var translator = GetTranslator(name);

    if (translator == null)
      throw new ArgumentException("No translator is available to translate the target xml.");

    return translator.Translate(element);
  }

  private IXmlTranslator GetTranslator(string name)
  {
    var translator = _translators
      .Where(t => t.Metadata.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
      .Select(t => t.Value)
      .FirstOrDefault();

    return translator;
  }
}

我已将可用翻译器的枚举作为构造函数参数的一部分,因为它定义了服务工作所需的依赖项。 MEF 将负责在组合时注入此枚举。

您需要做的是将 XmlTranslatorService 的实例导入到您想要使用它的任何类中,或者您可以直接从您的初始化一个实例CompositionContainer,例如:

var service = container.GetExportedValue<XmlTranslatorService>();

剩下的唯一一件事就是

  1. 为每种酒店类型创建专门的转换器,将其转换为通用的 XmlModel 模型类。
  2. XmlModel 类序列化到目标 xml 中。

希望这能为您指明正确的方向?

How are you doing your Xml transformation? Is it through code, or XSLT?

If through code, I would define an IXmlTranslator that converts your xml into a common model:

public interface IXmlTranslator
{
  XmlModel Translate(XElement element);
}

Where XmlModel is your common model:

public class XmlModel
{
  // Properties
}

You'd need to specifically know which translator to use, so you'd need to pass in some sort of metadata, so we'll define a name:

public interface INamedMetadata
{
  string Name { get; }
}

So an example translator could look like:

[Export(typeof(IXmlTranslator),
 ExportMetadata("Name", "Null")]
public class NullXmlTranslator : IXmlTranslator
{
  public XmlModel Translate(XElement element)
  {
    return null;
  }
}

MEF will take care of projecting your metadata into an instance of INamedMetadata. Next, create a service which consumes IXmlTranslators:

[Export]
public class XmlTranslatorService
{
  private readonly IEnumerable<Lazy<IXmlTranslator, INamedMetadata>> _translators;

  [ImportingConstructor]
  public XmlTranslatorService(IEnumerable<Lazy<IXmlTranslator, INamedMetadata>> translators)
  {
    _translators = translators;
  }

  public XmlModel Translate(string name, XElement element)
  {
    var translator = GetTranslator(name);

    if (translator == null)
      throw new ArgumentException("No translator is available to translate the target xml.");

    return translator.Translate(element);
  }

  private IXmlTranslator GetTranslator(string name)
  {
    var translator = _translators
      .Where(t => t.Metadata.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
      .Select(t => t.Value)
      .FirstOrDefault();

    return translator;
  }
}

I've made the enumerable of available translators part of the constructor arguments, as it defines dependencies that are required for the service to work. MEF will take care of injecting this enumerable at composition time.

What you need to do, is either Import an instance of the XmlTranslatorService into whatever class you want to use it from, or you can initialise an instance directly from your CompositionContainer, e.g.:

var service = container.GetExportedValue<XmlTranslatorService>();

The only thing remaining would be

  1. Creating specialised translators for each of the hotel types into the common XmlModel model class.
  2. Serialisation of the XmlModel class into the target xml.

Hope that points you in the right direction?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文