使用 NHibernate 的可扩展域模型

发布于 2024-08-02 21:17:49 字数 564 浏览 10 评论 0原文

我目前正在设计解决方案,其中域模型和存储库可以通过应用程序插件进行扩展。现在,我遇到了一些问题,我在下面列出了这些问题。

  1. 我的第一个问题是使域模型可扩展。我正在考虑在这里使用继承,但老实说,我不知道如何利用扩展同一域对象的多个插件程序集。我有点倾向于使每个域对象部分化并允许插件以这种方式扩展它。如果我有多个扩展同一域对象的插件,我不必担心为每个插件加载不同的扩展域程序集。我在运行时仍然只有一个合并的域对象。对此有什么想法吗?

  2. 另一个问题是扩展 NHibernate 映射文件。我可以让每个程序集嵌入它所扩展的域对象的映射文件,并让我的 NHibernate 管理器加载它,而不是核心域中提供的文件。再次,问题是如果我有多个插件扩展同一个域对象怎么办。我可以让一个插件覆盖另一个插件的映射文件。 我对最后一个问题的解决方案并不是那么好,但我正在考虑在插件程序集中包含一个校验和,作为扩展它之前使用的原始映射文件的签名。我可以在加载期间验证此校验和,并且仅在校验和匹配时才加载插件映射。相当丑陋,但至少我不会覆盖任何与插件程序集中用于扩展的基本地图不同的地图。

无论如何,我想听听你们对此有何看法。谢谢!

I'm currently designing solution where the domain model & the repository can be extended by application plugins. Now, i have come across a few issues that i am listing below.

  1. My first issue is making domain model extensible. I was thinking about using inheritance here, but honestly, i have no idea how i can leverage multiple plugin assemblies extending the same domain object. I'm kind of leaning toward making every domain object partial and allowing plugins extend it this way. In case i have multiple plugins extending the same domain object, i won't have to worry about loading different extended domain assemblies for each plugin. I would still have only one merged domain object at run-time. Any ideas on this?

  2. Another issue is extending the NHibernate mapping file. I could have each assembly embed mapping file for the domain object it's extending and have my NHibernate manager load it instead of the one provided in the core domain. Once again, the issue is what if i have multiple plugins extending the same domain object. I could have one plugin overriding mapping file for the other.
    The solution i have to the last problem is not so great, but I was thinking about including a checksum into the plugin assembly as a signature for the original mapping file it used before extending it. I can verify this checksum during load and only load the plugin map if the checksums match. Pretty ugly, but at least i won't be overriding any maps that differ from the base map used to extend upon in the plugin assembly.

Any way, i'd like to hear what you guys think about this. Thanks!

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

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

发布评论

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

评论(2

情绪 2024-08-09 21:17:49

好消息是,您所要求的事情是可能的,而且管理起来并不困难。

关于插件管理,你可以看看Microsoft Prism (http://msdn .microsoft.com/fr-fr/magazine/cc785479.aspx)这是关于模块化应用程序开发的几个不错的功能。

关于 1.您可以在单独的映射、单独的程序集中映射子类,查找 NH 文档。子类的单独映射文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <subclass name="YourClassFullName, YourPluginAssemblyName"
            extends="YourParentClassFullName, TheAssemblyWhereYourBaseClassIsDefined"
            discriminator-value="whateveryouwant">
    ... add your subclass mapping here ...
  </subclass>
</hibernate-mapping>

关于 2. 您可以保留核心域映射。一种更简单的方法是创建一个服务(假设是 IMappingLoader),您的插件可以使用它来注册额外的映射(而不覆盖基类映射)。此服务的实现会将您的映射添加到 NH 配置类。例如,在 Microsoft Prism 中,所有插件都必须实现 IModule 接口,该函数在加载时会调用 Initialize()。此函数是调用 IMappingLoader 服务的理想位置。

希望有帮助。

The good news is that what you are asking for is possible and not that difficult to manage.

About plugin management, you can take a look at Microsoft Prism (http://msdn.microsoft.com/fr-fr/magazine/cc785479.aspx) which as several nice features about modular application development.

About 1. You can map subclasses in separate mappings, in separate assemblies, look for NH documentation. A separate mapping file for a subclass looks like this :

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <subclass name="YourClassFullName, YourPluginAssemblyName"
            extends="YourParentClassFullName, TheAssemblyWhereYourBaseClassIsDefined"
            discriminator-value="whateveryouwant">
    ... add your subclass mapping here ...
  </subclass>
</hibernate-mapping>

About 2. You can keep your core domain mapping. A simpler way would be to create a service (let's say IMappingLoader) your plugins can use to register your extra mappings (without overriding the base class mapping). Your implementation of this service would add your mapping to NH Configuration class. For example, in Microsoft Prism, all your plugins must implement the IModule interface, which function Initialize() is called when it is loaded. This function is the ideal place to call your IMappingLoader service.

Hope it helped.

め可乐爱微笑 2024-08-09 21:17:49

为了使域模型可扩展,我将使用大量工厂。工厂可以通过依赖注入进行换入/换出,并且域对象应该针对接口进行编码。

例如,可以通过 Fluent NHibernate 来完成映射,并且这些可以在该插件程序集中。

最后,我将向该插件程序集添加可加载配置,该程序集设置 DI 容器并加载新映射。对于主组件,可以有用于插件配置的扫描仪。也许 MEF 在这里可能会有所帮助,或者您可以自己制作,这应该不会很复杂。

To get domain model extensible I'll use lots of factories. Factories can be swap in/out via dependency injection and domain objects should be coded against interfaces.

Mapping could be done for instance via Fluent NHibernate and these could be in that plugin assembly.

Finally I would add loadable configuration to that plugin assembly, which setups DI container and load new mappings. For main assembly there could be scanner for plugin configurations. Maybe MEF could be helpful here or you could make your own, which should not be complicated.

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